Skip to main content
net10.0net5.0net6.0net7.0net8.0net9.0

Redis依赖注入扩展,支持IDistributedCache分布式缓存接口和IDataProtection数据保护,简化ASP.NET Core集成

Published
Oct 19, 2026
2d ago
Total Downloads
60.7K
60,678

Install

dotnet add package NewLife.Redis.Extensions --version 6.6.2026.1019-beta1511
ProductVersions
.NET
net10.0net5.0net6.0net7.0net8.0net9.0
.NET Standard
netstandard2.0netstandard2.1
Other
.NETCoreApp3.1

README

NewLife.Redis.Extensions

Nuget Nuget Downloads

ASP.NET Core Redis Deep Integration Extensions

NewLife.Redis.Extensions is the official ASP.NET Core integration package for NewLife.Redis, providing out-of-the-box support for IDistributedCache distributed caching, IDataProtection key persistence, and automatic DI registration.

🆕 This is the successor to the deprecated NewLife.Extensions.Caching.Redis (v5.5). If you are using the old package, see the Migration Guide.


Feature Overview

Feature Description
IDistributedCache ASP.NET Core standard distributed cache interface
IDataProtection Persist data protection keys to Redis (PersistKeysToRedis)
DI Integration Single AddDistributedRedisCache call registers all services
Inherits FullRedis RedisCache inherits FullRedis — full data structure access
Broad Framework Coverage netcoreapp3.1 / net5.0 ~ net10.0 / netstandard2.0 / netstandard2.1

Installation

dotnet add package NewLife.Redis.Extensions

Quick Start

Register Services

using NewLife.Redis.Extensions;

var builder = WebApplication.CreateBuilder(args);

// One-liner: registers FullRedis / IDistributedCache / ICache / ICacheProvider
builder.Services.AddDistributedRedisCache(options =>
{
    options.Server   = "127.0.0.1:6379";
    options.Password = "pass";
    options.Db       = 0;
    options.Prefix   = "myapp:";
});

Using IDistributedCache

public class HomeController : Controller
{
    private readonly IDistributedCache _cache;

    public HomeController(IDistributedCache cache)
    {
        _cache = cache;
    }

    public async Task<IActionResult> Index()
    {
        var value = await _cache.GetStringAsync("greeting");
        if (value == null)
        {
            value = "Hello, Redis!";
            await _cache.SetStringAsync("greeting", value,
                new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1) });
        }
        return Content(value);
    }
}

Using FullRedis for Complete Redis Access

Since RedisCache inherits from FullRedis, you can use all Redis data structures directly after injection:

public class UserService
{
    private readonly FullRedis _redis;

    public UserService(FullRedis redis)
    {
        _redis = redis;
    }

    public void SaveUser(User user)
    {
        var list = _redis.GetList<User>("users");
        list.Add(user);

        var hash = _redis.GetDictionary<String>("user:" + user.Id);
        hash["Name"]  = user.Name;
        hash["Email"] = user.Email;
    }
}

Data Protection Key Persistence

builder.Services.AddDataProtection()
    .PersistKeysToRedis(redis, "DataProtection-Keys");

For more Redis operations, see the NewLife.Redis main documentation.


Migrating from the Old Package

This is the direct successor to NewLife.Extensions.Caching.Redis (v5.5), offering better architecture and more features:

Comparison NewLife.Extensions.Caching.Redis (Old) NewLife.Redis.Extensions (New)
Version Series v5.5 v6.5+
IDistributedCache ✅ ✅
IDataProtection Persistence ❌ ✅
Framework Coverage net461; netstandard2.0; netstandard2.1 netcoreapp3.1 ~ net10.0; netstandard2.0/2.1
RedisCache Base Class Standalone class, wraps FullRedis Inherits FullRedis, full API available
Namespace NewLife.Extensions.Caching.Redis NewLife.Redis.Extensions

Migration Steps:

  1. Uninstall the old package, install the new one
  2. Change namespace: NewLife.Extensions.Caching.Redis → NewLife.Redis.Extensions
  3. Switch DI registration: services.AddRedis(configString) → services.AddDistributedRedisCache(options => {...})

Related Links

Changes from Previous Version

No structural changes between v6.6.2026.1019-beta0424 and v6.6.2026.1019-beta1511.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 9 target frameworks; confirm the exact target used by your application.The published metadata declares 49 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.