A redis & memory cache middleware using AspectCore-Framework & csredis.
- In dotnetcore 3.0, we must configure Program.cs:
using AspectCore.Extensions.DependencyInjection;// import this namespace
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
// for aspcectcore
.UseServiceProviderFactory(new AspectCoreServiceProviderFactory())
;
}
-
- configure services in StartUp.cs:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddAspectCoreMemoryCache();
services.AddAspectCoreRedisCache("your redis connetion string");
return services.BuildServiceProvider();
}
-
- declare an inteface.
CacheKey -> default value : {namespace}{class}{method}{params hashcode}
Expiration -> default value : 600 (seconds)
public interface ISysMenuRepository:IRepository<SysMenu, string>
{
[Cached(CacheKey = "Redis_Cache_SysMenu", Expiration = 5)]
IList<SysMenu> GetMenusByCache(Expression<Func<SysMenu, bool>> where);
}
-
- declare an implement class
public class SysMenuRepository : BaseRepository<SysMenu, string>, ISysMenuRepository
{
public IList<SysMenu> GetMenusByCache(Expression<Func<SysMenu, bool>> @where)
{
return DbContext.Get(where, true).ToList();
}
}