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

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Constants;
class ConfigCode
{
const string TODAY_CUT_OFF_TIME_KEY = 'TodayCutOffTime';
const array DEFAULT_VALUE = [
self::TODAY_CUT_OFF_TIME_KEY => '15:00:00'
];
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Constants;
class RedisCode
{
CONST STRING SYSTEM_DB = 'system';
CONST STRING DEFAULT_DB = 'default';
CONST STRING LOCK_DB = 'lock';
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\ChefRequest;
use App\Service\Admin\System\ChefService;
@@ -16,7 +17,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class ChefController
class ChefController extends AbstractController
{
/**
* 厨师列表

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\DriverRequest;
use App\Service\Admin\System\DriverService;
@@ -16,7 +17,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class DriverController
class DriverController extends AbstractController
{
/**
* 司机列表

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\EmployeeRequest;
use App\Service\Admin\User\EmployeeService;
@@ -17,7 +18,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class EmployeeController
class EmployeeController extends AbstractController
{
/**
* @param EmployeeRequest $request

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\categoryRequest;
use App\Request\Admin\GoodRequest;
@@ -21,7 +22,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class GoodController
class GoodController extends AbstractController
{
/**
* spu添加

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\KitchenRequest;
use App\Service\Admin\System\KitchenService;
@@ -16,7 +17,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class KitchenController
class KitchenController extends AbstractController
{
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Request\Admin\MemberRequest;
use App\Service\Admin\User\MemberService;
@@ -16,7 +17,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class MemberController
class MemberController extends AbstractController
{
/**
* @param MemberRequest $request

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\SectionRequest;
use App\Service\Admin\User\SectionService;
@@ -19,7 +20,7 @@ use RedisException;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class SectionController
class SectionController extends AbstractController
{
/**
* @param SectionRequest $request

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\SiteRequest;
use App\Service\Admin\System\SiteService;
@@ -16,7 +17,7 @@ use Hyperf\Validation\Annotation\Scene;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class SiteController
class SiteController extends AbstractController
{
#[RequestMapping(path: "add", methods: "POST")]
#[Scene(scene: "add")]

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Service\Api\Good\MealListService;
use App\Service\Api\Good\OptionalListService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'api/good')]
class GoodController extends AbstractController
{
#[RequestMapping(path: 'optional',methods: 'post')]
public function optional()
{
return (new OptionalListService)->handle();
}
#[RequestMapping(path: 'meal',methods: 'post')]
public function meal()
{
return (new MealListService)->handle();
}
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Request\Api\LoginRequest;
use App\Service\Api\Login\LoginService;
use Hyperf\HttpServer\Annotation\Controller;
@@ -11,7 +12,7 @@ use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'api/login')]
class LoginController
class LoginController extends AbstractController
{
#[RequestMapping(path: 'index',methods: 'post')]
#[Scene(scene: 'login')]

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Request\Api\UserRequest;
use App\Service\Api\User\BindPhoneByWxService;
@@ -18,7 +19,7 @@ use Psr\Container\NotFoundExceptionInterface;
#[Middlewares([
JwtAuthMiddleware::class,
])]
class UserController
class UserController extends AbstractController
{
/**
* @param UserRequest $request

View File

@@ -13,6 +13,7 @@ namespace App\Cron\Good;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\Common\CycleCache;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Extend\DateUtil;
use App\Lib\Log;
use App\Model\Cycle;
@@ -72,7 +73,7 @@ class CycleCreateTask
$noDataInsertArr = [];
$noDataArr = [];
foreach ($nextCycleRange as $date) {
$one = $this->redis->zScore($key,$date,'system');
$one = $this->redis->zScore($key,$date,RedisCode::SYSTEM_DB);
if ($one) continue;
@@ -93,7 +94,7 @@ class CycleCreateTask
$res = $res->toArray();
foreach ($res as $one) {
$this->redis->zAdd($key,$one['id'],$one['dates'],'system');
$this->redis->zAdd($key,$one['id'],$one['dates'],RedisCode::SYSTEM_DB);
}
$this->log->notice('添加周期成功,添加的周期为'.json_encode($noDataArr,JSON_UNESCAPED_UNICODE));

View File

@@ -12,6 +12,7 @@ namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Extend\DateUtil;
use App\Lib\Log;
use App\Model\OssObject;
@@ -58,7 +59,7 @@ class OssDelByDisableTask
foreach ($list->toArray() as $item) {
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByOssId(), (string)$item,'system');
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByOssId(), (string)$item,RedisCode::SYSTEM_DB);
}
$this->log->notice(__CLASS__.'异步删除昨日无效图片');

View File

@@ -12,6 +12,7 @@ namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Lib\Log;
use App\Model\OssObject;
use Hyperf\Crontab\Annotation\Crontab;
@@ -48,7 +49,7 @@ class OssDelByOssIdTask
$ossIds = [];
for ($i = 1; $i < 50; $i++) {
$one = $this->redis->rPop($key,'system');
$one = $this->redis->rPop($key,RedisCode::SYSTEM_DB);
if (empty($one)){
continue;
}
@@ -87,7 +88,7 @@ class OssDelByOssIdTask
//把图片地址丢到删除redis队列
foreach ($urlList as $item) {
$url = substr(parse_url($item)['path'],1);
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $url,'system');
$this->redis->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $url,RedisCode::SYSTEM_DB);
}
$this->log->notice(__CLASS__.':success:删除oss文件个数:' . count($ossIds));

View File

@@ -12,6 +12,7 @@ namespace App\Cron\Oss;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Lib\Log;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
@@ -62,7 +63,7 @@ class OssDelByUrlTask
$delNum = 0;
for ($i = 1; $i < 50; $i++) {
$url = $this->redis->rPop($key,'system');
$url = $this->redis->rPop($key,RedisCode::SYSTEM_DB);
if (!empty($url)) {
$delNum++;

View File

@@ -0,0 +1,21 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Good;
use App\Service\Api\BaseService;
class MealListService extends BaseService
{
public function handle()
{
//todo Write logic
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Good;
use App\Service\Api\BaseService;
class OptionalListService extends BaseService
{
public function handle()
{
//todo Write logic
}
}

View File

@@ -13,6 +13,7 @@ namespace App\Service\Common;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\Common\OssObjectCode;
use App\Constants\RedisCode;
use App\Exception\ErrException;
use App\Lib\AdminReturn;
use App\Lib\Log;
@@ -308,7 +309,7 @@ class OssCallbackService
private function deleteOssObject(): void
{
//删除旧的文件
$this->redisCache->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $this->data['object'],'system');
$this->redisCache->lPush(CommonRedisKey::getDeleteOssImgListByUrl(), $this->data['object'],RedisCode::SYSTEM_DB);
}
}

View File

@@ -13,6 +13,7 @@ namespace App\Service\ServiceTrait\Api;
use App\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use App\Exception\ErrException;
use App\Lib\Log;
use GuzzleHttp\Exception\GuzzleException;
@@ -75,7 +76,7 @@ trait WxMiniTrait
if (empty($data) || empty($data['access_token']) || empty($data['expires_in'])) return '';
$this->redisCache->setEx($key, $data['access_token'], $data['expires_in'] - 10,'system');
$this->redisCache->setEx($key, $data['access_token'], $data['expires_in'] - 10,RedisCode::SYSTEM_DB);
return $data['access_token'];
}

View File

@@ -2,7 +2,9 @@
namespace App\Service\ServiceTrait\Common;
use App\Cache\Redis\Common\ConfigCache;
use App\Cache\Redis\Common\CycleCache;
use App\Constants\ConfigCode;
use Hyperf\Di\Annotation\Inject;
trait CycleTrait
@@ -13,10 +15,22 @@ trait CycleTrait
#[Inject]
protected CycleCache $cycleCache;
/**
* @var ConfigCache
*/
#[Inject]
protected ConfigCache $configCache;
protected function initTodayCycleId()
{
$TodayCutOffTime = $this->configCache->getConfigValueByKey(ConfigCode::TODAY_CUT_OFF_TIME_KEY);
if (date('H:i:s') >= ($TodayCutOffTime)) {
$day = date('Y-m-d',strtotime('+1 day'));
} else {
$day = date('Y-m-d',strtotime('today'));
}
}
}

View File

@@ -12,6 +12,7 @@ namespace App\Service\ServiceTrait\Common;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\RedisCode;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -36,7 +37,7 @@ trait SiteTrait
public function setSiteCache($value, string $lng = '0', string $lat = '0'): void
{
$key = CommonRedisKey::getActivateSiteList();
$this->redisCache->zRem($key,$value,'system');
$this->redisCache->geoAdd($key,$lng,$lat,$value,'system');
$this->redisCache->zRem($key,$value,RedisCode::SYSTEM_DB);
$this->redisCache->geoAdd($key,$lng,$lat,$value,RedisCode::SYSTEM_DB);
}
}