CacheService 8.0.1
See the version list below for details.
dotnet add package CacheService --version 8.0.1
NuGet\Install-Package CacheService -Version 8.0.1
<PackageReference Include="CacheService" Version="8.0.1" />
paket add CacheService --version 8.0.1
#r "nuget: CacheService, 8.0.1"
// Install CacheService as a Cake Addin #addin nuget:?package=CacheService&version=8.0.1 // Install CacheService as a Cake Tool #tool nuget:?package=CacheService&version=8.0.1
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. |
-
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 |
---|---|---|
9.1.1 | 90 | 12/20/2024 |
9.1.0 | 327 | 11/28/2024 |
9.0.1 | 109 | 11/26/2024 |
9.0.0 | 319 | 11/13/2024 |
8.0.4 | 4,552 | 10/2/2024 |
8.0.3 | 119 | 10/1/2024 |
8.0.2 | 9,575 | 2/29/2024 |
8.0.1 | 5,644 | 1/19/2024 |
8.0.0 | 854 | 12/14/2023 |
0.0.3 | 3,159 | 5/4/2023 |
0.0.2 | 2,633 | 11/12/2021 |
0.0.1 | 345 | 11/11/2021 |
0.0.1-rc1 | 230 | 11/5/2021 |
0.0.1-beta | 204 | 11/1/2021 |
0.0.1-alpha | 206 | 11/1/2021 |