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:

  1. > set akey avalue
  2. > get akey
  3. 显示 avalue
  4. > del akey
  5. 删除akey
  1. 在nuget里面搜索abp.redis,或者在nuget console里面Install-Package abp.redis在service层或者web层,使用 redis cache integration,在module层的preinitialize中使用
  1. //...other namespaces
  2. using Abp.Runtime.Caching.Redis;
  3. namespace MyProject.AbpZeroTemplate.Web
  4. {
  5. [DependsOn(
  6. //...other module dependencies
  7. typeof(AbpRedisCacheModule))]
  8. public class MyProjectWebModule : AbpModule
  9. {
  10. public override void PreInitialize()
  11. {
  12. //...other configurations
  13. Configuration.Caching.UseRedis();
  14. }
  15. //...other code
  16. }
  17. }
  1. 注入 ICacheManager
  1. public DemoAppService:ApplicationService{
  2. private IRepository<Simple> _simpleRepository;
  3. private ICacheManager _cacheManager;
  4. public SimpleAppService(IRepository<Simple> simpleRepository, ICacheManager cacheManager)
  5. {
  6. _simpleRepository = simpleRepository;
  7. _cacheManager = cacheManager;
  8. }
  9. public int GetSaveRedisId()
  10. {
  11. var simple = new Simple() { Name = "simple" };
  12. var id = _simpleRepository.InsertAndGetId(simple);
  13. var simpleFromCache = _cacheManager.GetCache("simple").Get(id, () => { return _simpleRepository.Get(id); });
  14. //每一个get方法其实就是set方法,如果缓存不存在就先创建,如果缓存不存在就先找到东西填充起来再返回结果
  15. return simpleFromCache.Id;
  16. }
  17. }
  1. 使用cache get
  1. ITypedCache<int,Item> myCache=_cacheManager.GetCache<int,Item>("myCqche");
  1. Config Cache
  1. //Configuration for all caches
  2. Configuration.Caching.ConfigureAll(cache =>
  3. {
  4. cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
  5. });
  6. //Configuration for a specific cache
  7. Configuration.Caching.Configure("MyCache", cache =>
  8. {
  9. cache.DefaultSlidingExpireTime = TimeSpan.FromHours(8);
  10. });
  1. using redis visualizer
  1. download from https://redisdesktop.com/download
  1. EntityCache in abp
  1. //1. create an entity
  2. public class Person:Entity{ public string Name{get;set;}}
  3. //2. create a cache item
  4. [AutoMapFrom(typeof(Person))]
  5. public class PersonCacheItem{
  6. public string Name{get;set;}
  7. }
  8. //3.create person cache interface
  9. public interface IPersonCache:IEntityCache<PersonCacheItem>{
  10. }
  11. //4.create cache class for Person with cacheManager and Person repository
  12. public class PersonCache : EntityCache<Person, PersonCacheItem>, IPersonCache, ITransientDependency
  13. {
  14. public PersonCache(ICacheManager cacheManager, IRepository<Person> repository)
  15. : base(cacheManager, repository)
  16. {
  17. }
  18. }
  19. //5.create personservice only with personCache
  20. public class MyPersonService : ITransientDependency
  21. {
  22. private readonly IPersonCache _personCache;
  23. public MyPersonService(IPersonCache personCache)
  24. {
  25. _personCache = personCache;
  26. }
  27. public string GetPersonNameById(int id)
  28. {
  29. return _personCache[id].Name; //alternative: _personCache.Get(id).Name;
  30. }
  31. }
  32. // now the person entity is everytime stored in cache
  1. 总结使用redis: 一般就是设定某个缓存的时间,或者总体设定所有缓存的时间, 然后通过api来执行相关的命令来操作缓存
这样这篇文章就完成了redis客户端的安装,然后还有abp框架里面引入redis客户端,使用相关接口的方法,在application service的api里面就可以通过注入的cachemanager操作redis缓存了;
还介绍了使用entitycache,把实体和cache的操作都放到entity cache的一套中间件层集成里面,在仓储里默认就有增删改查等一系列方法。

GoToTop

使用abp的 redis cache的更多相关文章

  1. ABP中使用Redis Cache(1)

    本文将讲解如何在ABP中使用Redis Cache以及使用过程中遇到的各种问题.下面就直接讲解使用步骤,Redis环境的搭建请直接网上搜索. 使用步骤: 一.ABP环境搭建 到http://www.a ...

  2. ABP中使用Redis Cache(2)

    上一篇讲解了如何在ABP中使用Redis Cache,虽然能够正常的访问Redis,但是Redis里的信息无法同步更新.本文将讲解如何实现Redis Cache与实体同步更新.要实现数据的同步更新,我 ...

  3. 缓存与ABP Redis Cache

    缓存与ABP Redis Cache 为什么要用缓存 为什么要用缓存呢,说缓存之前先说使用缓存的优点. 减少寄宿服务器的往返调用(round-trips). 如果缓存在客户端或是代理,将减少对服务器的 ...

  4. ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十五节--缓存小结与ABP框架项目中 Redis Cache的实现

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 缓存 为什么要用缓存 为什么要用缓存呢,说缓存之前先说使用缓存的优点. 减少寄宿服务器的往返调用(round-tr ...

  5. abp使用redis缓存

    利用NuGet程序包管理程序,添加 Abp.RedisCache 在 xxxx.Web.Core 项目的Module中注册Redis 在刚才上面这个类文件头部注册Redis组件 在Web.config ...

  6. Azure Redis Cache

    将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache.Azure Redis Cache包含以下两个层级的产品 ...

  7. Azure Redis Cache (1) 入门

    <Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...

  8. Azure Redis Cache (2) 创建和使用Azure Redis Cache

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 注意: 截至今日2015年10月7日,国内由世纪互联运维的Azur ...

  9. Azure Redis Cache (3) 创建和使用P级别的Redis Cache

    <Windows Azure Platform 系列文章目录> 在笔者之前的文档里面已经说明了,Azure Redis Cache分为三个不同的级别: - 基本,Basic,不包含SLA ...

随机推荐

  1. css3边框阴影效果

    下面来说下css3阴影的语法: box-shadow:none | <shadow> [ , <shadow> ]* <shadow> = inset? & ...

  2. 简介Kafka Streams

    本文从以下几个方面介绍Kafka Streams: 一. Kafka Streams 背景 二. Kafka Streams 架构 三. Kafka Streams 并行模型 四. Kafka Str ...

  3. loadrunner如何监控windows系统的资源

    1.测试客户端与服务器之间的网络,保证通信畅通 2.开启服务器端Windows中的如下两个服务,可见系统服务中查找,cmd输入:services.msc 如下图: Remote Registry需改为 ...

  4. cacti监控多个mysql端口

    1. 在Console -> Data Templates 找到mysql-cacti-templates的mysql模板,编辑: 在这里面把Port的Use Per-Data Source V ...

  5. foreach循环2

    <select id="test" parameterType="java.util.List" resultType="user"& ...

  6. JAVA IDE IntelliJ IDEA使用简介(三)—之你不能忘记的快捷键

    IDEA有许多的快捷键来帮助你更便捷的编写代码,以下列出的快捷键(默认情况下,你还没有定制你的快捷键)是工作中经常需要使用到的,请牢记 快捷键 描述 备注 Alt+F1 视图切换 切换当前工作文件的视 ...

  7. 第28天:js-Tab栏切换封装函数

    一.input.value所有值都是string 二.变量和属性var index=10;//变量var arr=[];//数组arr.index=20;//index为自定义属性,只能在arr下使用 ...

  8. 【bzoj5094】硬盘检测 乱搞

    题目描述 已知从 $n$ 个不同的32位无符号整数中随机选 $m=10000$ 次所得的结果,求可能性最大的 $n$ ,其中 $n=10^k,1\le k\le 7$. 输入 第一行包含一个正整数m( ...

  9. 【bzoj2435】[NOI2011]道路修建 树形dp

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...

  10. pascal语言中学版整理

    P1:主菜单File中的Command shell选项,可以暂时退出Pascal,进入DOS提示符状态,但Pascal仍然驻留在内存中.输入命令exit即可返回Pascal. P3:Edit菜单中Un ...