- RedisTokenStore 구현 (ITokenStore, Redis StringSet/Get/KeyDelete) - DI 등록 변경 (InMemoryTokenStore → RedisTokenStore) - AddMemoryCache() 제거 (더 이상 사용처 없음) Closes #162
69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using SPMS.Application.Interfaces;
|
|
using SPMS.Application.Settings;
|
|
|
|
namespace SPMS.Infrastructure.Caching;
|
|
|
|
public class RedisTokenStore : ITokenStore
|
|
{
|
|
private readonly RedisConnection _redis;
|
|
private readonly RedisSettings _settings;
|
|
private readonly ILogger<RedisTokenStore> _logger;
|
|
|
|
public RedisTokenStore(
|
|
RedisConnection redis,
|
|
IOptions<RedisSettings> settings,
|
|
ILogger<RedisTokenStore> logger)
|
|
{
|
|
_redis = redis;
|
|
_settings = settings.Value;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task StoreAsync(string key, string value, TimeSpan expiry)
|
|
{
|
|
try
|
|
{
|
|
var db = await _redis.GetDatabaseAsync();
|
|
await db.StringSetAsync(BuildKey(key), value, expiry);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "토큰 저장 실패: key={Key}", key);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<string?> GetAsync(string key)
|
|
{
|
|
try
|
|
{
|
|
var db = await _redis.GetDatabaseAsync();
|
|
var value = await db.StringGetAsync(BuildKey(key));
|
|
return value.IsNullOrEmpty ? null : value.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "토큰 조회 실패: key={Key}", key);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task RemoveAsync(string key)
|
|
{
|
|
try
|
|
{
|
|
var db = await _redis.GetDatabaseAsync();
|
|
await db.KeyDeleteAsync(BuildKey(key));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "토큰 삭제 실패: key={Key}", key);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private string BuildKey(string key) => $"{_settings.InstanceName}token:{key}";
|
|
}
|