ExpressionCache.Distributed
4.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package ExpressionCache.Distributed --version 4.0.0
NuGet\Install-Package ExpressionCache.Distributed -Version 4.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.Distributed" Version="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExpressionCache.Distributed --version 4.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ExpressionCache.Distributed, 4.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.Distributed as a Cake Addin #addin nuget:?package=ExpressionCache.Distributed&version=4.0.0 // Install ExpressionCache.Distributed as a Cake Tool #tool nuget:?package=ExpressionCache.Distributed&version=4.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- ExpressionCache.Core (>= 4.0.0)
- Microsoft.Extensions.Caching.Abstractions (>= 6.0.0)
- System.Text.Json (>= 6.0.8)
-
net7.0
- ExpressionCache.Core (>= 4.0.0)
- Microsoft.Extensions.Caching.Abstractions (>= 7.0.0)
- System.Text.Json (>= 7.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.