feat : config

This commit is contained in:
2025-03-20 15:27:53 +08:00
parent ef9f663fb2
commit d3d0cda616
9 changed files with 164 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Cache\Redis\Api;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class UserCache
{
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* @param int $userId
* @param string $token
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function setUserToken(int $userId,string $token): void
{
$key = CommonRedisKey::userTokenHashKey();
$this->redis->hSet($key, $userId, $token);
}
/**
* @param int $userId
* @return false|mixed|\Redis|string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getUserToken(int $userId): mixed
{
$key = CommonRedisKey::userTokenHashKey();
return $this->redis->hGet($key, $userId);
}
/**
* @param int $userId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function removeUserToken(int $userId): void
{
$key = CommonRedisKey::userTokenHashKey();
$this->redis->hDel($key, $userId);
}
}

View File

@@ -119,4 +119,12 @@ class CommonRedisKey
{
return '__system:yiLianYun:self:app:token';
}
/**
* @return string
*/
public static function userTokenHashKey(): string
{
return 'user:token:online';
}
}

View File

@@ -84,7 +84,7 @@ class ConfigCache
};
if ($isRedis == 1) {
$this->redis->setEx($redisKey, $value, $expire,RedisCode::SYSTEM_DB);
$this->redis->setEx($redisKey, $value ?? ConfigCode::DEFAULT_VALUE[$key], $expire,RedisCode::SYSTEM_DB);
}
return $value ?? ConfigCode::DEFAULT_VALUE[$key];