ExpressionCache.Core 5.0.0

dotnet add package ExpressionCache.Core --version 5.0.0                
NuGet\Install-Package ExpressionCache.Core -Version 5.0.0                
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="ExpressionCache.Core" Version="5.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExpressionCache.Core --version 5.0.0                
#r "nuget: ExpressionCache.Core, 5.0.0"                
#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.
// Install ExpressionCache.Core as a Cake Addin
#addin nuget:?package=ExpressionCache.Core&version=5.0.0

// Install ExpressionCache.Core as a Cake Tool
#tool nuget:?package=ExpressionCache.Core&version=5.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)

  1. Lets say we call GetAsync(5) on the above snippet.
  2. cacheAction is not equal to Bypass, so InvokeCacheAsync gets called.
  3. A lookup in cache with generated key {SampleService}{GetAsync}{5}{Bypass} happens.
  4. No cache entry is found so the expression GetAsync(5, CacheAction.Bypass) is invoked.
  5. This time the cache is skipped because of Bypass. Result of sampleRepository is returned.
  6. The returned value gets cached and InvokeCacheAsync returns.

CacheAction enum

  1. Invoke - The default action. Will check for a cached value and return it if found.
  2. Bypass - Used to bypass the caching entirely. Note! Should always be used in the expression to InvokeCache.
  3. 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 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 was computed.  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. 
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 ExpressionCache.Core:

Package Downloads
ExpressionCache.Distributed

ExpressionCache service for IDistributedCache interface.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 99 7/30/2024
4.0.1 317 10/18/2023
4.0.0 175 10/17/2023
3.1.0 814 4/14/2022
3.0.0 569 1/20/2021
2.0.0 663 11/3/2020
1.0.0 3,134 6/24/2018