Files
hyperf_service/app/Cache/Redis/Admin/UserCache.php
2025-01-21 16:20:16 +08:00

48 lines
1.2 KiB
PHP

<?php
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;
class UserCache
{
/**
* @var RedisCache $redis
*/
#[Inject]
protected RedisCache $redis;
/**
* @param $userId
* @return string|false
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function getAdminToken($userId): false|string
{
return $this->redis->get(AdminRedisKey::adminUserToken($userId),RedisCode::SYSTEM_DB) ?? false;
}
/**
* @param $userId
* @param string $token
* @param int $ttl
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function setAdminToken($userId, string $token, int $ttl): bool
{
$key = AdminRedisKey::adminUserToken($userId);
$this->redis->delete($key,RedisCode::SYSTEM_DB);
return $this->redis->setEx($key, $token, $ttl, RedisCode::SYSTEM_DB);
}
}