improvement: InMemoryTokenStore를 Redis 기반으로 교체 (#162)
All checks were successful
SPMS_API/pipeline/head This commit looks good
All checks were successful
SPMS_API/pipeline/head This commit looks good
Reviewed-on: https://git.ipstein.myds.me/SPMS/SPMS_API/pulls/163
This commit is contained in:
commit
febd6f6da0
68
SPMS.Infrastructure/Caching/RedisTokenStore.cs
Normal file
68
SPMS.Infrastructure/Caching/RedisTokenStore.cs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
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}";
|
||||
}
|
||||
|
|
@ -88,8 +88,7 @@ public static class DependencyInjection
|
|||
services.AddHostedService<DataRetentionWorker>();
|
||||
|
||||
// Token Store & Email Service
|
||||
services.AddMemoryCache();
|
||||
services.AddSingleton<ITokenStore, InMemoryTokenStore>();
|
||||
services.AddSingleton<ITokenStore, RedisTokenStore>();
|
||||
services.AddSingleton<IEmailService, ConsoleEmailService>();
|
||||
|
||||
return services;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user