CacheService 8.0.3
See the version list below for details.
dotnet add package CacheService --version 8.0.3
NuGet\Install-Package CacheService -Version 8.0.3
<PackageReference Include="CacheService" Version="8.0.3" />
<PackageVersion Include="CacheService" Version="8.0.3" />
<PackageReference Include="CacheService" />
paket add CacheService --version 8.0.3
#r "nuget: CacheService, 8.0.3"
#:package CacheService@8.0.3
#addin nuget:?package=CacheService&version=8.0.3
#tool nuget:?package=CacheService&version=8.0.3
CacheService
CacheService is a simple and fast double layer cache service for dotnet core.
Features
The main idea is to have an in memory cache and a distributed cache, both managed by a single service: ICacheService.
This service have the GetOrSetAsync() method and it should:
- Read from MemoryCache (if exists return the read value)
- Read from DistributedCache (if exists return the read value) and then set it in MemoryCache
- Read from source (if not exists return
null) and then set in MemoryCache and DistributedCache.
And all values read from any source or cache should be automatically refreshed in the background at a specified time.
Quick Start
Before using this library, you need to install the NuGet package:
dotnet add package CacheService
CacheService has some dependencies should be already registered in your application: ILoggerFactory, IMemoryCache and IDistributed. As an example:
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.StackExchangeRedis;
// dependencies
services.AddLogging();
services.AddMemoryCache();
services.AddStackExchangeRedisCache(op => ...);
Next, to use the CacheService you need to add the following line in the startup file:
// register cache service
services.AddCacheService();
Finally you can use the ICacheService in your methods getting the cache from the ServiceProvider:
var cache = serviceProvider.GetRequiredService<ICacheService>();
var myCachedValue = cache.GetOrSetAsync("some-key", ct => GetValueFromDatabaseAsync(ct), cancellationToken);
Or in any Asp.Net Core MVC controller:
public WeatherForecastController(ICacheService cache)
{
_cache = cache;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<IActionResult> GetAsync()
{
var model = await _cache.GetOrSetAsync("forecast", ct => GetFromDatabaseAsync(ct), HttpContext.RequestAborted);
return Ok(model);
}
ICacheService
The main ICacheService method is GetOrSetAsync. It allows you to get a value from the cache or set a new value in the cache:
ValueTask<T> GetOrSetAsync<T>(string key, CacheServiceOptions options, Func<CancellationToken, ValueTask<T>> getter, CancellationToken cancellationToken = default);
The parameters are:
| Parameter | Mandatory | Description | Type |
| --------- | ---------- | ----------- | ---- |
| key | Yes | The cache key | string |
| options | No | The CacheServiceOptions to use | CacheServiceOptions |
| getter | Yes | The function to get the value from the source | Func<CancellationToken, ValueTask<T>> |
| cancellationToken | No | The cancellation token | CancellationToken |
CacheServiceOptions
CacheServiceOptions is a class that contains the options to use in the ICacheService methods. It has the following properties:
| Property | Description | Type |
|---|---|---|
| Memory | Sets the configuration for the in memory cache | CacheOptions |
| Distributed | Sets the configuration for the distributed cache | CacheOptions |
| ForceRefresh | Sets if you want to force the refresh of the cache value | bool |
CacheOptions
CacheOptions is a class that contains the options to use in each configured kind of cache. It has the following properties:
| Property | Description | Type | Default |
|---|---|---|---|
| AbsoluteExpiration | Sets an absolute expiration date for the cache entry | DateTimeOffset |
null |
| AbsoluteExpirationRelativeToNow | Sets an absolute expiration date relative to now for the cache entry | TimeSpan |
null |
| SlidingExpiration | sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed. This will not extend the entry lifetime beyond the absolute expiration (if set) | TimeSpan |
null |
| RefreshInterval | Sets the interval to automatically refresh the cache value | TimeSpan |
null |
Configuration
You can add the cache service to the services collection:
services.AddCacheService(op => ...);
And you can configure the ICacheService with the following options:
| Property | Description | Type | Default |
|---|---|---|---|
| DefaultOptions | Sets the default options to use in the ICacheService methods |
CacheServiceOptions |
|
| UseMemoryCache | Sets if you want to manage IMemoryCache with ICacheService |
bool |
true |
| UseDistributedCache | Sets if you want to manage IDistributedCache with ICacheService |
bool |
true |
| BackgroundJobMode | Sets how you want to use the background process to automatically update your cache values<br/>Options are: None, HostedService or Timer |
BackgroundJobMode |
BackgroundJobMode.HostedService |
| BackgroundJobInterval | Sets the background process to update cache value execution interval | TimeSpan |
TimeSpan.FromMinutes(1) |
License
The source code we develop at CacheService is default being licensed as MIT. You can read more about here.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
-
net6.0
- Microsoft.Extensions.Caching.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
-
net7.0
- Microsoft.Extensions.Caching.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
-
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.1 | 633 | 12/9/2025 |
| 10.0.0 | 584 | 11/19/2025 |
| 9.1.3 | 12,515 | 4/1/2025 |
| 9.1.2 | 4,221 | 1/29/2025 |
| 9.1.1 | 5,506 | 12/20/2024 |
| 9.1.0 | 853 | 11/28/2024 |
| 9.0.1 | 220 | 11/26/2024 |
| 9.0.0 | 1,148 | 11/13/2024 |
| 8.0.4 | 10,611 | 10/2/2024 |
| 8.0.3 | 197 | 10/1/2024 |
| 8.0.2 | 9,999 | 2/29/2024 |
| 8.0.1 | 8,689 | 1/19/2024 |
| 8.0.0 | 963 | 12/14/2023 |
| 0.0.3 | 3,275 | 5/4/2023 |
| 0.0.2 | 2,788 | 11/12/2021 |
| 0.0.1 | 554 | 11/11/2021 |
| 0.0.1-rc1 | 379 | 11/5/2021 |
| 0.0.1-beta | 351 | 11/1/2021 |
| 0.0.1-alpha | 370 | 11/1/2021 |