-
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
Eduard Keilholz
committed
Sep 23, 2024
1 parent
9f2c0e9
commit 6d0ddbb
Showing
4 changed files
with
87 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Wam.Core.Abstractions; | ||
|
||
public interface IWamCacheService | ||
{ | ||
Task<T> GetFromCacheOrInitialize<T>(string cacheKey, Func<Task<T>> initializeFunction, int ttlInSeconds = 900, | ||
CancellationToken cancellationToken = default); | ||
Task SaveState<T>(string cacheKey, T data, int ttlInSeconds = 900, CancellationToken? cancellationToken = null); | ||
Task Invalidate(string cacheKey, CancellationToken? cancellationToken = null); | ||
} |
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,74 @@ | ||
using Dapr.Client; | ||
using Microsoft.Extensions.Logging; | ||
using Wam.Core.Abstractions; | ||
|
||
namespace Wam.Core.Cache; | ||
|
||
public class WamCacheService( | ||
ILogger<WamCacheService> logger, | ||
DaprClient dapr) : IWamCacheService | ||
{ | ||
|
||
private const string StateStoreName = "statestore"; | ||
|
||
public async Task<T> GetFromCacheOrInitialize<T>(string cacheKey, Func<Task<T>> initializeFunction, int ttlInSeconds = 900, CancellationToken cancellationToken = default ) | ||
{ | ||
try | ||
{ | ||
var state = await dapr.GetStateEntryAsync<T>( | ||
StateStoreName, | ||
cacheKey, | ||
cancellationToken: cancellationToken); | ||
|
||
if (state.Value != null) | ||
{ | ||
logger.LogInformation("Received information from cache ({cacheKey})", cacheKey); | ||
return state.Value; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Failed to get state from cache"); | ||
} | ||
|
||
var tResult = await initializeFunction(); | ||
|
||
try | ||
{ | ||
if (tResult != null) | ||
{ | ||
await SaveState(cacheKey, tResult, ttlInSeconds, cancellationToken); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Failed to save state to cache"); | ||
} | ||
|
||
return tResult; | ||
} | ||
|
||
public async Task SaveState<T>(string cacheKey, T data, int ttlInSeconds = 900, CancellationToken? cancellationToken = null) | ||
{ | ||
|
||
var usableCancellationToken = cancellationToken ?? CancellationToken.None; | ||
await dapr.SaveStateAsync( | ||
StateStoreName, | ||
cacheKey, | ||
data, | ||
metadata: new Dictionary<string, string> | ||
{ | ||
{ | ||
"ttlInSeconds", $"{ttlInSeconds}" | ||
} | ||
}, | ||
cancellationToken: usableCancellationToken); | ||
} | ||
|
||
public async Task Invalidate(string cacheKey, CancellationToken? cancellationToken = null) | ||
{ | ||
|
||
var usableCancellationToken = cancellationToken ?? CancellationToken.None; | ||
await dapr.DeleteStateAsync(StateStoreName, cacheKey, cancellationToken: usableCancellationToken); | ||
} | ||
} |
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