ExpressionCache.Core
4.0.0
See the version list below for details.
dotnet add package ExpressionCache.Core --version 4.0.0
NuGet\Install-Package ExpressionCache.Core -Version 4.0.0
<PackageReference Include="ExpressionCache.Core" Version="4.0.0" />
paket add ExpressionCache.Core --version 4.0.0
#r "nuget: ExpressionCache.Core, 4.0.0"
// Install ExpressionCache.Core as a Cake Addin #addin nuget:?package=ExpressionCache.Core&version=4.0.0 // Install ExpressionCache.Core as a Cake Tool #tool nuget:?package=ExpressionCache.Core&version=4.0.0
ExpressionCache.Core
ExpressionCache use providers to support different caching engines.
See ExpressionCache.Distributed
for a cache provider for IDistributedCache
.
Key generation
The cache key will be a combination of class name, function name and parameter values.
The following code snippet is the heart of the library:
public class SampleService
{
private readonly IDistributedCacheService _cacheService;
private readonly SampleRepository _sampleRepository;
public SampleService(IDistributedCacheService cacheService, SampleRepository sampleRepository)
{
_cacheService = cacheService;
_sampleRepository = sampleRepository;
}
public async Task<EntityModel> GetAsync(int entityId, CacheAction cacheAction = CacheAction.Invoke)
{
if (cacheAction != CacheAction.Bypass)
{
return await _cacheService.InvokeCacheAsync(
() => GetAsync(entityId, CacheAction.Bypass),
TimeSpan.FromDays(1), cacheAction);
}
return await _sampleRepository.GetAsync(entityId);
}
}
Flow (Without an existing cache entry)
- Lets say we call GetAsync(5) on the above snippet.
- cacheAction is not equal to Bypass, so InvokeCacheAsync gets called.
- A lookup in cache with generated key {SampleService}{GetAsync}{5}{Bypass} happens.
- No cache entry is found so the expression GetAsync(5, CacheAction.Bypass) is invoked.
- This time the cache is skipped because of Bypass. Result of sampleRepository is returned.
- The returned value gets cached and InvokeCacheAsync returns.
CacheAction enum
- Invoke - The default action. Will check for a cached value and return it if found.
- Bypass - Used to bypass the caching entirely. Note! Should always be used in the expression to InvokeCache.
- Overwrite - Skip checking for cached value, but still cache new value.
Object parameters
If complex objects are used as function parameters, ExpressionCache needs a way to know how to build the key value.
By extending ICacheKey one can define how to build the key.
public class SampleObject : ICacheKey
{
public int Parameter1 { get; set; }
public int Parameter2 { get; set; }
public virtual void BuildCacheKey(ICacheKeyBuilder builder)
{
builder
.By(Parameter1)
.By(Parameter2);
}
}
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 was computed. 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.Memory (>= 6.0.1)
-
net7.0
- Microsoft.Extensions.Caching.Memory (>= 7.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ExpressionCache.Core:
Package | Downloads |
---|---|
ExpressionCache.Distributed
ExpressionCache service for IDistributedCache interface. |
GitHub repositories
This package is not used by any popular GitHub repositories.