使用abp的 redis cache
top
使用abp的 redis cache
-1. 在微软维护的github项目的release里找到redis的windows版本 64位 大约5M,安装,安装,然后在安装目录找到redis.windows.conf, 更改redis的密码 requirepassword 123456, 更改最大上限 200M 或自定; 启动redis-server.exe,默认监听6379端口; 启动redis-cli.exe进入reds,然后命令很简单,就是简单的get,set:
> set akey avalue
> get akey
显示 avalue
> del akey
删除akey
- 在nuget里面搜索abp.redis,或者在nuget console里面Install-Package abp.redis在service层或者web层,使用 redis cache integration,在module层的preinitialize中使用
//...other namespaces
using Abp.Runtime.Caching.Redis;
namespace MyProject.AbpZeroTemplate.Web
{
[DependsOn(
//...other module dependencies
typeof(AbpRedisCacheModule))]
public class MyProjectWebModule : AbpModule
{
public override void PreInitialize()
{
//...other configurations
Configuration.Caching.UseRedis();
}
//...other code
}
}
- 注入 ICacheManager
public DemoAppService:ApplicationService{
private IRepository<Simple> _simpleRepository;
private ICacheManager _cacheManager;
public SimpleAppService(IRepository<Simple> simpleRepository, ICacheManager cacheManager)
{
_simpleRepository = simpleRepository;
_cacheManager = cacheManager;
}
public int GetSaveRedisId()
{
var simple = new Simple() { Name = "simple" };
var id = _simpleRepository.InsertAndGetId(simple);
var simpleFromCache = _cacheManager.GetCache("simple").Get(id, () => { return _simpleRepository.Get(id); });
//每一个get方法其实就是set方法,如果缓存不存在就先创建,如果缓存不存在就先找到东西填充起来再返回结果
return simpleFromCache.Id;
}
}
- 使用cache get
ITypedCache<int,Item> myCache=_cacheManager.GetCache<int,Item>("myCqche");
- Config Cache
//Configuration for all caches
Configuration.Caching.ConfigureAll(cache =>
{
cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
});
//Configuration for a specific cache
Configuration.Caching.Configure("MyCache", cache =>
{
cache.DefaultSlidingExpireTime = TimeSpan.FromHours(8);
});
- using redis visualizer
download from https://redisdesktop.com/download
- EntityCache in abp
//1. create an entity
public class Person:Entity{ public string Name{get;set;}}
//2. create a cache item
[AutoMapFrom(typeof(Person))]
public class PersonCacheItem{
public string Name{get;set;}
}
//3.create person cache interface
public interface IPersonCache:IEntityCache<PersonCacheItem>{
}
//4.create cache class for Person with cacheManager and Person repository
public class PersonCache : EntityCache<Person, PersonCacheItem>, IPersonCache, ITransientDependency
{
public PersonCache(ICacheManager cacheManager, IRepository<Person> repository)
: base(cacheManager, repository)
{
}
}
//5.create personservice only with personCache
public class MyPersonService : ITransientDependency
{
private readonly IPersonCache _personCache;
public MyPersonService(IPersonCache personCache)
{
_personCache = personCache;
}
public string GetPersonNameById(int id)
{
return _personCache[id].Name; //alternative: _personCache.Get(id).Name;
}
}
// now the person entity is everytime stored in cache
- 总结使用redis: 一般就是设定某个缓存的时间,或者总体设定所有缓存的时间, 然后通过api来执行相关的命令来操作缓存
这样这篇文章就完成了redis客户端的安装,然后还有abp框架里面引入redis客户端,使用相关接口的方法,在application service的api里面就可以通过注入的cachemanager操作redis缓存了;
还介绍了使用entitycache,把实体和cache的操作都放到entity cache的一套中间件层集成里面,在仓储里默认就有增删改查等一系列方法。