<?php
namespace appcommonlogic;
/**
* author Lijy
* date 2020/6/17
* Class RedisLogic
* @package appcommonlogic
*/
class RedisLogic
{
/**
* RedisLogic constructor.
*/
public function __construct()
{
$this->redisConnect([
'host' => env('REDIS.HOST'),
'port' => env('REDIS.PORT'),
'password' => env('REDIS.PASSWORD'),
]);
}
/** @var PredisClient|Redis */
protected $handler;
/**
* 配置参数
* @var array
*/
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 0,
'persistent' => false,
'prefix' => '',
'tag_prefix' => 'tag:',
'serialize' => [],
];
/**
* 架构函数
* @access public
* @param array $options 缓存参数
*/
public function redisConnect(array $options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if (extension_loaded('redis')) {
$this->handler = new Redis;
if ($this->options['persistent']) {
$this->handler->pconnect($this->options['host'], (int)$this->options['port'], (int)$this->options['timeout'], 'persistent_id_' . $this->options['select']);
} else {
$this->handler->connect($this->options['host'], (int)$this->options['port'], (int)$this->options['timeout']);
}
if ('' != $this->options['password']) {
$this->handler->auth($this->options['password']);
}
}
if (0 != $this->options['select']) {
$this->handler->select($this->options['select']);
}
}
/**
* author Lijy
* date 2020/7/25
*
* @param $key
* @param $value
* @param null $timeout
* @return bool
*/
public function set($key, $value, $timeout = null)
{
return $this->handler->set($key, $value, $timeout);
}
/**
* author Lijy
* date 2020/7/25
*
* @param $key
* @return bool|mixed|string
*/
public function get($key)
{
return $this->handler->get($key);
}
/**
* 判断缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($key): bool
{
return $this->handler->exists($key) ? true : false;
}
/**
* 有序添加
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $score1
* @param $value1
* @return int
*/
public function zAdd($key, $score1, $value1)
{
return $this->handler->zAdd($key, $score1, $value1);
}
/**
* 排序 从小到大
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $start
* @param $end
* @param null $withscores
* @return array
*/
public function zRange($key, $start, $end, $withscores = null)
{
return $this->handler->zRange($key, $start, $end, $withscores);
}
/**
* 排序 从大到小
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $start
* @param $end
* @param $withscore
* @return array
*/
public function zRevRange($key, $start, $end, $withscore)
{
return $this->handler->zRevRange($key, $start, $end, $withscore);
}
/**
* author Lijy
* date 2020/8/9
* 获取值的分值
* @param $key
* @param $member
* @return bool|float
*/
public function zScore($key, $member)
{
return $this->handler->zScore($key, $member);
}
/**
* author Lijy
* date 2020/7/1
* List 删除
* @param $key
* @param $score1
* @param $value1
* @return bool|int
*/
//根据第三个参数(count),删除掉相对的value
//count > 0 : 从表头开始向表尾搜索,移除与value相等的元素,数量为count。
//count < 0 : 从表尾开始向表头搜索,移除与value相等的元素,数量为count的绝对值。
//count = 0 : 移除表中所有与value相等的值。
//返回实际删除元素个数
public function lRem($key, $value, $count)
{
return $this->handler->lrem($key, $value, $count);
}
/**
* 删除
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $member
* @return int
*/
public function zRem($key, $member)
{
return $this->handler->zRem($key, $member);
}
/**
* 查询有序集合长度
* author Lijy
* date 2020/6/17
*
* @param $key
* @return int
*/
public function zCard($key)
{
return $this->handler->zCard($key);
}
/**
* 增加
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $value
* @param $member
* @return float
*/
public function zIncrBy($key, $value, $member)
{
return $this->handler->zIncrBy($key, $value, $member);
}
/**
* 队列-左进去
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $value
* @return bool|int
*/
public function lPush($key, $value)
{
return $this->handler->lPush($key, $value);
}
/**
* 队列-输出
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $start
* @param $end
* @return array
*/
public function lRange($key, $start, $end)
{
return $this->handler->lRange($key, $start, $end);
}
/**
* author Lijy
* date 2020/7/2
*
* 右推进
* @param $key
* @param $value
* @return bool|int
*/
public function rPush($key, $value)
{
return $this->handler->rPush($key, $value);
}
/**
* 队列-右边出队列
* author Lijy
* date 2020/6/17
*
* @param $key
* @return bool|mixed
*/
public function rPop($key)
{
return $this->handler->rPop($key);
}
/**
* 队列-长度
* author Lijy
* date 2020/6/17
*
* @param $key
* @return bool|int
*/
public function lLen($key)
{
return $this->handler->lLen($key);
}
/**
* string-增长
* author Lijy
* date 2020/6/17
*
* @param $key
* @param int $increment
* @return float
*/
public function incrByFloat($key, $increment = 0)
{
return $this->handler->incrByFloat($key, $increment);
}
/**
* 开启事务
* author Lijy
* date 2020/6/17
*
* @param int $mode
* @return Redis
*/
public function multi($mode = 1)
{
return $this->handler->multi($mode);
}
/**
* 执行事务
* author Lijy
* date 2020/6/17
*
* @return array|void
*/
public function exec()
{
return $this->handler->exec();
}
/**
* 丢弃事务
* author Lijy
* date 2020/6/17
*
*/
public function discard()
{
return $this->handler->discard();
}
/**
* 获取hash
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $hashKey
* @return string
*/
public function hGet($key, $hashKey)
{
return $this->handler->hGet($key, $hashKey);
}
/**
* 获取hash
* author Lijy
* date 2020/6/17
*
* @param $key
* @return array
*/
public function hGetAll($key)
{
return $this->handler->hGetAll($key);
}
/**
* hash 增长
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $field
* @param $increment
* @return float
*/
public function hIncrByFloat($key, $field, $increment)
{
return $this->handler->hIncrByFloat($key, $field, $increment);
}
/**
* hash 增长
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $field
* @param $value
* @return int
*/
public function hIncrBy($key, $field, $value)
{
return $this->handler->hIncrBy($key, $field, $value);
}
/**
* hash 设置
* author Lijy
* date 2020/6/17
*
* @param $key
* @param $hashKey
* @param $value
* @return bool|int
*/
public function hSet($key, $hashKey, $value)
{
return $this->handler->hSet($key, $hashKey, $value);
}
/**
* 是否存在该keys
* author Lijy
* date 2020/6/17
*
* @param $key
* @return bool|int
*/
public function exists($key)
{
return $this->handler->exists($key);
}
/**
* 设置Key过期时间
* author Lijy
* date 2020/6/27
*
* @param $key
* @param $expireTime
* @return bool
*/
public function expire($key, $expireTime)
{
return $this->handler->expire($key, $expireTime);
}
}
<?php
namespace appcommonfacade;
use thinkFacade;
/**
* sorted zset
* @method int zAdd($key, $score1, $value1) static 有序添加
* @method array zRange($key, $start, $end, $withscores = null) static 排序 从小到大
* @method array zRevRange($key, $start, $end, $withscore) static 排序 从大到小
* @method int zRem($key, $member) static 删除
* @method int zCard($key) static 查询有序集合长度
* @method string zIncrBy($key, $value, $member) static 增长某个 值
* @method string zScore($key, $member) static 获取值的分值
*
* string
* @method int set($key, $value, $timeout = null) static string-设置
* @method mixed get($name, $default = false) static string-获取
* @method boolean has($name) static string-是否存在
* @method int rm($name) static string-删除
* @method mixed incrByFloat($key, $increment = 0) static string-float累加
*
* list
* @method int lPush($name, $value, $expire = null) static 队列-左推进
* @method int rPush($name, $value, $expire = null) static 队列-右推进
* @method array lRange($key, $start, $end) static 队列-左输出
* @method int rPop($key) static 队列-右出
* @method int lLen($key) static 队列-长度
* @method int lRem($key, $value, $count) static 队列-删除元素
*
* hash
* @method string hSet($key, $hashKey, $value) static 设置hash
* @method string hGet($key, $hashKey) static 获取hash
* @method array hGetAll($key) static 获取hash
* @method string hIncrByFloat($key, $field, $increment) static 增长
* @method string hIncrBy($key, $field, $value) static 增长
*
* keys
* @method int exists($key) static 判断是否存在该key
*
* transaction
* @method string multi($mode = 1) static 事务开始
* @method string exec() static 执行事务
* @method string discard() static 丢弃事务队列
*
* expire
* @method boolean expire($key, $expireTime) static 设置过期时间
*/
class Redis extends Facade
{
/**
* author Lijy
* date 2020/7/25
*
* @return string
*/
protected static function getFacadeClass()
{
return 'appcommonlogicRedisLogic';
}
}