feat: redis

This commit is contained in:
2025-01-21 16:20:16 +08:00
parent df78fc705d
commit 2beb8d9e55
32 changed files with 310 additions and 84 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Cache\Redis;
use App\Constants\RedisCode;
use Hyperf\Context\ApplicationContext;
use Hyperf\Redis\RedisFactory;
use Hyperf\Redis\RedisProxy;
@@ -21,7 +22,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getRedis(string $poolName = 'default'): RedisProxy
private function getRedis(string $poolName = RedisCode::DEFAULT_DB): RedisProxy
{
return ApplicationContext::getContainer()->get(RedisFactory::class)->get($poolName);
}
@@ -40,7 +41,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function eval(string $script, array $array, int $num = 1, string $poolName = 'default'): mixed
public function eval(string $script, array $array, int $num = 1, string $poolName = RedisCode::DEFAULT_DB): mixed
{
return $this->getRedis($poolName)->eval($script, $array, $num);
}
@@ -55,7 +56,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function addLock(string $key, int $ttl = 5, string $poolName = 'lock'): int
public function addLock(string $key, int $ttl = 5, string $poolName = RedisCode::LOCK_DB): int
{
$script = <<<lua
local isLock = redis.call('exists',KEYS[1])
@@ -78,7 +79,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|RedisException
*/
public function delLock(string $key, string $poolName = 'lock'): int|bool
public function delLock(string $key, string $poolName = RedisCode::LOCK_DB): int|bool
{
return $this->delete($key, $poolName);
}
@@ -90,7 +91,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function multi(string $poolName = 'default'): bool|Redis
public function multi(string $poolName = RedisCode::DEFAULT_DB): bool|Redis
{
return $this->getRedis($poolName)->multi();
}
@@ -102,7 +103,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function exec(string$poolName = 'default'): Redis|array|false
public function exec(string$poolName = RedisCode::DEFAULT_DB): Redis|array|false
{
return $this->getRedis($poolName)->exec();
}
@@ -119,7 +120,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function exists(string $key, string $poolName = 'default'): int|bool
public function exists(string $key, string $poolName = RedisCode::DEFAULT_DB): int|bool
{
return $this->getRedis($poolName)->exists($key);
}
@@ -133,7 +134,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function delete(string $key, string $poolName = 'default'): int|bool
public function delete(string $key, string $poolName = RedisCode::DEFAULT_DB): int|bool
{
return $this->getRedis($poolName)->del($key);
}
@@ -147,7 +148,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function expire(string $key, int $ex, string $poolName = 'default'): bool
public function expire(string $key, int $ex, string $poolName = RedisCode::DEFAULT_DB): bool
{
return $this->getRedis($poolName)->expire($key, $ex);
}
@@ -161,7 +162,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function ttl(string $key, string $poolName = 'default')
public function ttl(string $key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->ttl($key);
}
@@ -180,7 +181,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function set(string $key, string $value, string $poolName = 'default'): bool
public function set(string $key, string $value, string $poolName = RedisCode::DEFAULT_DB): bool
{
return $this->getRedis($poolName)->set($key, $value);
}
@@ -194,7 +195,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function get(string $key, string $poolName = 'default'): mixed
public function get(string $key, string $poolName = RedisCode::DEFAULT_DB): mixed
{
return $this->getRedis($poolName)->get($key);
}
@@ -210,7 +211,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function setEx(string $key, string $value, int $ttl, string $poolName = 'default'): Redis|bool
public function setEx(string $key, string $value, int $ttl, string $poolName = RedisCode::DEFAULT_DB): Redis|bool
{
return $this->getRedis($poolName)->setex($key, $ttl, $value);
@@ -227,7 +228,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function incr($key, string $poolName = 'default')
public function incr($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->incr($key);
}
@@ -247,7 +248,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function sIsMember($key, $value, string $poolName = 'default')
public function sIsMember($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sIsMember($key, $value);
}
@@ -262,7 +263,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function sAdd($key, $value, string $poolName = 'default')
public function sAdd($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sAdd($key, $value);
}
@@ -276,7 +277,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function sMembers($key, string $poolName = 'default')
public function sMembers($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sMembers($key);
}
@@ -290,7 +291,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function sRem($key, $value, string $poolName = 'default')
public function sRem($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sRem($key, $value);
}
@@ -305,7 +306,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function sAddBatch($key, array $values, string $poolName = 'default')
public function sAddBatch($key, array $values, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->sAddArray($key, $values);
}
@@ -324,7 +325,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hMset($key, $hashKeys, $expire = null, string $poolName = 'default')
public function hMset($key, $hashKeys, $expire = null, string $poolName = RedisCode::DEFAULT_DB)
{
$result = $this->getRedis($poolName)->hMset($key, $hashKeys);
if ($expire) {
@@ -342,7 +343,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hGetAll($key, string $poolName = 'default')
public function hGetAll($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hGetAll($key);
}
@@ -359,7 +360,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hSet($key, $hashKey, $hashValue, int $expire = 0, string $poolName = 'default')
public function hSet($key, $hashKey, $hashValue, int $expire = 0, string $poolName = RedisCode::DEFAULT_DB)
{
$result = $this->getRedis($poolName)->hSet($key, $hashKey, $hashValue);
if ($expire) {
@@ -378,7 +379,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hGet($key, $hashKey, string $poolName = 'default')
public function hGet($key, $hashKey, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hGet($key, $hashKey);
}
@@ -393,7 +394,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hDel($key, $hashKey, string $poolName = 'default')
public function hDel($key, $hashKey, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hDel($key, $hashKey);
}
@@ -408,7 +409,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function hExists($key, $hashKey, string $poolName = 'default')
public function hExists($key, $hashKey, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hExists($key, $hashKey);
}
@@ -425,7 +426,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function HIncrBy($key, $hashKey, $hashValue, string $poolName = 'default')
public function HIncrBy($key, $hashKey, $hashValue, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->hincrby($key, $hashKey, $hashValue);
}
@@ -444,7 +445,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function lPush(string $key, string $data, string $poolName = 'default'): false|int|Redis
public function lPush(string $key, string $data, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->lPush($key, $data);
}
@@ -458,7 +459,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function rPop($key, string $poolName = 'default')
public function rPop($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->rPop($key);
}
@@ -471,7 +472,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function lPushBatch($data, string $poolName = 'default')
public function lPushBatch($data, string $poolName = RedisCode::DEFAULT_DB)
{
$result = $this->getRedis($poolName);
call_user_func_array([$result, 'lPush'], $data);
@@ -487,7 +488,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function lLen($key, string $poolName = 'default')
public function lLen($key, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->lLen($key);
}
@@ -506,7 +507,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function zScore($key, $value, string $poolName = 'default')
public function zScore($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->zScore($key, $value);
}
@@ -522,7 +523,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function zAdd($key, $score, $value, string $poolName = 'default'): false|int|Redis
public function zAdd($key, $score, $value, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->zAdd($key, $score, $value);
}
@@ -536,7 +537,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function zRem($key, $value, string $poolName = 'default'): false|int|Redis
public function zRem($key, $value, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->zRem($key, $value);
}
@@ -549,7 +550,7 @@ class RedisCache
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function zRank($key, $value, string $poolName = 'default')
public function zRank($key, $value, string $poolName = RedisCode::DEFAULT_DB)
{
return $this->getRedis($poolName)->zRank($key, $value);
}
@@ -567,7 +568,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function geoAdd($key, $lng, $lat, $value, string $poolName = 'default'): false|int|Redis
public function geoAdd($key, $lng, $lat, $value, string $poolName = RedisCode::DEFAULT_DB): false|int|Redis
{
return $this->getRedis($poolName)->geoAdd($key, $lng, $lat, $value);
}
@@ -583,7 +584,7 @@ class RedisCache
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function geoDist($key, $value1, $value2, string $unit = 'km', string $poolName = 'default'): false|int|Redis
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);
}