Tenon.Caching.Interceptor.Castle 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.Caching.Interceptor.Castle.
dotnet add package Tenon.Caching.Interceptor.Castle --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.Caching.Interceptor.Castle -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.Interceptor.Castle" 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.Interceptor.Castle --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.Caching.Interceptor.Castle, 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.Interceptor.Castle as a Cake Addin
#addin nuget:?package=Tenon.Caching.Interceptor.Castle&version=0.0.1-alpha-202502101554&prerelease

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

Tenon.Caching.Interceptor.Castle

NuGet version License: MIT

基于 Castle DynamicProxy 的缓存拦截器实现,提供 Cache-Aside 模式和延时双删策略的 AOP 缓存解决方案。

✨ 功能特性

  • 🚀 基于 Castle DynamicProxy 的轻量级实现
  • 🔧 支持同步和异步方法拦截
  • 💉 Cache-Aside 缓存模式
  • 🎯 延时双删策略
  • 🔄 失败补偿机制
  • 📊 灵活的缓存键生成器
  • 🛡️ 完整的异常处理

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.Caching.Interceptor.Castle

🚀 快速入门

1. 注册服务

services.AddCachingInterceptor(options =>
{
    // 配置延时双删时间间隔(毫秒)
    options.DelayDeleteMilliseconds = 500;
    
    // 配置失败重试次数
    options.RetryCount = 3;
    
    // 配置重试间隔(毫秒)
    options.RetryIntervalMilliseconds = 200;
});

2. 使用缓存特性

public interface IProductService
{
    Task<Product> GetProductAsync(int id);
    Task UpdateProductAsync(Product product);
}

public class ProductService : IProductService
{
    [CachingAbl(ExpirationInSec = 3600)] // 缓存1小时
    public async Task<Product> GetProductAsync(int id)
    {
        // 从数据库获取商品
        return await _repository.GetByIdAsync(id);
    }

    [CachingEvict] // 更新时清除缓存
    public async Task UpdateProductAsync(Product product)
    {
        await _repository.UpdateAsync(product);
    }
}

3. 配置缓存参数

public class OrderService
{
    [CachingAbl(ExpirationInSec = 1800)] // 缓存30分钟
    public async Task<Order> GetOrderAsync(
        [CachingParameter(Name = "orderId")] string id,
        [CachingParameter(Ignore = true)] string userId)
    {
        return await _repository.GetOrderAsync(id);
    }
}

📖 高级用法

自定义缓存键生成器

public class CustomCacheKeyGenerator : ICacheKeyGenerator
{
    public string Generate(InvocationMetadata metadata)
    {
        var methodInfo = metadata.Method;
        var parameters = metadata.Parameters;
        
        // 自定义缓存键生成逻辑
        var key = $"{methodInfo.DeclaringType?.Name}:{methodInfo.Name}";
        
        foreach (var param in parameters)
        {
            if (!param.Ignore)
            {
                key += $":{param.Name}={param.Value}";
            }
        }
        
        return key;
    }
}

// 注册自定义生成器
services.AddSingleton<ICacheKeyGenerator, CustomCacheKeyGenerator>();

处理缓存异常

public class ProductService
{
    [CachingAbl(ExpirationInSec = 3600)]
    public async Task<Product> GetProductWithRetryAsync(int id)
    {
        try
        {
            return await _repository.GetByIdAsync(id);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取商品信息失败");
            throw new CachingAblException(
                "获取商品信息失败", 
                ex);
        }
    }
}

⚙️ 特性说明

CachingAbl 特性

用于标记需要缓存的方法:

[CachingAbl(ExpirationInSec = 3600)] // 缓存1小时
public async Task<T> GetDataAsync<T>(string key)
{
    return await _repository.GetAsync<T>(key);
}

CachingEvict 特性

用于标记需要清除缓存的方法:

[CachingEvict]
public async Task UpdateDataAsync<T>(string key, T value)
{
    await _repository.UpdateAsync(key, value);
}

CachingParameter 特性

用于自定义缓存键参数:

public async Task<User> GetUserAsync(
    [CachingParameter(Name = "uid")] int userId,
    [CachingParameter(Ignore = true)] string trace)
{
    return await _repository.GetUserAsync(userId);
}

🔨 项目依赖

  • Castle.Core
  • Tenon.Caching.Abstractions
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Logging

📝 使用注意事项

1. 延时双删策略

  • 合理配置延时时间
  • 考虑数据一致性要求
  • 注意性能影响

2. 异常处理

  • 实现完整的异常处理机制
  • 配置合适的重试策略
  • 记录必要的错误日志

3. 最佳实践

  • 合理设置缓存过期时间
  • 避免缓存大对象
  • 正确处理并发情况

✅ 示例场景

1. 商品缓存

public class ProductService
{
    [CachingAbl(ExpirationInSec = 1800)]
    public async Task<List<Product>> GetHotProductsAsync(
        [CachingParameter] int categoryId,
        [CachingParameter] int limit)
    {
        return await _repository.GetHotProductsAsync(
            categoryId, 
            limit);
    }

    [CachingEvict]
    public async Task UpdateProductStockAsync(
        [CachingParameter] int productId,
        [CachingParameter] int stock)
    {
        await _repository.UpdateStockAsync(
            productId, 
            stock);
    }
}

2. 用户信息缓存

public class UserService
{
    [CachingAbl(ExpirationInSec = 3600)]
    public async Task<UserInfo> GetUserInfoAsync(
        [CachingParameter] int userId)
    {
        return await _repository.GetUserInfoAsync(userId);
    }

    [CachingEvict]
    public async Task UpdateUserProfileAsync(
        [CachingParameter] UserProfile profile)
    {
        await _repository.UpdateProfileAsync(profile);
    }
}

🤝 参与贡献

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

📄 开源协议

本项目采用 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

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
0.0.1-alpha-202502101554 39 2/10/2025
0.0.1-alpha-202502101448 36 2/10/2025
0.0.1-alpha-202502101434 44 2/10/2025
0.0.1-alpha-202501130258 39 1/13/2025
0.0.1-alpha-202412311524 73 12/31/2024
0.0.1-alpha-202412061617 60 12/6/2024
0.0.1-alpha-202412051527 56 12/5/2024
0.0.1-alpha-202412051432 50 12/5/2024
0.0.1-alpha-202412041445 60 12/4/2024
0.0.1-alpha-202412021409 47 12/2/2024
0.0.1-alpha-202411301019 55 11/30/2024
0.0.1-alpha-202411170525 53 11/17/2024
0.0.1-alpha-202411161308 54 11/16/2024
0.0.1-alpha-202411131604 61 11/13/2024
0.0.1-alpha-202411111439 70 11/11/2024
0.0.1-alpha-202411051434 54 11/5/2024
0.0.1-alpha-202410281339 57 10/28/2024
0.0.1-alpha-202410131500 59 10/13/2024
0.0.1-alpha-202407261457 68 7/26/2024
0.0.1-alpha-202407261325 60 7/26/2024
0.0.1-alpha-202406271301 66 6/27/2024
0.0.1-alpha-202406251508 68 6/25/2024
0.0.1-alpha-202406251310 60 6/25/2024
0.0.1-alpha-202406141611 55 6/14/2024
0.0.1-alpha-202406141550 64 6/14/2024
0.0.1-alpha-202406121515 63 6/12/2024
0.0.1-alpha-202406061553 69 6/6/2024
0.0.1-alpha-202406041519 60 6/4/2024
0.0.1-alpha-202406011613 67 6/1/2024
0.0.1-alpha-202406011238 62 6/1/2024
0.0.1-alpha-202405311458 55 5/31/2024
0.0.1-alpha-202405291213 68 5/29/2024
0.0.1-alpha-202405190458 66 5/19/2024
0.0.1-alpha-202405161229 57 5/16/2024
0.0.1-alpha-202405141510 65 5/14/2024
0.0.1-alpha-202405101323 65 5/10/2024
0.0.1-alpha-202405081356 70 5/8/2024
0.0.1-alpha-202405021337 32 5/2/2024
0.0.1-alpha-202405021336 29 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 49 5/1/2024
0.0.1-alpha-202404291541 63 4/29/2024
0.0.1-alpha-202404281218 64 4/28/2024