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

This is a prerelease version of Tenon.Caching.Interceptor.Castle.
There is a newer prerelease version of this package available.
See the version list below for details.
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-202502241449 52 2/24/2025
0.0.1-alpha-202502101554 60 2/10/2025
0.0.1-alpha-202502101448 55 2/10/2025
0.0.1-alpha-202502101434 68 2/10/2025
0.0.1-alpha-202501130258 47 1/13/2025
0.0.1-alpha-202412311524 74 12/31/2024
0.0.1-alpha-202412061617 61 12/6/2024
0.0.1-alpha-202412051527 57 12/5/2024
0.0.1-alpha-202412051432 51 12/5/2024
0.0.1-alpha-202412041445 61 12/4/2024
0.0.1-alpha-202412021409 50 12/2/2024
0.0.1-alpha-202411301019 56 11/30/2024
0.0.1-alpha-202411170525 54 11/17/2024
0.0.1-alpha-202411161308 55 11/16/2024
0.0.1-alpha-202411131604 62 11/13/2024
0.0.1-alpha-202411111439 71 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 60 10/13/2024
0.0.1-alpha-202407261457 69 7/26/2024
0.0.1-alpha-202407261325 61 7/26/2024
0.0.1-alpha-202406271301 67 6/27/2024
0.0.1-alpha-202406251508 69 6/25/2024
0.0.1-alpha-202406251310 61 6/25/2024
0.0.1-alpha-202406141611 56 6/14/2024
0.0.1-alpha-202406141550 65 6/14/2024
0.0.1-alpha-202406121515 64 6/12/2024
0.0.1-alpha-202406061553 70 6/6/2024
0.0.1-alpha-202406041519 61 6/4/2024
0.0.1-alpha-202406011613 68 6/1/2024
0.0.1-alpha-202406011238 63 6/1/2024
0.0.1-alpha-202405311458 57 5/31/2024
0.0.1-alpha-202405291213 69 5/29/2024
0.0.1-alpha-202405190458 67 5/19/2024
0.0.1-alpha-202405161229 58 5/16/2024
0.0.1-alpha-202405141510 66 5/14/2024
0.0.1-alpha-202405101323 66 5/10/2024
0.0.1-alpha-202405081356 71 5/8/2024
0.0.1-alpha-202405021337 33 5/2/2024
0.0.1-alpha-202405021336 30 5/2/2024
0.0.1-alpha-202405020452 46 5/2/2024
0.0.1-alpha-202405011443 50 5/1/2024
0.0.1-alpha-202404291541 65 4/29/2024
0.0.1-alpha-202404281218 65 4/28/2024