Files
hyperf_service/app/Cache/Redis/RedisCache.php
2025-04-09 09:40:05 +08:00

611 lines
19 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Cache\Redis;
use App\Constants\RedisCode;
use Hyperf\Context\ApplicationContext;
use Hyperf\Redis\RedisFactory;
use Hyperf\Redis\RedisProxy;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Redis;
use RedisException;
class RedisCache
{
/**
* 获取 redis 对象
* @param string $poolName
* @return RedisProxy
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getRedis(string $poolName = RedisCode::DEFAULT_DB): RedisProxy
{
return ApplicationContext::getContainer()->get(RedisFactory::class)->get($poolName);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | atom 原子操作
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* @param string $script
* @param array $array
* @param int $num
* @param string $poolName
* @return mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function eval(string $script, array $array, int $num = 1, string $poolName = RedisCode::DEFAULT_DB): mixed
{
return $this->getRedis($poolName)->eval($script, $array, $num);
}
/**
* @param string $key
* @param int $ttl
* @param string $poolName
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function addLock(string $key, int $ttl = 5, string $poolName = RedisCode::LOCK_DB): int
{
$script = <<<lua
local isLock = redis.call('exists',KEYS[1])
if isLock == 1 then
return 0
else
redis.call('set',KEYS[1],1)
redis.call('Expire',KEYS[1],ARGV[1])
return 1;
end
lua;
return $this->getRedis($poolName)->eval($script, [$key, $ttl], 1);
}
/**
* @param string $key
* @param string $poolName
* @return int|bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|RedisException
*/
public function delLock(string $key, string $poolName = RedisCode::LOCK_DB): int|bool
{
return $this->delete($key, $poolName);
}
/**
* 接下来的操作启用原子操作 - 事物开启
* @param string $poolName
* @return bool|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function multi(string $poolName = RedisCode::DEFAULT_DB): bool|Redis
{
return $this->getRedis($poolName)->multi();
}
/**
* 结束原子操作 - 事物结束
* @param string $poolName
* @return Redis|array|false
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function exec(string$poolName = RedisCode::DEFAULT_DB): Redis|array|false
{
return $this->getRedis($poolName)->exec();
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | key
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* 判断 key 是否存在
* @param string $key
* @param string $poolName
* @return int|bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function exists(string $key, string $poolName = RedisCode::DEFAULT_DB): int|bool
{
return $this->getRedis($poolName)->exists($key);
}
/**
* 删除
* @param string $key
* @param string $poolName
* @return int|bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function delete(string $key, string $poolName = RedisCode::DEFAULT_DB): int|bool
{
return $this->getRedis($poolName)->del($key);
}
/**
* @param string $key
* @param int $ex
* @param string $poolName
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function expire(string $key, int $ex, string $poolName = RedisCode::DEFAULT_DB): bool
{
return $this->getRedis($poolName)->expire($key, $ex);
}
/**
* 返回过期时间 -1=key存在未设置过期时间 -2=key不存在 >0 = 过期时间(秒)
* @param string $key
* @param string $poolName
* @return bool|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function ttl(string $key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->ttl($key);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | string
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* 设置一个key
* @param string $key
* @param string $value
* @param string $poolName
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function set(string $key, string $value, string $poolName = RedisCode::DEFAULT_DB): bool
{
return $this->getRedis($poolName)->set($key, $value);
}
/**
* 获取key
* @param string $key
* @param string $poolName
* @return false|mixed|Redis|string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function get(string $key, string $poolName = RedisCode::DEFAULT_DB): mixed
{
return $this->getRedis($poolName)->get($key);
}
/**
* 添加一个有过期值的key ps:如果 key 已经存在, setEx 命令将会替换旧的值。)
* @param string $key
* @param string $value
* @param int $ttl
* @param string $poolName
* @return Redis|bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function setEx(string $key, string $value, int $ttl, string $poolName = RedisCode::DEFAULT_DB): Redis|bool
{
return $this->getRedis($poolName)->setex($key, $ttl, $value);
}
/**
* 将 key 中储存的数字值增一。
* 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。
* @param $key
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function incr($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->incr($key);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | set
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* 判断 value 元素是否是集合 key 的成员
* 如果 member 元素是集合的成员,返回 1 。
* 如果 member 元素不是集合的成员,或 key 不存在,返回 0 。
* @param $key
* @param $value
* @param string $poolName
* @return bool|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function sIsMember($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sIsMember($key, $value);
}
/**
* 添加集合成员
* @param $key
* @param $value
* @param string $poolName
* @return bool|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function sAdd($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sAdd($key, $value);
}
/**
* 获取集合所有内容
* @param $key
* @param string $poolName
* @return array|false|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function sMembers($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sMembers($key);
}
/**
* 删除集合成员
* @param $key
* @param $value
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function sRem($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sRem($key, $value);
}
/**
* 集合批量添加
* @param $key
* @param array $values
* @param string $poolName
* @return bool|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function sAddBatch($key, array $values, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sAddArray($key, $values);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | hash
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* 设置多个key-value 的 hash值
* @param $key
* @param $hashKeys
* @param string $poolName
* @return bool|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function hMset($key, $hashKeys, string $poolName = RedisCode::DEFAULT_DB)
{
$result = $this->getRedis($poolName)->hMset($key, $hashKeys);
return $result;
}
/**
* 返回列表 key 的集合数据
* @param $key
* @param string $poolName
* @return false|array|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hGetAll($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hGetAll($key);
}
/**
* 设置单个key-value 的 hash值
* @param $key
* @param $hashKey
* @param $hashValue
* @param string $poolName
* @return bool|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function hSet($key, $hashKey, $hashValue, string $poolName = RedisCode::DEFAULT_DB)
{
$result = $this->getRedis($poolName)->hSet($key, $hashKey, $hashValue);
return $result;
}
/**
* 获取单个字段的 hash 值
* @param $key
* @param $hashKey
* @param string $poolName
* @return false|mixed|Redis|string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hGet($key, $hashKey, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hGet($key, $hashKey);
}
/**
* 删除hash某个值
* @param $key
* @param $hashKey
* @param string $poolName
* @return bool|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hDel($key, $hashKey, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hDel($key, $hashKey);
}
/**
* 查找hash某个值
* @param $key
* @param $hashKey
* @param string $poolName
* @return bool|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hExists($key, $hashKey, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hExists($key, $hashKey);
}
/**
* 单个字段的 hash 值原子增加
* @param string $key
* @param string $hashKey
* @param int $hashValue
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function hIncrBy(string $key, string $hashKey, int $hashValue, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->hincrby($key, $hashKey, $hashValue);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | list
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* 从左边入
* @param string $key
* @param string $data
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function lPush(string $key, string $data, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->lPush($key, $data);
}
/**
* 从右边出
* @param $key
* @param string $poolName
* @return array|bool|mixed|Redis|string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function rPop($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->rPop($key);
}
/**
* 批量加入数据
* @param $data
* @param string $poolName
* @return RedisProxy
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function lPushBatch($data, string $poolName = RedisCode::DEFAULT_DB)
{
$result = $this->getRedis($poolName);
call_user_func_array([$result, 'lPush'], $data);
return $result;
}
/**
* 返回列表 key 的长度
* @param $key
* @param string $poolName
* @return bool|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function lLen($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->lLen($key);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | sorted set
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* 返回有序集 key 中,成员 member 的 score 值。
* 如果 member 元素不是有序集 key 的成员,或 key 不存在,返回 nil 。
* @param $key
* @param $value
* @param string $poolName
* @return bool|float|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function zScore($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->zScore($key, $value);
}
/**
* 加入有序集合
* @param $key
* @param $score
* @param $value
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function zAdd($key, $score, $value, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->zAdd($key, $score, $value);
}
/**
* @param $key
* @param $value
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function zRem($key, $value, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->zRem($key, $value);
}
/**
* @param $key
* @param $score
* @param $value
* @param string $poolName
* @return Redis|float|false
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function zIncrBy($key,$score, $value, string $poolName = RedisCode::DEFAULT_DB): Redis|float|false
{
return $this->getRedis($poolName)->zIncrBy($key, $score, $value);
}
/**
* @param $key
* @param $value
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function zRank($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->zRank($key, $value);
}
/**
* @param $key
* @param int $start
* @param int $end
* @param bool $score
* @param string $poolName
* @return array|false|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function zRevRange($key, int $start = 0, int $end = -1, $score = false, string $poolName = RedisCode::DEFAULT_DB): false|array|Redis
{
return $this->getRedis($poolName)->zRevRange($key, $start, $end,$score);
}
// +--------------------------------------------------------------------------------------------------------------------------------------------
// | geo
// +--------------------------------------------------------------------------------------------------------------------------------------------
/**
* @param $key
* @param $lng
* @param $lat
* @param $value
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function geoAdd($key, $lng, $lat, $value, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->geoAdd($key, $lng, $lat, $value);
}
/**
* @param $key
* @param $value1
* @param $value2
* @param string $unit [m|km|ft|mi]
* @param string $poolName
* @return false|int|Redis
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function geoDist($key, $value1, $value2, string $unit = 'km', string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->geoDist($key, $value1, $value2, $unit);
}
}