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

@@ -3,6 +3,7 @@
namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Model\AdminMenu;
use App\Service\ServiceTrait\Admin\AdminRoleMenuTrait;
use Hyperf\Di\Annotation\Inject;
@@ -48,15 +49,15 @@ class MenuCache
*/
public function getMenu(): array
{
if ($this->redis->exists($this->menuKey,'system')) {
return json_decode($this->redis->get($this->menuKey,'system'),true);
if ($this->redis->exists($this->menuKey,RedisCode::SYSTEM_DB)) {
return json_decode($this->redis->get($this->menuKey,RedisCode::SYSTEM_DB),true);
}
$allMenuList = $this->adminMenuModel->getAllMenu();
$data = $this->getDbMenu($allMenuList);
$this->redis->set($this->menuKey,json_encode($data),'system');
$this->redis->set($this->menuKey,json_encode($data),RedisCode::SYSTEM_DB);
return $data;
}
@@ -69,6 +70,6 @@ class MenuCache
*/
public function delMenu(): void
{
$this->redis->delete($this->menuKey,'system');
$this->redis->delete($this->menuKey,RedisCode::SYSTEM_DB);
}
}

View File

@@ -5,6 +5,7 @@ namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Constants\Admin\AuthCode;
use App\Constants\Common\RoleCode;
use App\Constants\RedisCode;
use App\Model\AdminMenu;
use App\Model\AdminRoleMenu;
use App\Service\ServiceTrait\Admin\AdminRoleMenuTrait;
@@ -75,10 +76,10 @@ class RoleCache
$this->getKey();
if ($this->redis->exists($this->roleMenuArrKey,'system') && $this->redis->exists($this->roleMenuListKey,'system')) {
if ($this->redis->exists($this->roleMenuArrKey,RedisCode::SYSTEM_DB) && $this->redis->exists($this->roleMenuListKey,RedisCode::SYSTEM_DB)) {
return [
'role_arr' => $this->redis->sMembers($this->roleMenuArrKey,'system'),
'role_list' => json_decode($this->redis->get($this->roleMenuListKey,'system'),true),
'role_arr' => $this->redis->sMembers($this->roleMenuArrKey,RedisCode::SYSTEM_DB),
'role_list' => json_decode($this->redis->get($this->roleMenuListKey,RedisCode::SYSTEM_DB),true),
];
}
@@ -101,8 +102,8 @@ class RoleCache
$this->delRoleCache();
$this->redis->set($this->roleMenuListKey,json_encode($data),'system');
$this->redis->sAddBatch($this->roleMenuArrKey,$menuIds,'system');
$this->redis->set($this->roleMenuListKey,json_encode($data),RedisCode::SYSTEM_DB);
$this->redis->sAddBatch($this->roleMenuArrKey,$menuIds,RedisCode::SYSTEM_DB);
return [
'role_arr' => $menuIds,
@@ -124,7 +125,7 @@ class RoleCache
$this->getKey();
}
$this->redis->delete($this->roleMenuArrKey,'system');
$this->redis->delete($this->roleMenuListKey,'system');
$this->redis->delete($this->roleMenuArrKey,RedisCode::SYSTEM_DB);
$this->redis->delete($this->roleMenuListKey,RedisCode::SYSTEM_DB);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Model\AdminSection;
use App\Service\ServiceTrait\Admin\SectionTrait;
use Hyperf\Di\Annotation\Inject;
@@ -49,15 +50,15 @@ class SectionCache
*/
public function getList(): array
{
if ($this->redis->exists($this->key,'system')) {
return json_decode($this->redis->get($this->key,'system'),true);
if ($this->redis->exists($this->key,RedisCode::SYSTEM_DB)) {
return json_decode($this->redis->get($this->key,RedisCode::SYSTEM_DB),true);
}
$allList = $this->adminSectionModel->getList();
$data = $this->getDbList($allList);
$this->redis->set($this->key,json_encode($data),'system');
$this->redis->set($this->key,json_encode($data),RedisCode::SYSTEM_DB);
return $data;
}
@@ -70,6 +71,6 @@ class SectionCache
*/
public function delList()
{
$this->redis->delete($this->key,'system');
$this->redis->delete($this->key,RedisCode::SYSTEM_DB);
}
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Cache\Redis\Admin;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -26,7 +27,7 @@ class UserCache
*/
public function getAdminToken($userId): false|string
{
return $this->redis->get(AdminRedisKey::adminUserToken($userId),'system') ?? false;
return $this->redis->get(AdminRedisKey::adminUserToken($userId),RedisCode::SYSTEM_DB) ?? false;
}
/**
@@ -41,7 +42,7 @@ class UserCache
public function setAdminToken($userId, string $token, int $ttl): bool
{
$key = AdminRedisKey::adminUserToken($userId);
$this->redis->delete($key,'system');
return $this->redis->setEx($key, $token, $ttl, 'system');
$this->redis->delete($key,RedisCode::SYSTEM_DB);
return $this->redis->setEx($key, $token, $ttl, RedisCode::SYSTEM_DB);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Cache\Redis\Common;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Model\SystemCityConfig;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
@@ -41,7 +42,7 @@ class CityCache
$key = CommonRedisKey::getSystemRegionList($type);
if ($this->redis->exists($key)) return json_decode($this->redis->get($key), true);
if ($this->redis->exists($key,RedisCode::SYSTEM_DB)) return json_decode($this->redis->get($key,RedisCode::SYSTEM_DB), true);
if ($type == 'all') {
$data = $this->systemCityConfigModel->getAll();
@@ -49,7 +50,7 @@ class CityCache
$data = $this->systemCityConfigModel->getListByDeep($deep);
}
$this->redis->set($key, json_encode($data));
$this->redis->set($key, json_encode($data),RedisCode::SYSTEM_DB);
return $data;
}

View File

@@ -9,11 +9,30 @@ class CommonRedisKey
* @param $pid
* @return string
*/
public static function getSystemConfigList($pid): string
public static function getSystemConfigListByPid($pid): string
{
return '__system:config:list:' . $pid;
}
/**
* 获取配置项所有
* @return string
*/
public static function getSystemConfigList(): string
{
return '__system:config:list';
}
/**
* 获取配置项缓存key
* @param $name
* @return string
*/
public static function getConfigKey($name): string
{
return '__system:config:' . $name;
}
/**
* 获取系统配置
* @return string

View File

@@ -3,12 +3,18 @@
namespace App\Cache\Redis\Common;
use App\Cache\Redis\RedisCache;
use App\Constants\ConfigCode;
use App\Constants\RedisCode;
use App\Model\Config;
use App\Model\Cycle;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Tappable\HigherOrderTapProxy;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ConfigCache
{
/**
* @var RedisCache
*/
@@ -21,11 +27,78 @@ class ConfigCache
#[Inject]
protected Config $configModel;
public function setConfigCacheByPid($pid)
/**
* @param $pid
* @return array|mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function setConfigCacheByPid($pid): mixed
{
$key = CommonRedisKey::getSystemConfigList($pid);
if ($this->redis->exists($key,'system')) return json_decode($this->redis->get($key),true);
$key = CommonRedisKey::getSystemConfigListByPid($pid);
if ($this->redis->exists($key,RedisCode::SYSTEM_DB)) return json_decode($this->redis->get($key,RedisCode::SYSTEM_DB),true);
return [];
$data = $this->configModel->where('pid', $pid)->pluck('value','key')->toArray();
if (empty($data)) return [];
$this->redis->set($key,json_encode($data),RedisCode::SYSTEM_DB);
return $data;
}
/**
* @param $key
* @return mixed|string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getConfigValue($key): mixed
{
$arr = $this->setConfigCache();
return $arr[$key] ?? '';
}
/**
* @param $key
* @param int $isRedis
* @param int $expire
* @return false|HigherOrderTapProxy|mixed|\Redis|string|null
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getConfigValueByKey($key, int $isRedis = 0,int $expire = 600): mixed
{
$redisKey = CommonRedisKey::getConfigKey($key);
if (!$this->redis->exists($redisKey,RedisCode::SYSTEM_DB)) {
$value = $this->configModel->where('key',$key)->value('value');
if ($isRedis == 1) {
$this->redis->setEx($redisKey, $value, $expire,RedisCode::SYSTEM_DB);
}
return $value;
}
return $this->redis->get($key,RedisCode::SYSTEM_DB) ?? ConfigCode::DEFAULT_VALUE[$key];
}
/**
* @return array|mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function setConfigCache(): mixed
{
$key = CommonRedisKey::getSystemConfigList();
if ($this->redis->exists($key,RedisCode::SYSTEM_DB)) return json_decode($this->redis->get($key,RedisCode::SYSTEM_DB),true);
$data = $this->configModel->pluck('value','key')->toArray();
if (empty($data)) return [];
$this->redis->set($key,json_encode($data),RedisCode::SYSTEM_DB);
return $data;
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Cache\Redis\Common;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Model\Cycle;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
@@ -10,6 +11,7 @@ use Psr\Container\NotFoundExceptionInterface;
class CycleCache
{
/**
* @var RedisCache
*/
@@ -31,7 +33,7 @@ class CycleCache
{
$key = CommonRedisKey::getCycleList();
if ($this->redis->exists($key,'system')) return;
if ($this->redis->exists($key,RedisCode::SYSTEM_DB)) return;
$allData = $this->cycleModel->get();
@@ -40,7 +42,12 @@ class CycleCache
$allData = $allData->toArray();
foreach ($allData as $item) {
$this->redis->zAdd($key, $item['id'],$item['dates'],'system');
$this->redis->zAdd($key, $item['id'],$item['dates'],RedisCode::SYSTEM_DB);
}
}
// public function getCycleCache(string $key)
// {
// $this->redis->zScore()
// }
}

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