1. NguGet 引用 Abp.RedisCache
根据自身ABP框架版本来引用,这边我是4.9版本
2. 在 appsettings.json加上Redis服务器配置
"RedisCache": {
"ConnectionString": "127.0.0.1:6379",
"DatabaseId": "6"
},
3. 配置缓存为redis,不配置的话,走的是内存缓存
类的头部加入:
typeof(AbpRedisCacheModule))
PreInitialize方法中加入:
Configuration.Caching.UseRedis(option =>
{
option.ConnectionString = _appConfiguration.GetSection("RedisCache:ConnectionString").Value;
option.DatabaseId = _appConfiguration.GetValue<int>("RedisCache:DatabaseId");
});
4. 原有实体取值改造
var entity = _baseRepository.Get(id);
改成
var entity = _cacheManager.GetCache(typeof(TEntity).Name).Get(id, () => CacheGetEntity(id));
private TEntity CacheGetEntity(int id)
{
var entity = _baseRepository.Get(id);
if (entity == null) { entity = new TEntity(); }
return entity;
}
5. redis的启动 redis-server.exe 双击就启动啦.
redis-cli.exe 双击启动客户端,可以看见ip
6. 打开管理工具,清掉影响的数据
7. 启动程序访问
增加了两个缓存
结束:
================================
参考地址:
http://www.manongjc.com/article/76769.html
https://blog.youkuaiyun.com/weixin_30444105/article/details/97902889
==============================================
//配置所有Cache的默认过期时间为2小时 Configuration.Caching.ConfigureAll(cache => { cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2); });
//配置指定的Cache过期时间为10分钟 Configuration.Caching.Configure("BossAssistant", cache => { cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(10); });