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\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()
// }
}