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
  1. 在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
}
}
  1. 注入 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;
}
}
  1. 使用cache get
ITypedCache<int,Item> myCache=_cacheManager.GetCache<int,Item>("myCqche");
  1. 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);
});
  1. using redis visualizer
download from  https://redisdesktop.com/download

  1. 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
  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. android入门 — ListView

    ListView主要是用来解决大量数据展示的问题,它的用途很广泛,几乎所有的app都会用到,比如说知乎.今日头条.微博.通讯录等. ListView允许用户通过上下滑动的方式将屏幕外的数据滚动到屏幕中 ...

  2. lintcode-176-图中两个点之间的路线

    176-图中两个点之间的路线 给出一张有向图,设计一个算法判断两个点 s 与 t 之间是否存在路线. 样例 如下图: for s = B and t = E, return true for s = ...

  3. MVC绕过登陆界面验证时HttpContext.Current.User.Identity.Name取值为空问题解决方法

    Global.asax界面添加如下方法: void FormsAuthentication_Authenticate(object sender, FormsAuthenticationEventAr ...

  4. Delphi SQL语句字符串拼接

    单引号必须成对出现,最外层的单引号表示其内部符号为字符:除最外层以外的单引号,每两个单引号代表一个'字符.加号:+用于字符串之间的连接.字符串常量用四个单引号,例如 ' select * from T ...

  5. java基础简介

    一.软件开发 软件:是由数据和指令组成的(例:计算器) 如何实现软件开发呢?    就是使用开发工具和计算机语言做出东西来 二.常用dos命令 d: 回车 盘符切换 dir(directory):列出 ...

  6. 【bzoj1026】[SCOI2009]windy数 数位dp

    题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 输入 包含两个整数 ...

  7. csrf漏洞攻击手段和影响详解

    针对web应用安全中csrf漏洞两种典型的攻击方式:即输入和执行,这种简单模式下的攻击手段以及中途包含确认页面的攻击方法. 图解什么是csrf漏洞 我们先进行约束,比如存在csrf漏洞的网站叫webA ...

  8. BZOJ2151 种树(贪心+堆+链表/wqs二分+动态规划)

    dp容易想到,但没法进一步优化了. 考虑贪心,每次选出价值最大的物品.但这显然是不对的因为会影响其他物品的选择. 于是考虑加上反悔操作.每次选出一个物品后,将其相邻两物品删除,再将原物品价值变为相邻两 ...

  9. wsgiref 源码解析

    Web Server Gateway Interface(wsgi),即Web服务器网关接口,是Web服务器软件和用Python编写的Web应用程序之间的标准接口. 想了解更多关于WSGI请前往: h ...

  10. [2018国家集训队][UOJ449] 喂鸽子 [dp+组合数学]

    题面 传送门 思路 首先,这道题是可以暴力min-max反演+NTT做出来的......但是这个不美观,我来讲一个做起来舒服一点的做法 一个非常basic的idea:我们发现在一只鸽子吃饱以后再喂给它 ...