123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * REDIS缓存模块服务层
- * Class RedisService
- */
- namespace App\Services;
- use Illuminate\Support\Facades\Redis as BaseRedis;
- class RedisService extends BaseRedis
- {
- /**
- * 设置缓存
- * @param $key 键名
- * @param $data 数据:数组和字符串,数组自动转化JSON
- * @param int $expire 有效期:0为永久
- * @return mixed
- */
- public static function set($key, $data, $expire =null)
- {
- $expire = $expire !=null ? $expire : env('REDIS_EXPIRE', 24 * 3600);
- $data = is_array($data) && !empty($data)? json_encode($data, 256) : $data;
- $data = !empty($data)? $data : null;
- if ($expire > 0) {
- return BaseRedis::setex($key, $expire, $data);
- } else {
- return BaseRedis::set($key, $data);
- }
- }
- /**
- * 获取缓存数据
- * @param $key 键名
- * @return array|mixed
- */
- public static function get($key)
- {
- $data = BaseRedis::get($key);
- $jsonData = $data ? json_decode($data, true) : [];
- return $jsonData ? $jsonData : $data;
- }
- /**
- * 键名是否存在
- * @param $key
- * @param $expire
- * @return mixed
- */
- public static function exists($key){
- return BaseRedis::exists($key);
- }
- /**
- * 递增
- * @param $key
- * @return bool
- */
- public static function incr($key, $step=1){
- return BaseRedis::incrby($key, $step);
- }
- /**
- * 递减
- * @param $key
- * @return bool
- */
- public static function decr($key, $step=1){
- return BaseRedis::decrby($key, $step);
- }
- /**
- * 进队列缓存
- * @param $key 键名
- * @param $data 队列数据
- * @param bool $clear 是否清除之前数据
- * @return mixed
- */
- public static function rPush($key, $data, $clear = false)
- {
- if ($clear) {
- BaseRedis::del($key);
- }
- $data = is_array($data) ? json_encode($data, 256) : $data;
- return BaseRedis::rPush($key, $data);
- }
- /**
- * 出队列
- * @param $key 键名
- * @return array|mixed
- */
- public static function lPop($key)
- {
- $data = BaseRedis::lPop($key);
- $jsonData = $data ? json_decode($data, true) : [];
- return $jsonData ? $jsonData : $data;
- }
- /**
- * 清除缓存
- * @param $key 键名
- * @return mixed
- */
- public static function clear($key)
- {
- return BaseRedis::del($key);
- }
- /**
- * 按键名匹配删除
- * @param $key
- * @return bool
- */
- public static function keyDel($key){
- $keys = BaseRedis::keys($key);
- foreach($keys as $key){
- BaseRedis::del($key);
- }
- return true;
- }
- /**
- * 有效期设置
- * @param $key
- * @param $expire
- * @return mixed
- */
- public static function expire($key, $expire){
- return BaseRedis::expire($key, $expire);
- }
- /**
- * 原子锁
- * @param $key
- * @param $value
- * @param int $expire
- * @return mixed
- */
- public static function setnx($key, $value, $expire=0){
- $res = BaseRedis::setnx($key, $value);
- if($expire){
- BaseRedis::expire($key, $expire);
- }
- return $res;
- }
- }
|