ExpressionCache.Distributed 4.0.1

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

// Install ExpressionCache.Distributed as a Cake Tool
#tool nuget:?package=ExpressionCache.Distributed&version=4.0.1

ExpressionCache.Distributed

ExpressionCache use providers to support different caching engines.

This package works with Microsofts IDistributedCache interface.

.NET Core Dependency Injection

The library include extensions for IServiceCollection interface.

public void ConfigureServices(IServiceCollection services)
{
    // Register an IDistributedCache implementation.
    services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "localhost";
        options.InstanceName = "Sample";
    });

    // Register the distributed cache service.
    services.AddDistributedExpressionCache();
}

You can now inject IDistributedCacheService using DI into you code.

Full service example

public class SampleService
{
    private readonly IDistributedCacheService _cacheService;
    private readonly SampleRepository _sampleRepository;

    public SampleService(IDistributedCacheService cacheService, SampleRepository sampleRepository)
    {
        _cacheService = cacheService;
        _sampleRepository = sampleRepository;
    }

    public async Task<bool> CreateAsync(EntityDto entity)
    {
        var newId = await _sampleRepository.CreateAsync(entity);
        if (newId > 0)
        {
            // Add new entity to cache.
            return await GetAsync(newId, CacheAction.Overwrite) != null;
        }
        return false;
    }

    public async Task<bool> UpdateAsync(EntityDto entity)
    {
        if (await _sampleRepository.UpdateAsync(entity))
        {
            // Overwrite cached entity with updated one.
            return await GetAsync(entity.Id, CacheAction.Overwrite) != null;
        }
        return false;
    }

    public async Task<bool> DeleteAsync(int entityId)
    {
        if (await _sampleRepository.DeleteAsync(id)) 
        {
            // Remove cached entity.
            await _cacheService.RemoveAsync(() => GetAsync(entityId, CacheAction.Bypass));
            return true;
        }
        return false;
    }

    public async Task<EntityModel> GetAsync(int entityId, CacheAction cacheAction = CacheAction.Invoke)
    {
        if (cacheAction != CacheAction.Bypass)
        {
            // Check for existing cached entity.
            return await _cacheService.InvokeCacheAsync(
                () => GetAsync(entityId, CacheAction.Bypass),
                TimeSpan.FromDays(1), cacheAction);
        }

        // If not already cached, this result will get cached.
        return await _sampleRepository.GetAsync(entityId);
    }
}

Interface members

The full interface of IDistributedCacheService.

public interface IExpressionCacheBase
{
    string GetKey<TResult>(Expression<Func<TResult>> expression);
    string GetKey<TResult>(Expression<Func<Task<TResult>>> expression);

    TResult InvokeCache<TResult>(Expression<Func<TResult>> expression, TimeSpan expiry, CacheAction cacheAction);
    Task<TResult> InvokeCacheAsync<TResult>(Expression<Func<Task<TResult>>> expression, TimeSpan expiry, CacheAction cacheAction);
}

public interface IDistributedCacheService : IExpressionCacheBase
{
    void Remove<TResult>(Expression<Func<TResult>> expression);
    void Remove<TResult>(Expression<Func<Task<TResult>>> expression);
    Task RemoveAsync<TResult>(Expression<Func<TResult>> expression);
    Task RemoveAsync<TResult>(Expression<Func<Task<TResult>>> expression);

    bool Exists<TResult>(Expression<Func<TResult>> expression);
    bool Exists<TResult>(Expression<Func<Task<TResult>>> expression);
    Task<bool> ExistsAsync<TResult>(Expression<Func<TResult>> expression);
    Task<bool> ExistsAsync<TResult>(Expression<Func<Task<TResult>>> expression);

    TResult Get<TResult>(Expression<Func<TResult>> expression);
    TResult Get<TResult>(Expression<Func<Task<TResult>>> expression);
    Task<TResult> GetAsync<TResult>(Expression<Func<TResult>> expression);
    Task<TResult> GetAsync<TResult>(Expression<Func<Task<TResult>>> expression);

    Task<List<TResult>> GetManyAsync<TResult>(IEnumerable<Expression<Func<TResult>>> expressions);
    Task<List<TResult>> GetManyAsync<TResult>(IEnumerable<Expression<Func<Task<TResult>>>> expressions);

    void Set<TResult, TValue>(Expression<Func<TResult>> expression, TValue value, TimeSpan expiry);
    void Set<TResult, TValue>(Expression<Func<Task<TResult>>> expression, TValue value, TimeSpan expiry);
    Task SetAsync<TResult, TValue>(Expression<Func<TResult>> expression, TValue value, TimeSpan expiry);
    Task SetAsync<TResult, TValue>(Expression<Func<Task<TResult>>> expression, TValue value, TimeSpan expiry);
}
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
4.0.1 232 10/18/2023
4.0.0 105 10/17/2023
3.1.0 554 4/14/2022
3.0.0 387 1/20/2021
2.0.0 412 11/3/2020
1.0.0 2,882 6/24/2018