47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cache\Redis\Admin;
|
|
|
|
use App\Cache\Redis\RedisCache;
|
|
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),'system') ?? 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,'system');
|
|
return $this->redis->setEx($key, $token, $ttl, 'system');
|
|
}
|
|
} |