Skip to content

Commit

Permalink
Adding central cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Keilholz committed Sep 23, 2024
1 parent 9f2c0e9 commit 6d0ddbb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/Wam.Core/Abstractions/IWamCacheService.cs
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);
}
74 changes: 74 additions & 0 deletions src/Wam.Core/Cache/WamCacheService.cs
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);
}
}
7 changes: 4 additions & 3 deletions src/Wam.Core/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using HexMaster.RedisCache;
using Man.Dapr.Sidekick;
using Man.Dapr.Sidekick;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Wam.Core.Abstractions;
using Wam.Core.Cache;
using Wam.Core.Identity;

namespace Wam.Core.Configuration;
Expand Down Expand Up @@ -56,8 +57,8 @@ public static IServiceCollection AddWamCoreConfiguration(
services.AddApplicationInsightsTelemetry(configuration);
}

services.AddHexMasterCache(configuration);
services.AddDaprClient();
services.AddScoped<IWamCacheService, WamCacheService>();
return services;
}
}
2 changes: 0 additions & 2 deletions src/Wam.Core/Wam.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.Messaging.WebPubSub" Version="1.4.0" />
<PackageReference Include="Dapr.AspNetCore" Version="1.14.0" />
<PackageReference Include="HexMaster.RedisCache" Version="0.1.6" />
<PackageReference Include="Man.Dapr.Sidekick.AspNetCore" Version="1.2.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
Expand All @@ -32,7 +31,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Cache\" />
<Folder Include="DataTransferObjects\" />
</ItemGroup>

Expand Down

0 comments on commit 6d0ddbb

Please sign in to comment.