Files
hyperf_service/app/Cache/Redis/Api/UserCache.php
2025-03-20 15:29:13 +08:00

55 lines
1.3 KiB
PHP

<?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, (string)$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, (string)$userId);
}
/**
* @param int $userId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function removeUserToken(int $userId): void
{
$key = CommonRedisKey::userTokenHashKey();
$this->redis->hDel($key, (string)$userId);
}
}