GovUK.Dfe.CoreLibs.Caching
1.0.13
Prefix Reserved
dotnet add package GovUK.Dfe.CoreLibs.Caching --version 1.0.13
NuGet\Install-Package GovUK.Dfe.CoreLibs.Caching -Version 1.0.13
<PackageReference Include="GovUK.Dfe.CoreLibs.Caching" Version="1.0.13" />
<PackageVersion Include="GovUK.Dfe.CoreLibs.Caching" Version="1.0.13" />
<PackageReference Include="GovUK.Dfe.CoreLibs.Caching" />
paket add GovUK.Dfe.CoreLibs.Caching --version 1.0.13
#r "nuget: GovUK.Dfe.CoreLibs.Caching, 1.0.13"
#:package GovUK.Dfe.CoreLibs.Caching@1.0.13
#addin nuget:?package=GovUK.Dfe.CoreLibs.Caching&version=1.0.13
#tool nuget:?package=GovUK.Dfe.CoreLibs.Caching&version=1.0.13
GovUK.Dfe.CoreLibs.Caching
This caching library offers a unified, efficient caching solution for .NET projects. It provides a simple, reusable abstraction over different caching mechanisms, enabling developers to easily implement in-memory and distributed caching strategies, improving the performance and scalability of their applications.
Installation
To install the GovUK.Dfe.CoreLibs.Caching Library, use the following command in your .NET project:
dotnet add package GovUK.Dfe.CoreLibs.Caching
Usage
Usage in Handlers
Service Registration: You use
ICacheServicein your handlers to store and retrieve data from memory to avoid unnecessary processing and database queries. Here's how you register the caching service:public void ConfigureServices(IServiceCollection services) { services.AddServiceCaching(config); }Usage in Handlers: Here's an example of how caching is used in one of your query handlers:
public class GetPrincipalBySchoolQueryHandler( ISchoolRepository schoolRepository, IMapper mapper, ICacheService<IMemoryCacheType> cacheService) : IRequestHandler<GetPrincipalBySchoolQuery, Principal?> { public async Task<Principal?> Handle(GetPrincipalBySchoolQuery request, CancellationToken cancellationToken) { var cacheKey = $"Principal_{CacheKeyHelper.GenerateHashedCacheKey(request.SchoolName)}"; var methodName = nameof(GetPrincipalBySchoolQueryHandler); return await cacheService.GetOrAddAsync(cacheKey, async () => { var principal= await schoolRepository .GetPrincipalBySchoolAsync(request.SchoolName, cancellationToken); var result = mapper.Map<Principal?>(principal); return result; }, methodName); } }
In this case, the query handler checks if the principals are cached by generating a unique cache key. If the data is not cached, it retrieves the data from the repository, caches it, and returns it.
Cache Duration Based on Method Name
The caching service dynamically determines the cache duration based on the method name. This is particularly useful when you want to apply different caching durations to different query handlers.
In this example, the cache duration for GetPrincipalBySchoolQueryHandler is retrieved from the configuration using the method name. If no specific duration is defined for the method, it will fall back to the default cache duration.
Example of Cache Settings in appsettings.json
Here is the configuration for cache durations in the appsettings.json file:
```csharp
"CacheSettings": {
"Memory": {
"DefaultDurationInSeconds": 60,
"Durations": {
"GetPrincipalBySchoolQueryHandler": 86400
}
}
```
This setup ensures that the GetPrincipalBySchoolQueryHandler cache duration is set to 24 hours (86400 seconds), while other handlers will use the default duration of 60 seconds if no specific duration is configured.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- StackExchange.Redis (>= 2.9.32)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GovUK.Dfe.CoreLibs.Caching:
| Package | Downloads |
|---|---|
|
GovUK.Dfe.CoreLibs.Security
A library providing flexible foundation for managing security in .NET projects, including role-based and claim-based policies, custom requirements, and dynamic claims. It enables consistent, configurable security across applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.13 | 4,132 | 11/3/2025 |
| 1.0.12 | 773 | 10/31/2025 |
| 1.0.11 | 235 | 10/28/2025 |
| 1.0.11-prerelease-8 | 213 | 10/27/2025 |
| 1.0.11-prerelease-7 | 200 | 10/27/2025 |
| 1.0.10 | 12,328 | 9/8/2025 |
| 0.1.0 | 209 | 9/8/2025 |
| 0.1.0-prerelease-143 | 213 | 9/8/2025 |