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; } }