Tenon.Caching.Abstractions 0.0.1-alpha-202502241449

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

Tenon.Caching.Abstractions

NuGet version License: MIT

Tenon.Caching.Abstractions 提供了统一的缓存抽象接口定义,是 Tenon 框架缓存功能的核心基础。通过抽象接口设计,实现了缓存提供者的可插拔性和一致性。

✨ 设计优势

  • 🎯 统一抽象:提供统一的 ICacheProvider 接口,确保不同缓存实现的一致性
  • 🔌 可插拔性:支持多种缓存实现无缝切换,无需修改业务代码
  • 💡 简洁接口:精心设计的 API 接口,易于使用和扩展
  • 🛡️ 类型安全:泛型设计确保类型安全,避免运行时类型错误
  • 🔄 异步支持:全面支持异步操作,提升性能
  • 📦 批量操作:支持批量缓存操作,提高效率
  • 高性能:优化的缓存值包装器,最小化性能开销

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.Caching.Abstractions

🚀 核心接口

ICacheProvider

提供统一的缓存操作接口:

public interface ICacheProvider
{
    // 设置缓存
    bool Set<T>(string cacheKey, T cacheValue, TimeSpan expiration);
    Task<bool> SetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration);
    
    // 获取缓存
    CacheValue<T> Get<T>(string cacheKey);
    Task<CacheValue<T>> GetAsync<T>(string cacheKey);
    
    // 删除缓存
    bool Remove(string cacheKey);
    Task<bool> RemoveAsync(string cacheKey);
    
    // 检查缓存是否存在
    bool Exists(string cacheKey);
    Task<bool> ExistsAsync(string cacheKey);
    
    // 批量操作
    long RemoveAll(IEnumerable<string> cacheKeys);
    Task<long> RemoveAllAsync(IEnumerable<string> cacheKeys);
    
    // 过期设置
    Task KeysExpireAsync(IEnumerable<string> cacheKeys);
    Task KeysExpireAsync(IEnumerable<string> cacheKeys, TimeSpan expiration);
}

CacheValue<T>

优化的缓存值包装器:

public readonly struct CacheValue<T>
{
    public bool HasValue { get; }
    public bool IsNull { get; }
    public T Value { get; }
    
    public static CacheValue<T> Null { get; }
    public static CacheValue<T> NoValue { get; }
}

📚 缓存实现

Tenon 框架提供了多种缓存实现,都基于此抽象接口:

1. 内存缓存

Tenon.Caching.InMemory

  • 基于 System.Runtime.Caching
  • 适用于单机部署场景
  • 高性能、低延迟

2. Redis 缓存

Tenon.Caching.Redis

  • Redis 缓存抽象实现
  • 支持多种 Redis 客户端
  • 分布式缓存基础

3. StackExchange.Redis 实现

Tenon.Caching.RedisStackExchange

  • 基于 StackExchange.Redis
  • 企业级分布式缓存方案
  • 高性能、高可用

4. Castle 拦截器

Tenon.Caching.Interceptor.Castle

  • AOP 缓存实现
  • Cache-Aside 模式
  • 延时双删策略

🎯 使用示例

1. 基础用法

public class UserService
{
    private readonly ICacheProvider _cache;
    
    public UserService(ICacheProvider cache)
    {
        _cache = cache;
    }
    
    public async Task<User> GetUserAsync(int userId)
    {
        var cacheKey = $"user:{userId}";
        
        // 尝试获取缓存
        var cacheValue = await _cache.GetAsync<User>(cacheKey);
        if (cacheValue.HasValue)
            return cacheValue.Value;
            
        // 从数据源获取
        var user = await _repository.GetUserAsync(userId);
        
        // 设置缓存
        await _cache.SetAsync(cacheKey, user, TimeSpan.FromHours(1));
        
        return user;
    }
}

2. 批量操作

public class ProductService
{
    private readonly ICacheProvider _cache;
    
    public async Task UpdateProductsAsync(List<Product> products)
    {
        // 更新数据
        await _repository.UpdateProductsAsync(products);
        
        // 批量清除缓存
        var cacheKeys = products.Select(p => $"product:{p.Id}");
        await _cache.RemoveAllAsync(cacheKeys);
    }
}

⚙️ 最佳实践

1. 缓存键设计

public static class CacheKeys
{
    private const string Prefix = "app:";
    
    public static string GetUserKey(int userId) 
        => $"{Prefix}user:{userId}";
        
    public static string GetProductKey(int productId)
        => $"{Prefix}product:{productId}";
}

2. 异常处理

public async Task<User> GetUserWithRetryAsync(int userId)
{
    try
    {
        var cacheValue = await _cache.GetAsync<User>(
            CacheKeys.GetUserKey(userId));
            
        return cacheValue.HasValue 
            ? cacheValue.Value 
            : await GetFromSourceAsync(userId);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "缓存操作失败");
        return await GetFromSourceAsync(userId);
    }
}

🔨 项目依赖

  • Microsoft.Extensions.DependencyInjection.Abstractions
  • System.Threading.Tasks

📝 使用注意事项

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.  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 (4)

Showing the top 4 NuGet packages that depend on Tenon.Caching.Abstractions:

Package Downloads
Tenon.Caching.Redis

Package Description

Tenon.Caching.Interceptor.Castle

Package Description

Tenon.Caching.InMemory

Package Description

Tenon.Hangfire.Extensions

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1-alpha-202502241449 76 2/24/2025
0.0.1-alpha-202502101554 79 2/10/2025
0.0.1-alpha-202502101448 78 2/10/2025
0.0.1-alpha-202502101434 88 2/10/2025
0.0.1-alpha-202501130258 57 1/13/2025
0.0.1-alpha-202412311524 91 12/31/2024
0.0.1-alpha-202412061617 68 12/6/2024
0.0.1-alpha-202412051527 71 12/5/2024
0.0.1-alpha-202412051432 70 12/5/2024
0.0.1-alpha-202412041445 64 12/4/2024
0.0.1-alpha-202412021409 66 12/2/2024
0.0.1-alpha-202411301019 70 11/30/2024
0.0.1-alpha-202411170525 65 11/17/2024
0.0.1-alpha-202411161308 65 11/16/2024
0.0.1-alpha-202411131604 67 11/13/2024
0.0.1-alpha-202411111439 76 11/11/2024
0.0.1-alpha-202411051434 64 11/5/2024
0.0.1-alpha-202410281339 69 10/28/2024
0.0.1-alpha-202410131500 69 10/13/2024
0.0.1-alpha-202407261457 78 7/26/2024
0.0.1-alpha-202407261325 68 7/26/2024
0.0.1-alpha-202406271301 75 6/27/2024
0.0.1-alpha-202406251508 66 6/25/2024
0.0.1-alpha-202406251310 72 6/25/2024
0.0.1-alpha-202406141611 73 6/14/2024
0.0.1-alpha-202406141550 66 6/14/2024
0.0.1-alpha-202406121515 70 6/12/2024
0.0.1-alpha-202406061553 77 6/6/2024
0.0.1-alpha-202406041519 70 6/4/2024
0.0.1-alpha-202406011613 77 6/1/2024
0.0.1-alpha-202406011238 73 6/1/2024
0.0.1-alpha-202405311458 64 5/31/2024
0.0.1-alpha-202405291213 81 5/29/2024
0.0.1-alpha-202405190458 76 5/19/2024
0.0.1-alpha-202405161229 63 5/16/2024
0.0.1-alpha-202405141510 71 5/14/2024
0.0.1-alpha-202405101323 77 5/10/2024
0.0.1-alpha-202405081356 76 5/8/2024
0.0.1-alpha-202405021337 39 5/2/2024
0.0.1-alpha-202405021336 39 5/2/2024
0.0.1-alpha-202405020452 56 5/2/2024
0.0.1-alpha-202405011443 60 5/1/2024
0.0.1-alpha-202404291541 69 4/29/2024
0.0.1-alpha-202404281218 71 4/28/2024