-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #793 from DFE-Digital/feat/216205/implement-new-sq…
…l-caching Feat: Implement new sql caching
- Loading branch information
Showing
13 changed files
with
243 additions
and
91 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
29 changes: 29 additions & 0 deletions
29
src/Dfe.PlanTech.Application/Caching/Models/QueryCacher.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,29 @@ | ||
using Dfe.PlanTech.Application.Caching.Interfaces; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Dfe.PlanTech.Application.Caching.Models; | ||
|
||
public class QueryCacher : IQueryCacher | ||
{ | ||
private MemoryCache _cache = new(new MemoryCacheOptions()); | ||
private const int CacheDurationMinutes = 30; | ||
|
||
public async Task<TResult> GetOrCreateAsyncWithCache<T, TResult>( | ||
string key, | ||
IQueryable<T> queryable, | ||
Func<IQueryable<T>, CancellationToken, Task<TResult>> queryFunc, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await _cache.GetOrCreateAsync(key, entry => | ||
{ | ||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(CacheDurationMinutes); | ||
return queryFunc(queryable, cancellationToken); | ||
}) ?? await queryFunc(queryable, cancellationToken); | ||
} | ||
|
||
public void ClearCache() | ||
{ | ||
_cache.Dispose(); | ||
_cache = new MemoryCache(new MemoryCacheOptions()); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/Dfe.PlanTech.Domain/Caching/Interfaces/IQueryCacher.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,12 @@ | ||
namespace Dfe.PlanTech.Application.Caching.Interfaces; | ||
|
||
public interface IQueryCacher | ||
{ | ||
public Task<TResult> GetOrCreateAsyncWithCache<T, TResult>( | ||
string key, | ||
IQueryable<T> queryable, | ||
Func<IQueryable<T>, CancellationToken, Task<TResult>> queryFunc, | ||
CancellationToken cancellationToken = default); | ||
|
||
public void ClearCache(); | ||
} |
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
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
Oops, something went wrong.