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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GovUK.Dfe.CoreLibs.Caching" Version="1.0.13" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GovUK.Dfe.CoreLibs.Caching" Version="1.0.13" />
                    
Directory.Packages.props
<PackageReference Include="GovUK.Dfe.CoreLibs.Caching" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GovUK.Dfe.CoreLibs.Caching --version 1.0.13
                    
#r "nuget: GovUK.Dfe.CoreLibs.Caching, 1.0.13"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GovUK.Dfe.CoreLibs.Caching@1.0.13
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GovUK.Dfe.CoreLibs.Caching&version=1.0.13
                    
Install as a Cake Addin
#tool nuget:?package=GovUK.Dfe.CoreLibs.Caching&version=1.0.13
                    
Install as a Cake Tool

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

  1. Service Registration: You use ICacheService in 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);
        }
    
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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