ABP中使用Redis Cache(2)
上一篇讲解了如何在ABP中使用Redis Cache,虽然能够正常的访问Redis,但是Redis里的信息无法同步更新。本文将讲解如何实现Redis Cache与实体同步更新。要实现数据的同步更新,我们能够想到的最基本、最简单、也是复杂的方法:在每一个增、删、改的方法里添加同步缓存的代码,说它最简单,是因为技术实现非常简单,就是在每一个方法里多加一句代码;说它复杂,是因为这样的写的话,会出现很多重复的代码,并且容易出现遗漏,维护起来很不方便。那么有没有一种更简单的方式呢,在数据出现变化后,触发一个事件,主动通知订阅者执行相关操作呢?答案是肯定的,我们可以通过注册领域事件来实现,在ABP中,实体增加、删除、修改后会触发相关事件,只要注册就可以了。要注册事件,有两种方式可以实现,第一是自动注册,实现IEventHandler就可以了,ABP会自动注册;第二是通过IEventBus的Register方法手动注册。ABP中推荐使用自动注册的方式实现,本文也会采用第一种方式实现。下面我们就来看具体的实现方式,首先增加一个处理缓存同步的接口ICacheSyncService,代码如下(本文的代码是在上一篇的基础之上编写的):
ICacheSyncService.cs
public interface ICacheSyncService
{
void Add<TEntity>(TEntity entity) where TEntity : class, IEntity<int>;
void Remove<TEntity>(int id) where TEntity : class, IEntity<int>;
void Update<TEntity>(TEntity entity) where TEntity : class, IEntity<int>;
}
CacheSyncService.cs
public class CacheSyncService : ICacheSyncService, ISingletonDependency
{
public ICacheService CacheService { get; set; } public void Add<TEntity>(TEntity entity) where TEntity : class, IEntity<int>
{
CacheService.Set(entity.Id, entity);
} public void Remove<TEntity>(int id) where TEntity : class, IEntity<int>
{
CacheService.Remove<int, TEntity>(id);
} public void Update<TEntity>(TEntity entity) where TEntity : class, IEntity<int>
{
CacheService.Set(entity.Id, entity);
}
}
第二增加一个处理实体事件的泛型基类EntityChangedHandlerBase<TEntity>,代码如下:
public abstract class EntityChangedHandlerBase<TEntity>:
ISingletonDependency,
IEventHandler<EntityCreatedEventData<TEntity>>,
IEventHandler<EntityDeletedEventData<TEntity>>,
IEventHandler<EntityUpdatedEventData<TEntity>>
where TEntity : class, IEntity<int>
{
public ICacheSyncService CacheSyncService { get; set; } public virtual void HandleEvent(EntityCreatedEventData<TEntity> eventData)
{
CacheSyncService.Add(eventData.Entity);
} public virtual void HandleEvent(EntityDeletedEventData<TEntity> eventData)
{
CacheSyncService.Remove<TEntity>(eventData.Entity.Id);
} public virtual void HandleEvent(EntityUpdatedEventData<TEntity> eventData)
{
CacheSyncService.Update(eventData.Entity);
}
}
第三,增加一个需要进行缓存同步的实体类,继承自EntityChangedHandlerBase<TEntity>就可以了,不需要编写任何代码,如果有特殊情况,可以重新HandleEvent方法,代码如下:
public class ArticleChangedHandler : EntityChangedHandlerBase<Article>
{ }
至此,缓存同步的全部代码已编写完成,我们运行来看看效果:
我们添加文章后,什么操作也不做,直接到Redis里查看是否有新增的数据,如果有新增的数据表示缓存能够自动更新了(删除和修改是一样的,这里就不编写相关代码了),直接看单元测试代码。
public class CacheSync_Tests : UsingRedisInAbpTestBase
{
[Fact]
public void Test_Entity_Changed_Event()
{
LoginAsHostAdmin();
var title = "unit_test";
var articleId = ;
var service = Resolve<TestCacheSyncService>();
service.IsCreatedEventFired.ShouldBeFalse();
service.IsUpdatedEventFired.ShouldBeFalse();
service.IsDeletedEventFired.ShouldBeFalse(); //新增文章测试
UsingDbContext(c =>c.Articles.Add(new Article {Title = title})); service.IsCreatedEventFired.ShouldBe(true); //更新文章测试
UsingDbContext(c =>
{
var article=c.Articles.First();
c.Articles.Attach(article);
article.Title = "new_title";
});
service.IsUpdatedEventFired.ShouldBe(true); //删除文章测试
UsingDbContext(c =>
{
var article = c.Articles.First();
c.Articles.Remove(article);
});
service.IsDeletedEventFired.ShouldBe(true);
}
}
TestSyncCache.cs代码:
public class TestCacheSyncService : ICacheSyncService
{
public bool IsCreatedEventFired { get; set; }
public bool IsDeletedEventFired { get; set; }
public bool IsUpdatedEventFired { get; set; }
public void Add<TEntity>(TEntity entity) where TEntity : class, IEntity<int>
{
IsCreatedEventFired = true; } public void Remove<TEntity>(int id) where TEntity : class, IEntity<int>
{
IsDeletedEventFired = true; } public void Update<TEntity>(TEntity entity) where TEntity : class, IEntity<int>
{
IsUpdatedEventFired = true;
}
}
通过以上方式已实现了ABP中Redis缓存的读取和设置,也实现了缓存的同步更新,如果要在ABP中使用其他缓存也是一样的,只需要把缓存实现部分换成其他缓存就行。本文的所有源代码下载地址:
http://files.cnblogs.com/files/loyldg/UsingRedisInAbp_2.src.rar
ABP中使用Redis Cache(2)的更多相关文章
- ABP中使用Redis Cache(1)
本文将讲解如何在ABP中使用Redis Cache以及使用过程中遇到的各种问题.下面就直接讲解使用步骤,Redis环境的搭建请直接网上搜索. 使用步骤: 一.ABP环境搭建 到http://www.a ...
- Azure Redis Cache
将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache.Azure Redis Cache包含以下两个层级的产品 ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache
文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ...
- ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十五节--缓存小结与ABP框架项目中 Redis Cache的实现
返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 缓存 为什么要用缓存 为什么要用缓存呢,说缓存之前先说使用缓存的优点. 减少寄宿服务器的往返调用(round-tr ...
- 缓存与ABP Redis Cache
缓存与ABP Redis Cache 为什么要用缓存 为什么要用缓存呢,说缓存之前先说使用缓存的优点. 减少寄宿服务器的往返调用(round-trips). 如果缓存在客户端或是代理,将减少对服务器的 ...
- 使用abp的 redis cache
top 使用abp的 redis cache -1. 在微软维护的github项目的release里找到redis的windows版本 64位 大约5M,安装,安装,然后在安装目录找到redis.wi ...
- .NET中使用Redis (二)
很久以前写了一篇文章 .NET中使用Redis 介绍了如何安装Redis服务端,以及如何在.NET中调用Redis读取数据.本文简单介绍如何设计NoSQL数据库,以及如何使用Redis来存储对象. 和 ...
- 如何在ASP.NET Core中使用Redis
注:本文提到的代码示例下载地址> https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d826418 Redis是一个开源的内 ...
- Azure Redis Cache (1) 入门
<Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...
随机推荐
- struts tags
HTTP ERROR 500 Problem accessing /showognl.jsp. Reason: Server Error Caused by: org.apache.jasper.Ja ...
- Fig 应用编排
Fig是Docker的应用编排工具,主要用来跟 Docker 一起来构建基于 Docker 的复杂应用,Fig 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景. 说 ...
- Eclipse 常用快捷键 (动画讲解)
Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 编辑相关快捷键 注释 Ctrl + / 快速修复 Ctrl + ...
- [oracle]逗号(或分号等)间隔的列,按这一列劈分后转多行
今天遇到个需求,要匹配两个表, 但是关联的字段,在另一个表中是放在一个字段中用分号分割的 怎么全部匹配呢? 后来在网上搜到了, 记录下 SELECT cp.ES_EXTNUMBER, cp.ES_AG ...
- 一个上好的C# http/https类
直接Copy拿去用吧: 新的 tls 协议需要新的.net版本, tls 至少更新到.net4吧,尽量用最新的.net! 不然出错了就折腾... using System; using System. ...
- Oracle 多行转多列,列值转为列名
前段时间做调查问卷,客户创建自定义问卷内容,包括题目和选项内容; 之后需要导出问卷明细,,,,麻烦来咯 于是到网上到处搜索,没有直接结果;于是又找各种相似的,,终于功夫不负有心人 然后最终自己写出来了 ...
- String Aop 动态代理例子
动态代理原理:spring AOP采用动态代理来实现 (1)定义一个接口Boy package aop001; public interface Boy { public void beat(Stri ...
- LINQ系列:LINQ to SQL Select查询
1. 查询全部字段 using (NorthwindContext context = new NorthwindContext()) { var expr = context.Products; f ...
- SQL Server 2014新特性探秘(1)-内存数据库
简介 SQL Server 2014提供了众多激动人心的新功能,但其中我想最让人期待的特性之一就要算内存数据库了.去年我再西雅图参加SQL PASS Summit 2012的开幕式时,微软就宣布 ...
- 【Win10 应用开发】解决VS 2015 RC不能调试手机应用的问题
VS2015 RC已发布,当然这个版本还不能用于实际生产中,如果你没有测试环境,就等正式版出来,RC都来了,RTM就不远了. 如果你也像老周一样,已经在耍RC版了,你可能会遇到下面问题: 安装Win ...