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
                    
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="DNV.Dapr.Caching" Version="2.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DNV.Dapr.Caching" Version="2.2.0" />
                    
Directory.Packages.props
<PackageReference Include="DNV.Dapr.Caching" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DNV.Dapr.Caching --version 2.2.0
                    
#r "nuget: DNV.Dapr.Caching, 2.2.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.
#:package DNV.Dapr.Caching@2.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DNV.Dapr.Caching&version=2.2.0
                    
Install as a Cake Addin
#tool nuget:?package=DNV.Dapr.Caching&version=2.2.0
                    
Install as a Cake Tool

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

  • GetAsync(string key) - Retrieve cached item
  • SetAsync(string key, byte[] value, DistributedCacheEntryOptions options) - Store item with expiration
  • RefreshAsync(string key) - Refresh sliding expiration
  • RemoveAsync(string key) - Remove cached item

Synchronous Operations

  • Synchronous methods (Get, Set, Refresh, Remove) throw NotImplementedException
  • Use async methods for all operations

Expiration Behavior

Absolute Expiration

Items expire at a specific time, calculated from:

  • AbsoluteExpirationRelativeToNow: Relative to current time
  • AbsoluteExpiration: 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 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. 
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
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