Common.Cache.Redis 1.3.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Common.Cache.Redis --version 1.3.2
                    
NuGet\Install-Package Common.Cache.Redis -Version 1.3.2
                    
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="Common.Cache.Redis" Version="1.3.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Common.Cache.Redis" Version="1.3.2" />
                    
Directory.Packages.props
<PackageReference Include="Common.Cache.Redis" />
                    
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 Common.Cache.Redis --version 1.3.2
                    
#r "nuget: Common.Cache.Redis, 1.3.2"
                    
#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 Common.Cache.Redis@1.3.2
                    
#: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=Common.Cache.Redis&version=1.3.2
                    
Install as a Cake Addin
#tool nuget:?package=Common.Cache.Redis&version=1.3.2
                    
Install as a Cake Tool

Common.Cache.Redis

这是一个基于 StackExchange.Redis 的 Redis 缓存封装库,提供了更加便捷的缓存操作接口。

功能特性

  • 基于 StackExchange.Redis 实现
  • 支持多种缓存操作方法
  • 支持缓存空值配置
  • 支持批量操作
  • 支持模糊匹配删除
  • 支持 Key 前缀配置
  • 支持多框架:.NET 6.0 / 7.0 / 8.0 / 9.0 / 10.0

安装

通过 NuGet 安装:

Install-Package Common.Cache.Redis

或通过 .NET CLI:

dotnet add package Common.Cache.Redis

使用方法

基本配置

在 Program.cs 中配置服务:

// 使用推荐的新方法
services.AddRedisCacheStore(options =>
{
    options.ConnectionString = "localhost:6379,password=123456,DefaultDatabase=0";
    options.KeyPrefix = "myapp";
    options.CacheEmptyCollections = true; // 是否缓存空集合和空字符串数据
});

// 或使用旧方法(已过时)
services.AddRedisCacheService(options =>
{
    options.ConnectionString = "localhost:6379,password=123456,DefaultDatabase=0";
    options.KeyPrefix = "myapp";
    options.CacheEmptyCollections = true; // 是否缓存空集合和空字符串数据
});

在服务中使用

注入 ICacheProviderIRedisProvider 接口并在代码中使用:

public class MyService
{
    private readonly ICacheProvider _cacheProvider;

    public MyService(ICacheProvider cacheProvider)
    {
        _cacheProvider = cacheProvider;
    }

    public async Task<string> GetDataAsync(string key)
    {
        // 获取缓存
        var cachedValue = await _cacheProvider.GetAsync<string>(key);
        if (cachedValue != null)
        {
            return cachedValue;
        }

        // 获取实际数据
        var data = await GetDataFromDatabase(key);

        // 设置缓存
        await _cacheProvider.SetAsync(key, data, TimeSpan.FromMinutes(10));

        return data;
    }

    public async Task<T> GetOrCreateDataAsync<T>(string key, Func<Task<T>> factory)
    {
        // 获取或创建缓存
        return await _cacheProvider.GetOrCreateAsync(key, factory, TimeSpan.FromMinutes(10));
    }
}

批量操作

// 批量删除缓存
var keysToDelete = new[] { "key1", "key2", "key3" };
await _cacheProvider.RemoveAsync(keysToDelete);

// 模糊匹配删除
await _cacheProvider.RemoveMatchKeyAsync("user_*");

配置选项

RedisConfig 类提供了以下配置选项:

  • ConnectionString: Redis 连接字符串
  • KeyPrefix: Key 前缀
  • InitErrorIntervalSecond: 初始化错误间隔时间(秒)
  • CacheEmptyCollections: 是否缓存空集合和空字符串数据(默认为true)

模糊匹配规则

模糊匹配支持以下通配符:

  • * 表示可以匹配多个任意字符
  • ? 表示可以匹配单个任意字符
  • [] 表示可以匹配指定范围内的字符

例如:

  • user_* 匹配所有以 "user_" 开头的键
  • user_? 匹配类似 "user_a", "user_1" 这样的键
  • user_[1-9] 匹配 user_1 到 user_9 的键

版本更新记录

  • 1.3.2
    • 更新GetOrCreateAsync方法
  • 1.3.1
    • 更新异常信息输出
  • 1.3.0
    • 更新正式包
  • 1.2.0-beta9
    • 引用.Net10正式包
  • 1.2.0-beta8
    • 适配.net10
  • 1.2.0-beta7
    • 设置不缓存空值的时候问题修复
  • 1.2.0-beta6
    • 增加可设置是否存储空字符串或者空集合选项,默认存储
  • 1.2.0-beta5
    • 修复GetOrCreateAsync读取不到缓存还存储redis的问题
  • 1.2.0-beta4
    • 支持.Net9
    • 增加扩展方法AddRedisCacheStore
  • 1.2.0-beta-3
    • 修改方法KeyDeleteInBatchAsync为RemoveMatchKeyAsync
  • 1.2.0-beta2
    • 依赖基类包:Azrng.Cache.Core
    • 优化代码
  • 1.2.0-beta1
    • 支持netstandard2.1;net6.0;net7.0;net8.0
    • 将公共的缓存接口定义封装
  • 1.1.1
    • 修改redis操作管理类
  • 1.1.0
    • 更新版本为5.0
  • 1.0.0
    • 3.1版本的redis公共库
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 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 is compatible.  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 is compatible.  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
1.4.0 103 1/29/2026
1.3.2 100 1/27/2026
1.3.1 296 12/17/2025
1.3.0 379 11/30/2025
1.2.0-beta9 301 11/12/2025
1.2.0-beta8 168 11/6/2025
1.2.0-beta7 281 10/9/2025
1.2.0-beta6 179 10/8/2025
1.2.0-beta5 289 8/28/2025
1.2.0-beta4 197 7/14/2025
1.2.0-beta3 182 6/5/2024
1.2.0-beta2 196 4/4/2024
1.2.0-beta1 255 12/28/2023
1.1.0 2,386 11/11/2020
1.0.0 603 10/9/2020