| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- /**
- * 缓存服务
- * @author wesmiler
- */
- namespace app\weixin\service;
- use \think\cache\driver\Redis as BaseRedis;
- class PRedis
- {
- public static $redis = null;
- public static function instance(){
- if(self::$redis){
- return self::$redis;
- }
- $option = config('config.redis');
- $redis = new BaseRedis($option);
- return $redis;
- }
- /**
- * 设置
- * @param $key
- * @param $val
- * @param int $expire
- */
- public static function set($key, $val, $expire=0){
- $expire = $expire? $expire : 0;
- $val = is_array($val)? json_encode($val, 256) : $val;
- self::instance()->set($key, $val, $expire);
- }
- /**
- * 获取
- * @param $key
- * @return bool
- */
- public static function get($key){
- $cacheData = self::instance()->get($key);
- $data = $cacheData? json_decode($cacheData, true) : [];
- if(empty($data)){
- $data = $cacheData;
- }
- return $data;
- }
- /**
- * 删除
- * @param $key
- * @return bool
- */
- public static function del($key){
- return self::instance()->rm($key);
- }
- /**
- * 递增
- * @param $key
- * @return bool
- */
- public static function inc($key, $step=1){
- return self::instance()->inc($key, $step);
- }
- /**
- * 递减
- * @param $key
- * @return bool
- */
- public static function dec($key, $step=1){
- return self::instance()->dec($key, $step);
- }
- /**
- * 批量删除
- * @param $key
- * @return bool
- */
- public static function delByKeys($key){
- $keys = self::instance()->keys($key);
- if(empty($keys)){
- return false;
- }
- foreach($keys as $key){
- self::instance()->rm($key);
- }
- }
- /**
- * 右入队处理
- * @param $key
- * @param $data
- */
- public static function rpush($key, $data){
- return self::instance()->rpush($key, $data);
- }
- /**
- * 左入出处理
- * @param $key
- */
- public static function lpop($key){
- return self::instance()->lpop($key);
- }
- /**
- * 设置有效时间
- * @param $key
- * @param $expire
- * @return mixed
- */
- public static function expire($key, $expire){
- return self::instance()->expire($key, $expire);
- }
- /**
- * 键名是否存在
- * @param $key
- * @param $expire
- * @return mixed
- */
- public static function exists($key){
- return self::instance()->exists($key);
- }
- }
|