Tenon.Caching.Redis 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.Caching.Redis.
dotnet add package Tenon.Caching.Redis --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.Caching.Redis -Version 0.0.1-alpha-202502101554                
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="Tenon.Caching.Redis" Version="0.0.1-alpha-202502101554" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.Caching.Redis --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.Caching.Redis, 0.0.1-alpha-202502101554"                
#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 Tenon.Caching.Redis as a Cake Addin
#addin nuget:?package=Tenon.Caching.Redis&version=0.0.1-alpha-202502101554&prerelease

// Install Tenon.Caching.Redis as a Cake Tool
#tool nuget:?package=Tenon.Caching.Redis&version=0.0.1-alpha-202502101554&prerelease                

Tenon.Caching.Redis

NuGet version License: MIT

Tenon.Caching.Redis 是一个 Redis 缓存抽象层实现,为 Tenon 框架提供统一的 Redis 缓存接口和基础实现。

✨ 功能特性

  • 🚀 轻量级 Redis 缓存抽象实现
  • 🔧 统一的缓存接口定义
  • 💉 支持多种 Redis 客户端实现
  • 🎯 完整的缓存操作支持
  • 🔄 灵活的序列化选项
  • 📊 可扩展的缓存提供者
  • 🛡️ 内置异常处理机制

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.Caching.Redis

🚀 快速入门

1. 实现缓存提供者

public class CustomRedisCacheProvider : RedisCacheProvider
{
    public CustomRedisCacheProvider(
        IRedisProvider redisProvider, 
        ISerializer serializer,
        CachingOptions options) 
        : base(redisProvider, serializer, options)
    {
    }

    // 可以在这里扩展或重写基类方法
    public override T Get<T>(string key)
    {
        // 自定义实现
        return base.Get<T>(key);
    }
}

2. 注册服务

services.AddSingleton<ICacheProvider>(sp => 
{
    var redisProvider = sp.GetRequiredService<IRedisProvider>();
    var serializer = sp.GetRequiredService<ISerializer>();
    var options = new CachingOptions();
    
    return new CustomRedisCacheProvider(
        redisProvider, 
        serializer,
        options);
});

3. 使用缓存服务

public class CacheService
{
    private readonly ICacheProvider _cache;

    public CacheService(ICacheProvider cache)
    {
        _cache = cache;
    }

    public T GetOrCreate<T>(string key, Func<T> factory, TimeSpan expiry)
    {
        if (_cache.TryGet(key, out T? value))
            return value;

        value = factory();
        _cache.Set(key, value, expiry);
        return value;
    }
}

📖 高级用法

自定义序列化

public class CustomSerializer : ISerializer
{
    public string Serialize<T>(T value)
    {
        // 自定义序列化实现
    }

    public T Deserialize<T>(string value)
    {
        // 自定义反序列化实现
    }
}

// 注册自定义序列化器
services.AddSingleton<ISerializer, CustomSerializer>();

缓存键管理

public class CacheKeyManager
{
    private const string Prefix = "app:";
    
    public static string BuildKey(string module, string entity, string id)
        => $"{Prefix}{module}:{entity}:{id}";
        
    public static string BuildKey<T>(string id)
        => $"{Prefix}{typeof(T).Name.ToLower()}:{id}";
}

⚙️ 接口说明

ICacheProvider

public interface ICacheProvider
{
    // 获取缓存值
    T Get<T>(string key);
    
    // 尝试获取缓存值
    bool TryGet<T>(string key, out T value);
    
    // 设置缓存
    void Set<T>(string key, T value, TimeSpan expiry);
    
    // 移除缓存
    void Remove(string key);
    
    // 清空缓存
    void Clear();
}

RedisCacheProvider

基础 Redis 缓存提供者实现,包含:

  • 基础缓存操作实现
  • 序列化与反序列化处理
  • 异常处理和重试机制
  • 缓存键前缀管理
  • 过期时间处理

🔨 项目依赖

  • Tenon.Caching.Abstractions
  • Tenon.Infra.Redis
  • Tenon.Serialization.Abstractions
  • Microsoft.Extensions.DependencyInjection.Abstractions

📝 使用注意事项

1. 序列化处理

  • 选择合适的序列化方案
  • 注意序列化性能影响
  • 处理特殊类型序列化

2. 缓存操作

  • 合理设置过期时间
  • 注意并发操作处理
  • 实现缓存预热机制

3. 最佳实践

  • 统一缓存键管理
  • 实现缓存监控
  • 做好异常处理

🤝 参与贡献

欢迎参与项目贡献!请阅读我们的贡献指南了解如何参与项目开发。

📄 开源协议

本项目采用 MIT 开源协议 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tenon.Caching.Redis:

Package Downloads
Tenon.Caching.RedisStackExchange

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.1-alpha-202502101554 40 2/10/2025
0.0.1-alpha-202502101448 38 2/10/2025
0.0.1-alpha-202502101434 37 2/10/2025
0.0.1-alpha-202501130258 41 1/13/2025
0.0.1-alpha-202412311524 72 12/31/2024
0.0.1-alpha-202412061617 62 12/6/2024
0.0.1-alpha-202412051527 55 12/5/2024
0.0.1-alpha-202412051431 51 12/5/2024
0.0.1-alpha-202412041445 53 12/4/2024
0.0.1-alpha-202412021409 52 12/2/2024
0.0.1-alpha-202411301019 59 11/30/2024
0.0.1-alpha-202411170525 54 11/17/2024
0.0.1-alpha-202411161308 51 11/16/2024
0.0.1-alpha-202411131604 54 11/13/2024
0.0.1-alpha-202411111439 67 11/11/2024
0.0.1-alpha-202411051434 55 11/5/2024
0.0.1-alpha-202410281339 58 10/28/2024
0.0.1-alpha-202410131500 63 10/13/2024
0.0.1-alpha-202407261457 73 7/26/2024
0.0.1-alpha-202407261325 60 7/26/2024
0.0.1-alpha-202406271301 71 6/27/2024
0.0.1-alpha-202406251508 65 6/25/2024
0.0.1-alpha-202406251310 59 6/25/2024
0.0.1-alpha-202406141611 62 6/14/2024
0.0.1-alpha-202406141550 64 6/14/2024
0.0.1-alpha-202406121515 58 6/12/2024
0.0.1-alpha-202406061553 65 6/6/2024
0.0.1-alpha-202406041519 59 6/4/2024
0.0.1-alpha-202406011613 64 6/1/2024
0.0.1-alpha-202406011238 60 6/1/2024
0.0.1-alpha-202405311458 58 5/31/2024
0.0.1-alpha-202405291213 65 5/29/2024
0.0.1-alpha-202405190457 63 5/19/2024
0.0.1-alpha-202405161229 58 5/16/2024
0.0.1-alpha-202405141510 60 5/14/2024
0.0.1-alpha-202405101323 64 5/10/2024
0.0.1-alpha-202405081356 68 5/8/2024
0.0.1-alpha-202405021337 29 5/2/2024
0.0.1-alpha-202405021336 28 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 59 5/1/2024
0.0.1-alpha-202404291541 62 4/29/2024
0.0.1-alpha-202404281218 64 4/28/2024