watch($key); if ($ApiRedis::$redis->exists($key)) { return 1; } else { return 0; } } /** * 开启事务添加key * @param $key * @param $val * @param $time * @return bool */ public function addRedis($key, $val, $time) { $ApiRedis = new RedisController(); $ApiRedis::$redis->multi(); //开启事务 $value = is_array($val) ? json_encode($val) : $val; $state = $ApiRedis::$redis->setex($key, $time, $value); return $state; } /** * 清除缓存并且提交事务 * @param $key 键名 */ public function deleteRedis($key) { if (C('IS_SET_REDIS') && !empty($key)) { $ApiRedis = new RedisController(); $ApiRedis::$redis->delete($key); $ApiRedis::$redis->exec(); } } /** * 提交事务 */ public function submitAffair() { $ApiRedis = new RedisController(); $ApiRedis::$redis->exec(); } /** * 开启事务 */ public function startAffair() { $ApiRedis = new RedisController(); $ApiRedis::$redis->multi(); } /** * 检测key的锁 * @param $key */ public function testingKey($key) { $ApiRedis = new RedisController(); $ApiRedis::$redis->watch($key); } /** * 查询某key是否存在 若 key 存在返回 1 ,否则返回 0 * @param $key * @return bool */ public function isExists($key) { if (C('IS_SET_REDIS') && !empty($key)) { $ApiRedis = new RedisController(); if ($ApiRedis::$redis->exists($key)) { return 1; } else { return 0; } } else { return 0; } } /** * 一次获取多个key的值:返回一个键值对数组,其中不存在的key值为false * @param $key * @return array */ public function getManyCache($key) { if (C('IS_SET_REDIS') && !empty($key)) { $ApiRedis = new RedisController(); $result = $ApiRedis::$redis->mget($ApiRedis::$redis->keys($key)); } else { $result = []; } return $result; } /** * /创建一个具有时间限制的键值,过期则删除,秒为单位,成功返回true * @param $key * @param $time * @param $arr * @return bool */ public function addTimeCache($key, $time, $value) { if (C('IS_SET_REDIS') && !empty($key) && !empty($time) && !empty($value)) { $value = is_array($value) ? json_encode($value) : $value; $ApiRedis = new RedisController(); $state = $ApiRedis::$redis->setex($key, $time, $value); } else { $state = false; } return $state; } /** * 获取键值:成功返回String类型键值,若key不存在或不是String类型则返回false * @param $key * @return bool|mixed|string */ public function getSingleCache($key) { if (C('IS_SET_REDIS') && !empty($key)) { $ApiRedis = new RedisController(); $str = $ApiRedis::$redis->get($key); return $str; } else { return ''; } } /** * 设置键值:成功返回true,否则返回false,键值不存在则新建,否则覆盖 * @param $key * @param $str * @return bool */ public function addCache($key, $str) { if (C('IS_SET_REDIS') && !empty($key) && !empty($str)) { $ApiRedis = new RedisController(); $state = $ApiRedis::$redis->set($key, $str); } else { $state = false; } return $state; } /** * 清除缓存 * @param $key 键名 */ public function eliminateCache($key) { if (C('IS_SET_REDIS') && !empty($key)) { $ApiRedis = new RedisController(); $ApiRedis::$redis->delete($key); } } }