Skip to content

Commit

Permalink
(#description: Modified CachingService to be generic and support othe…
Browse files Browse the repository at this point in the history
…r types of caching storage)
  • Loading branch information
Farshad DASHTI authored and Farshad DASHTI committed Oct 8, 2024
1 parent ede4b8e commit 2f8fcda
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/nuget-package-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ jobs:
dotnet build -c Release
dotnet pack -c Release -p:PackageVersion=${{ env.NEW_VERSION }} --output .
dotnet nuget push "*.nupkg" --api-key ${{ secrets.GITHUB_TOKEN }} --source https://nuget.pkg.github.com/DFE-Digital/index.json
- name: Get Release Description
id: extract_description
run: |
DESCRIPTION=$(git log -1 --pretty=format:"%s" | grep -oP '\(#description:.*\)' | sed 's/#description: //')
if [[ -z "$DESCRIPTION" ]]; then
DESCRIPTION="No release notes provided."
fi
echo "RELEASE_DESCRIPTION=$DESCRIPTION" >> $GITHUB_ENV
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ env.NEW_VERSION }}
release_name: Release ${{ env.NEW_VERSION }}
body: ${{ env.RELEASE_DESCRIPTION }}
draft: false
prerelease: false
3 changes: 2 additions & 1 deletion src/DfE.CoreLibs.Caching/Interfaces/ICacheService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace DfE.CoreLibs.Caching.Interfaces
{
public interface ICacheService
public interface ICacheService<TCacheType> where TCacheType : ICacheType

Check warning on line 3 in src/DfE.CoreLibs.Caching/Interfaces/ICacheService.cs

View workflow job for this annotation

GitHub Actions / build-and-test / build-and-test

'TCacheType' is not used in the interface. (https://rules.sonarsource.com/csharp/RSPEC-2326)

Check warning on line 3 in src/DfE.CoreLibs.Caching/Interfaces/ICacheService.cs

View workflow job for this annotation

GitHub Actions / build-and-test / build-and-test

'TCacheType' is not used in the interface. (https://rules.sonarsource.com/csharp/RSPEC-2326)
{
Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> fetchFunction, string methodName);
void Remove(string cacheKey);
Type CacheType { get; }
}
}
4 changes: 4 additions & 0 deletions src/DfE.CoreLibs.Caching/Interfaces/ICacheType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace DfE.CoreLibs.Caching.Interfaces
{
public interface ICacheType;
}
4 changes: 4 additions & 0 deletions src/DfE.CoreLibs.Caching/Interfaces/IMemoryCacheType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace DfE.CoreLibs.Caching.Interfaces
{
public interface IMemoryCacheType : ICacheType;
}
2 changes: 1 addition & 1 deletion src/DfE.CoreLibs.Caching/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IServiceCollection AddServiceCaching(
this IServiceCollection services, IConfiguration config)
{
services.Configure<CacheSettings>(config.GetSection("CacheSettings"));
services.AddSingleton<ICacheService, MemoryCacheService>();
services.AddSingleton<ICacheService<IMemoryCacheType>, MemoryCacheService>();

return services;
}
Expand Down
7 changes: 4 additions & 3 deletions src/DfE.CoreLibs.Caching/Services/MemoryCacheService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using DfE.CoreLibs.Caching.Interfaces;
using DfE.CoreLibs.Caching.Settings;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;

namespace DfE.CoreLibs.Caching.Services
{
Expand All @@ -12,9 +12,10 @@ public class MemoryCacheService(
IMemoryCache memoryCache,
ILogger<MemoryCacheService> logger,
IOptions<CacheSettings> cacheSettings)
: ICacheService
: ICacheService<IMemoryCacheType>
{
private readonly CacheSettings _cacheSettings = cacheSettings.Value;
private readonly MemoryCacheSettings _cacheSettings = cacheSettings.Value.Memory;
public Type CacheType => typeof(IMemoryCacheType);

public async Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> fetchFunction, string methodName)
{
Expand Down
5 changes: 5 additions & 0 deletions src/DfE.CoreLibs.Caching/Settings/CacheSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
namespace DfE.CoreLibs.Caching.Settings
{
public class CacheSettings
{
public MemoryCacheSettings Memory { get; set; } = new();
}

public class MemoryCacheSettings
{
public int DefaultDurationInSeconds { get; set; } = 5;
public Dictionary<string, int> Durations { get; set; } = new();
Expand Down

0 comments on commit 2f8fcda

Please sign in to comment.