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 _logger; public RedisTokenStore( RedisConnection redis, IOptions settings, ILogger 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 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}"; }