@value 静态变量_封装redis类并实现门面静态调用

fe2f92781e084ab53735c2cdf67a9f17.png

封装redis类并实现门面静态调用

1.redis动态类

RedisLogic.php
<?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);
    }


}

2.封装门面(Facade)调用

静态方式调用动态方法 Redis.php
<?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';
    }
}
为什么注释用@method,请点击Thinkphp用门面调用在phpstorm中不支持跳转

3.调用示例

<?php

namespace applivecontroller;

use appcommonfacadeRedis;
use thinkRequest;

/**
 * author Lijy
 * date 2020/7/11
 * Class test 测试控制器
 * @package applivecontroller
 */
class Test
{
    public function test(Request $request)
    {
        $key = 'test';
        Redis::set($key,111); //redis静态调用
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值