-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfccb43
commit 951e9db
Showing
4 changed files
with
100 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
AuthService/Services/AccessDataCache/AccessDataCacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Text.Json; | ||
using AuthService.Models.AccessDataCache.Requests; | ||
using AuthService.Models.AccessDataCache.Responses; | ||
using AuthService.Services.Models; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
|
||
namespace AuthService.Services.AccessDataCache; | ||
|
||
public class AccessDataCacheService(IDistributedCache cache, ILogger<AccessDataCacheService> logger) : IAccessDataCacheService | ||
{ | ||
private readonly IDistributedCache _cache = cache; | ||
private readonly ILogger<AccessDataCacheService> _logger = logger; | ||
|
||
public async Task<UserAccessData?> Get(string username) | ||
{ | ||
_logger.LogDebug("Retrieving user from cache..."); | ||
try | ||
{ | ||
var userBytes = await _cache.GetAsync(username); | ||
|
||
return userBytes == null ? null : JsonSerializer.Deserialize<UserAccessData>(userBytes); | ||
} | ||
catch (JsonException) | ||
{ | ||
// Clear cache if deserialization fails | ||
_logger.LogError("Deserialization failed, removing user from cache..."); | ||
await _cache.RemoveAsync(username); | ||
throw; | ||
} | ||
catch (Exception) | ||
{ | ||
_logger.LogWarning("Failed to retrieve user {user} from cache", username); | ||
throw; | ||
} | ||
} | ||
|
||
public async Task<RecacheUserResponse> RecacheUser(RecacheUserRequest user) | ||
{ | ||
// TODO: some validation checks | ||
|
||
_logger.LogDebug("Received request to recache user {user}...", user.Username); | ||
try | ||
{ | ||
await _cache.SetStringAsync(user.Username, JsonSerializer.Serialize(user)); | ||
_logger.LogDebug("User {user} was successfully recached", user.Username); | ||
} | ||
catch (Exception) | ||
{ | ||
_logger.LogWarning("Failed to recache user {user}", user.Username); | ||
throw; | ||
} | ||
return new RecacheUserResponse { IsSuccess = true }; | ||
} | ||
|
||
public async Task<UserAccessData?> RequestAndCacheUser(string username) | ||
{ | ||
_logger.LogDebug("Retrieving user from cache..."); | ||
try | ||
{ | ||
var userBytes = await _cache.GetAsync(username); | ||
|
||
if (userBytes == null) | ||
{ | ||
_logger.LogDebug("User not found in cache, requesting user from userservice..."); | ||
|
||
// TODO: Implement communication with userservice | ||
throw new NotImplementedException("Communication with userservice not implemented"); | ||
} | ||
|
||
return userBytes == null ? null : JsonSerializer.Deserialize<UserAccessData>(userBytes); | ||
} | ||
catch (JsonException) | ||
{ | ||
// Clear cache if deserialization fails | ||
_logger.LogError("Deserialization failed, removing user from cache..."); | ||
await _cache.RemoveAsync(username); | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters