Yzl.Extensions.Cache 0.1.0

dotnet add package Yzl.Extensions.Cache --version 0.1.0
                    
NuGet\Install-Package Yzl.Extensions.Cache -Version 0.1.0
                    
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="Yzl.Extensions.Cache" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Yzl.Extensions.Cache" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Yzl.Extensions.Cache" />
                    
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 Yzl.Extensions.Cache --version 0.1.0
                    
#r "nuget: Yzl.Extensions.Cache, 0.1.0"
                    
#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 Yzl.Extensions.Cache@0.1.0
                    
#: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=Yzl.Extensions.Cache&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Yzl.Extensions.Cache&version=0.1.0
                    
Install as a Cake Tool

Yzl.Extensions.Cache 总体架构设计

Yzl.Extensions.Cache 是一个 基于 AOP / 动态代理的 .NET 缓存基础设施组件,主要目标:

在 .NET 世界中提供 类似 Spring Cache(@Cacheable) 的声明式缓存能力

  • 保持业务代码 零侵入
  • 支持 本地缓存 + Redis 缓存 灵活切换
  • 与 Feign / Actuator / IOC 体系无缝协作
  • 适用于企业级服务与长期演进

等价于 Java 世界里的:

<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

使用步骤

  1. 在Program中注册缓存框架

using Yzl.Extensions.Cache;
using Yzl.Extensions.Common.Ioc;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddBatchServices();

// Add services to the container.
builder.Services.AddControllersWithViews();

// ===== Yzl.Extensions.Cache  =====
builder.Services.AddEnableCaching(assemblies: null, enableRedis: true, redisConnectionString: builder.Configuration["redis:main-site"]);

var app = builder.Build();

app.MapGet("/", () => "Hello .NET 8 + Caching!");

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

  1. 注册参数说明

/// <summary>
/// 注册缓存拦截器
/// </summary>
/// <param name="services"></param>
/// <param name="assemblies">需要注册的程序集</param>
/// <param name="enableRedis">是否启用redis缓存</param>
/// <param name="redisConnectionString">redis连接字符串</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IServiceCollection AddCachingWithInterceptors(
this IServiceCollection services,
Assembly[]? assemblies= null,
bool enableRedis = false,
string? redisConnectionString = null)

  1. 使用方式

1. 方法必须为虚方法(virtual)
2. 方法上使用[Cacheable(cacheName:"a:bb:c",key:"#settingQo.UserId:#settingQo.key",cacheType:CacheType.Redis)]特性标记

  1. 完整demo

using feign.net.test.Acs;
using feign.net.test.Entity;
using feign.net.test.Entity.Qo;
using Yzl.Extensions.Cache.Attributes;
using Yzl.Extensions.Cache.Enums;
using Spring.Net.Feign.Ioc.Attributes;

namespace feign.net.test.Service;

[IocService]
public class UgcService(IUgcApiService apiService)
{

    // [Cacheable(cacheName: "a:b:c", key: "4:#userId", cacheType: CacheType.Redis)]
    // [Cacheable(cacheName:"a:b:c",key: "4", cacheType: CacheType.Redis)]
    [Cacheable(key: "3:#userId")]
    public virtual string? GetZujuanUserPointInfo(int userId)
    {
        Thread.Sleep(2000);

        return apiService.GetZujuanUserPointInfo(userId);
    }
    
    public virtual string? GetZujuanUserPointInfoNoCache(int userId)
    {
        return apiService.GetZujuanUserPointInfo(userId);
    }

    
    // [Cacheable(cacheName:"a:b",key:"2:#dic.userId")] // 测试SpEl表达式获取字典里的key
    [Cacheable(key:"2:#dic.userId")] // 测试cacheName为空时,使用方法全限定名
    public virtual ResponseResult<Dictionary<string, object>> CheckRoles(Dictionary<string, object> dic)
    {
        Thread.Sleep(2000);
        
        return apiService.CheckRoles(dic);
    }
    
    public virtual ResponseResult<Dictionary<string, object>> CheckRolesNoCache(Dictionary<string, object> dic)
    {
        return apiService.CheckRoles(dic);
    }

    // [Cacheable(key:"#settingQo.UserId:#settingQo.key",cacheType:CacheType.Redis)] // 测试SpEl表达式获取参数里的key(优先获取JsonProperty)
    // [Cacheable(key:"#settingQo.UserId:#settingQo.key",cacheType:CacheType.Redis)] // 不写CacheName,默认使用方法全限定名(feign.net.test.Service.UgcService.GetUserSettingObj::28759906:grade)
    [Cacheable(cacheName:"a:bb:c",key:"#settingQo.UserId:#settingQo.key",cacheType:CacheType.Redis)] // 写CacheName,key全路径则为:a:bb:c::28759906:grade
    public virtual ZuJuanUserSettingDo? GetUserSettingObj(UserSettingQo settingQo)
    {
        Thread.Sleep(2000);
        
        return apiService.GetUserSettingObj(settingQo);
    }
}

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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

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.1.0 116 3/13/2026