212 行
5.2 KiB
PHP
可执行文件
212 行
5.2 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Common\Business;
|
||
|
||
use Addons\Api\RedisController;
|
||
|
||
//[1.7.14]
|
||
|
||
/**
|
||
* 缓存类
|
||
* Class Cache
|
||
* [
|
||
* 清除缓存并且提交事务 deleteRedis
|
||
* 开启事务添加key addRedis
|
||
* 开启检测查询key 与事务一起使用 deposit
|
||
* 开启事务 startAffair
|
||
* 提交事务 submitAffair
|
||
* 检测key的锁 testingKey
|
||
* 查询某key是否存在 isExists
|
||
* 获取多个key的值 getManyCache
|
||
* 获取单个key的值 getSingleCache
|
||
* 创建一个具有时间限制的键值 addTimeCache
|
||
* 创建一个键值addCache
|
||
* 清除缓存 eliminateCache
|
||
*
|
||
* 添加无时间限制的字符串缓存 addCache
|
||
* 清除缓存 eliminateCache
|
||
* ]
|
||
* @package Common\Business
|
||
*/
|
||
class CacheHandle
|
||
{
|
||
/**
|
||
* 开启检测查询key 与事务一起使用
|
||
* @param $key
|
||
* @return int 1存在 0不存在
|
||
*/
|
||
public function deposit($key)
|
||
{
|
||
$ApiRedis = new RedisController();
|
||
$ApiRedis::$redis->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);
|
||
}
|
||
}
|
||
} |