幽灵按钮网站,网站建设技能培训,国家企业信息认证系统,黑龙江新闻夜航通过Aop内存缓存对接口、方法进行缓存
源码地址https://gitee.com/wangbenchi66/nuget
1. nuget包引入
必须引入包 至少在2024.11.7以上
PackageReference IncludeWBC66.Cache.Core Version2024.11.7 /必须开启内存缓存 否则后续步骤无法正…通过Aop内存缓存对接口、方法进行缓存
源码地址https://gitee.com/wangbenchi66/nuget
1. nuget包引入
必须引入包 至少在2024.11.7以上
PackageReference IncludeWBC66.Cache.Core Version2024.11.7 /必须开启内存缓存 否则后续步骤无法正常进行
//开启内存缓存
builder.Services.AddMemoryCacheSetup();2. 对方法使用内存缓存
//使用autofac(内部会自动进行程序集扫描注入,不需要手动注入)
builder.Host.AddAutofacHostSetup(builder.Services, options
{//开启内存缓存拦截器(带有IProxyService接口的类将会被拦截),带有CacheResultAttribute特性的方法将会被缓存options.AddMemoryCacheResultAop();
});3. 对控制器接口使用内存缓存
builder.Services.AddControllers(options
{//添加自定义的缓存过滤器options.Filters.AddCacheResultFilter();
});方法缓存使用示例
//同一个方法不同参数会进行多次缓存
//接口注入直接继承IProxyService接口(继承接口后就不需要再手动注入了),方法上使用CacheResultAttribute特性即可
public class CacheResultService : ICacheResultService
{//缓存5秒[CacheResult(5)]public Student GetStudentAsync(string name){return new Student { Name name };}
}
public interface ICacheResultService : IProxyService
{Student GetStudentAsync(string name);
}控制器缓存使用示例
[HttpGet]
[CacheResult(5)]//缓存5秒
public object Get()
{_cacheResultService.GetStudentAsync(这是参数);return ok;
}