DNV.Dapr.Caching
2.2.0
dotnet add package DNV.Dapr.Caching --version 2.2.0
NuGet\Install-Package DNV.Dapr.Caching -Version 2.2.0
<PackageReference Include="DNV.Dapr.Caching" Version="2.2.0" />
<PackageVersion Include="DNV.Dapr.Caching" Version="2.2.0" />
<PackageReference Include="DNV.Dapr.Caching" />
paket add DNV.Dapr.Caching --version 2.2.0
#r "nuget: DNV.Dapr.Caching, 2.2.0"
#:package DNV.Dapr.Caching@2.2.0
#addin nuget:?package=DNV.Dapr.Caching&version=2.2.0
#tool nuget:?package=DNV.Dapr.Caching&version=2.2.0
DNV.Dapr.Caching
A .NET library that provides a Dapr-based implementation of Microsoft's IDistributedCache
interface, enabling distributed caching using Dapr's state store building blocks.
Overview
This library integrates Dapr's state management capabilities with .NET's distributed caching abstractions, allowing applications to use any Dapr-supported state store (Redis, Cosmos DB, SQL Server, etc.) as a distributed cache backend through a familiar interface.
Target Framework
- .NET 8.0
Features
- IDistributedCache Implementation: Full compatibility with Microsoft's distributed caching abstractions
- Dapr State Store Integration: Leverage any Dapr state store component as cache backend
- TTL Support: Automatic expiration handling using Dapr's TTL metadata
- Sliding Expiration: Support for sliding expiration windows that refresh on access
- Async Operations: Full async/await support with cancellation tokens
- Logging Integration: Built-in logging for cache operations
- Dependency Injection: Easy registration with .NET's DI container
Dependencies
Dapr.Client
Microsoft.Extensions.Caching.Abstractions
Microsoft.Extensions.Logging
Microsoft.Extensions.Options
Key Components
DaprCache
Implementation of IDistributedCache
that uses Dapr's state store APIs for caching operations.
DaprCacheOptions
Configuration options for the Dapr cache implementation:
ComponentName
: Name of the Dapr state store component to use
CacheItem
Internal record representing cached data with TTL and sliding expiration settings.
Usage
Basic Setup
// Register Dapr and the cache service
services.AddSingleton<DaprClient>(_ => new DaprClientBuilder().Build());
services.AddDaprCache(o => o.ComponentName = "statestore");
// Use IDistributedCache as normal
public class MyService
{
private readonly IDistributedCache _cache;
public MyService(IDistributedCache cache)
{
_cache = cache;
}
public Task<string?> GetDataAsync(string key)
{
return _cache.GetStringAsync(key);
}
public Task SetDataAsync(string key, string value)
{
return _cache.SetStringAsync(key, value, TimeSpan.FromMinutes (30));
}
}
Supported Operations
Async Operations (Recommended)
GetAsync(string key)
- Retrieve cached itemSetAsync(string key, byte[] value, DistributedCacheEntryOptions options)
- Store item with expirationRefreshAsync(string key)
- Refresh sliding expirationRemoveAsync(string key)
- Remove cached item
Synchronous Operations
- Synchronous methods (
Get
,Set
,Refresh
,Remove
) throwNotImplementedException
- Use async methods for all operations
Expiration Behavior
Absolute Expiration
Items expire at a specific time, calculated from:
AbsoluteExpirationRelativeToNow
: Relative to current timeAbsoluteExpiration
: Specific DateTimeOffset
Sliding Expiration
Items expire after a period of inactivity:
SlidingExpiration
: Resets expiration on each access- Automatically refreshes TTL when items are retrieved or refreshed
No Expiration
Items without expiration settings are stored indefinitely (TTL = -1).
Dapr Component Configuration
Ensure your Dapr state store component supports TTL metadata:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
- name: enableTLS
value: false
Logging
The cache implementation includes structured logging for all operations:
- Cache retrievals:
"Retrieving cache item: {key}"
- Cache storage:
"Saving cache item: {key}"
- Cache removal:
"Removing cache item: {key}"
- Cache refresh:
"Refreshing cache item: {key}"
Limitations
- Synchronous methods are not implemented (by design for async-first approach)
- TTL precision depends on the underlying Dapr state store component
- Cache size limits depend on the configured state store backend
Author
Eric Liu li.eric.liu@dnv.com
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Dapr.Client (>= 1.15.4)
- Microsoft.Extensions.Caching.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Options (>= 8.0.2)
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 |
---|---|---|
2.2.0 | 66 | 9/18/2025 |
2.1.0 | 297 | 2/8/2025 |
2.1.0-preview.1756698443 | 118 | 9/1/2025 |
2.1.0-preview.1756370241 | 160 | 8/28/2025 |
2.1.0-preview.1756358515 | 164 | 8/28/2025 |
2.1.0-preview.1756200584 | 179 | 8/26/2025 |
2.1.0-preview.1756199251 | 179 | 8/26/2025 |
2.1.0-preview.1755771145 | 114 | 8/21/2025 |
2.0.1 | 132 | 9/27/2024 |
1.1.0 | 149 | 8/9/2024 |