52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
namespace AAIntegration.SimmonsBank.API.Services;
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
public interface ICacheService
|
|
{
|
|
int GetClientIdFromApiKey(string apiKey);
|
|
}
|
|
|
|
public class CacheService : ICacheService
|
|
{
|
|
private readonly IMemoryCache _memoryCache;
|
|
private readonly IUserService _userService;
|
|
|
|
public CacheService(IMemoryCache memoryCache, IUserService userService)
|
|
{
|
|
_memoryCache = memoryCache;
|
|
_userService = userService;
|
|
}
|
|
|
|
public int GetClientIdFromApiKey(string apiKey)
|
|
{
|
|
if (!_memoryCache.TryGetValue<Dictionary<string, int>>($"Authentication_ApiKeys", out var internalKeys))
|
|
{
|
|
internalKeys = _userService.GetAllApiKeys();
|
|
|
|
_memoryCache.Set($"Authentication_ApiKeys", internalKeys);
|
|
}
|
|
|
|
if (!internalKeys.TryGetValue(apiKey, out var clientId))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
return clientId;
|
|
}
|
|
|
|
/*public void InvalidateApiKey(string apiKey)
|
|
{
|
|
if (_memoryCache.TryGetValue<Dictionary<string, Guid>>("Authentication_ApiKeys", out var internalKeys))
|
|
{
|
|
if (internalKeys.ContainsKey(apiKey))
|
|
{
|
|
internalKeys.Remove(apiKey);
|
|
_memoryCache.Set("Authentication_ApiKeys", internalKeys);
|
|
}
|
|
}
|
|
|
|
_userService.InvalidateApiKey(apiKey);
|
|
}*/
|
|
}
|