Discuz!NT中的Redis架构设计
在之前的Discuz!NT缓存的架构方案中,曾说过Discuz!NT采用了两级缓存方式,即本地缓存+memcached方式。在近半年多的实际运行环境下,该方案经受住了检验。现在为了提供多样式的解决方案,我在企业版里引入了Redis这个目前炙手可热的缓存架构产品,即将memcached与Redis作为可选插件方式来提供了最终用户,尽管目前测试的结果两者的差异不是很大(毫秒级),但我想多一种选择对用户来说也是好的。
闲话不多说了,开始今天的正文吧。
熟悉我们产品的开发者都知道,我们的产品在缓存设计上使用了策略模式(Stratety Pattern),之前在系统中就已实现了DefaultCacheStrategy和MemCachedStrategy,前者用于本地缓存方式,后者顾名思义,就是memcached缓存方式。所以只要我们实现一个RedisStrategy策略,并适当修改加载缓存策略的代码,就可以将memcached缓存方式替换成Redis,如下图:
下面我先将RedisStrategy的部分代码放上来,大家一看便知:
- /// <summary>
- /// 企业级Redis缓存策略类
- /// </summary>
- public class RedisStrategy : DefaultCacheStrategy
- {
- /// <summary>
- /// 添加指定ID的对象
- /// </summary>
- /// <param name="objId"></param>
- /// <param name="o"></param>
- public override void AddObject(string objId, object o)
- {
- if (!objId.StartsWith("/Forum/ShowTopic/"))
- base.AddObject(objId, o, LocalCacheTime);
- using (IRedisClient Redis = RedisManager.GetClient())
- {
- Redis.Set<byte[]>(objId, new ObjectSerializer().Serialize(o));
- }
- }
- /// <summary>
- /// 加入当前对象到缓存中
- /// </summary>
- /// <param name="objId">对象的键值</param>
- /// <param name="o">缓存的对象</param>
- /// <param name="o">到期时间,单位:秒</param>
- public override void AddObject(string objId, object o, int expire)
- {
- //凡是以"/Forum/ShowTopic/"为前缀不添加到本地缓存中,现类似键值包括: "/Forum/ShowTopic/Tag/{topicid}/" , "/Forum/ShowTopic/TopList/{fid}"
- if (!objId.StartsWith("/Forum/ShowTopic/"))
- base.AddObject(objId, o, expire);
- using (IRedisClient Redis = RedisManager.GetClient())
- {
- //永不过期
- if (expire == 0)
- Redis.Set<byte[]>(objId, new ObjectSerializer().Serialize(o));
- else
- Redis.Set<byte[]>(objId, new ObjectSerializer().Serialize(o), DateTime.Now.AddSeconds(expire));
- }
- }
- /// <summary>
- /// 移除指定ID的对象
- /// </summary>
- /// <param name="objId"></param>
- public override void RemoveObject(string objId)
- {
- //先移除本地cached,然后再移除memcached中的相应数据
- base.RemoveObject(objId);
- using (IRedisClient Redis = RedisManager.GetClient())
- {
- Redis.Remove(objId);
- }
- Discuz.EntLib.SyncCache.SyncRemoteCache(objId);
- }
- public override object RetrieveObject(string objId)
- {
- object obj = base.RetrieveObject(objId);
- if (obj == null)
- {
- using (IRedisClient Redis = RedisManager.GetClient())
- {
- obj = new ObjectSerializer().Deserialize(Redis.Get<byte[]>(objId));
- if (obj != null && !objId.StartsWith("/Forum/ShowTopic/"))//对ShowTopic页面缓存数据不放到本地缓存
- {
- if (objId.StartsWith("/Forum/ShowTopicGuestCachePage/"))//对游客缓存页面ShowTopic数据缓存设置有效时间
- base.TimeOut = GeneralConfigs.GetConfig().Guestcachepagetimeout * 60;
- if (objId.StartsWith("/Forum/ShowForumGuestCachePage/"))//对游客缓存页面ShowTopic数据缓存设置有效时间
- base.TimeOut = RedisConfigs.GetConfig().CacheShowForumCacheTime * 60;
- else
- base.TimeOut = LocalCacheTime;
- base.AddObject(objId, obj, TimeOut);
- }
- }
- }
- return obj;
- }
- /// <summary>
- /// 到期时间,单位:秒
- /// </summary>
- public override int TimeOut
- {
- get
- {
- return 3600;
- }
- }
- /// <summary>
- /// 本地缓存到期时间,单位:秒
- /// </summary>
- public int LocalCacheTime
- {
- get
- {
- return RedisConfigs.GetConfig().LocalCacheTime;
- }
- }
- /// <summary>
- /// 清空的有缓存数据
- /// </summary>
- public override void FlushAll()
- {
- base.FlushAll();
- using (IRedisClient Redis = RedisManager.GetClient())
- {
- Redis.FlushAll();
- }
- }
- }
可以看出RedisStrategy类继承自DefaultCacheStrategy,这一点与MemCachedStrategy实现如出一辙,唯一不同是其缓存数据加载和设置的方式有所不同,而具体的用法可以参见我之前写的这篇文章中的“object序列化方式存储” 。
当然上面代码中有两个类未说明,一个是RedisConfigs,一个是RedisManager,前者是配置文件信息类,我们产品因为使用了统一的序列化接口实现方式(参见该文),所以其实现方式比较清晰,其序列化类的结构如下:
- /// <summary>
- /// Redis配置信息类文件
- /// </summary>
- public class RedisConfigInfo : IConfigInfo
- {
- private bool _applyRedis;
- /// <summary>
- /// 是否应用Redis
- /// </summary>
- public bool ApplyRedis
- {
- get
- {
- return _applyRedis;
- }
- set
- {
- _applyRedis = value;
- }
- }
- private string _writeServerList;
- /// <summary>
- /// 可写的Redis链接地址
- /// </summary>
- public string WriteServerList
- {
- get
- {
- return _writeServerList;
- }
- set
- {
- _writeServerList = value;
- }
- }
- private string _readServerList;
- /// <summary>
- /// 可读的Redis链接地址
- /// </summary>
- public string ReadServerList
- {
- get
- {
- return _readServerList;
- }
- set
- {
- _readServerList = value;
- }
- }
- private int _maxWritePoolSize;
- /// <summary>
- /// 最大写链接数
- /// </summary>
- public int MaxWritePoolSize
- {
- get
- {
- return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5;
- }
- set
- {
- _maxWritePoolSize = value;
- }
- }
- private int _maxReadPoolSize;
- /// <summary>
- /// 最大读链接数
- /// </summary>
- public int MaxReadPoolSize
- {
- get
- {
- return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
- }
- set
- {
- _maxReadPoolSize = value;
- }
- }
- private bool _autoStart;
- /// <summary>
- /// 自动重启
- /// </summary>
- public bool AutoStart
- {
- get
- {
- return _autoStart;
- }
- set
- {
- _autoStart = value;
- }
- }
- private int _localCacheTime = 30000;
- /// <summary>
- /// 本地缓存到期时间,该设置会与memcached搭配使用,单位:秒
- /// </summary>
- public int LocalCacheTime
- {
- get
- {
- return _localCacheTime;
- }
- set
- {
- _localCacheTime = value;
- }
- }
- private bool _recordeLog = false;
- /// <summary>
- /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
- /// </summary>
- public bool RecordeLog
- {
- get
- {
- return _recordeLog;
- }
- set
- {
- _recordeLog = value;
- }
- }
- private int _cacheShowTopicPageNumber = 5;
- /// <summary>
- /// 缓存帖子列表分页数(showtopic页数使用缓存前N页的帖子列表信息)
- /// </summary>
- public int CacheShowTopicPageNumber
- {
- get
- {
- return _cacheShowTopicPageNumber;
- }
- set
- {
- _cacheShowTopicPageNumber = value;
- }
- }
- /// <summary>
- /// 缓存showforum页面分页数
- /// </summary>
- public int CacheShowForumPageNumber{set;get;}
- /// <summary>
- /// 缓存showforum页面时间(单位:分钟)
- /// </summary>
- public int CacheShowForumCacheTime{set;get;}
- }
其序列化出来的xml文件格式形如:
- <?xml version="1.0"?>
- <RedisConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <ApplyRedis>true</ApplyRedis>
- <WriteServerList>10.0.4.210:6379</WriteServerList>
- <ReadServerList>10.0.4.210:6379</ReadServerList>
- <MaxWritePoolSize>60</MaxWritePoolSize>
- <MaxReadPoolSize>60</MaxReadPoolSize>
- <AutoStart>true</AutoStart>
- <LocalCacheTime>180</LocalCacheTime>
- <!--单位:秒-->
- <RecordeLog>false</RecordeLog>
- <!--缓存帖子列表分页数(showtopic页数使用缓存前N页的帖子列表信息)-->
- <CacheShowTopicPageNumber>2</CacheShowTopicPageNumber>
- <!--缓存showforum页面分页数-->
- <CacheShowForumPageNumber>2</CacheShowForumPageNumber>
- <!--缓存showforum页面时间(单位:分钟)-->
- <CacheShowForumCacheTime>10</CacheShowForumCacheTime>
- </RedisConfigInfo>
之前所说的RedisManager类则是一个RedisClient客户端实现的简单封装,主要为了简化基于链接池的Redis链接方式的使用。其结构如下:
- using System.Collections;
- using Discuz.Config;
- using Discuz.Common;
- using ServiceStack.Redis;
- using ServiceStack.Redis.Generic;
- using ServiceStack.Redis.Support;
- namespace Discuz.EntLib
- {
- /// <summary>
- /// MemCache管理操作类
- /// </summary>
- public sealed class RedisManager
- {
- /// <summary>
- /// redis配置文件信息
- /// </summary>
- private static RedisConfigInfo redisConfigInfo = RedisConfigs.GetConfig();
- private static PooledRedisClientManager prcm;
- /// <summary>
- /// 静态构造方法,初始化链接池管理对象
- /// </summary>
- static RedisManager()
- {
- CreateManager();
- }
- /// <summary>
- /// 创建链接池管理对象
- /// </summary>
- private static void CreateManager()
- {
- string[] writeServerList = Utils.SplitString(redisConfigInfo.WriteServerList, ",");
- string[] readServerList = Utils.SplitString(redisConfigInfo.ReadServerList, ",");
- prcm = new PooledRedisClientManager(readServerList, writeServerList,
- new RedisClientManagerConfig
- {
- MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
- MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
- AutoStart = redisConfigInfo.AutoStart,
- });
- }
- /// <summary>
- /// 客户端缓存操作对象
- /// </summary>
- public static IRedisClient GetClient()
- {
- if (prcm == null)
- CreateManager();
- return prcm.GetClient();
- }
- }
- }
上面的代码主要将redis.config的配置文件文件信息加载到程序里并实始化PooledRedisClientManager对象,该对象用于池化redis的客户端链接,具体方式参见这篇文章
好了,到这里主要的内容就介绍完了。
Discuz!NT中的Redis架构设计的更多相关文章
- Redis --> Redis架构设计
Redis架构设计 一.前言 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列 ...
- 细说分布式Redis架构设计和踩过的那些坑
细说分布式Redis架构设计和踩过的那些坑_redis 分布式_ redis 分布式锁_分布式缓存redis 细说分布式Redis架构设计和踩过的那些坑
- Java生鲜电商平台-生鲜系统中微服务架构设计与分析实战
Java生鲜电商平台-生鲜系统中微服务架构设计与分析实战 说明: Java生鲜系统中微服务的拆分应该如何架构设计与分析呢?以下是我的实战中的设计与经验分析. 目录 1. 微服务简介2. 当前现状3. ...
- Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲
Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲 Java生鲜电商平台: 微服务是当前非常流行的技术框架,通过服务的小型化.原子化以及分布式架构的弹性伸缩和高可用性, ...
- Discuz!NT中集成Memcached分布式缓存
大约在两年前我写过一篇关于Discuz!NT缓存架构的文章,在那篇文章的结尾介绍了在IIS中如果开启多个应用程序池会造成多个缓存实例之间数据同步的问题.虽然给出了一个解决方案,但无形中却把压力转移到了 ...
- [转载] Codis作者黄东旭细说分布式Redis架构设计和踩过的那些坑们
原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=208733458&idx=1&sn=691bfde670fb ...
- 浅谈iOS中MVVM的架构设计与团队协作
说到架构设计和团队协作,这个对App的开发还是比较重要的.即使作为一个专业的搬砖者,前提是你这砖搬完放在哪?不只是Code有框架,其他的东西都是有框架的,比如桥梁等等神马的~在这儿就不往外扯了.一个好 ...
- iOS中MVVM的架构设计与团队协作
对MVVM的理解主要是借鉴于之前的用过的MVC的Web框架,之前用过ThinkPHP框架,和SSH框架,都是MVC的架构模式,今天MVVM与传统的MVC可谓是极为相似,也可以说是兄弟关系,也就是一家人 ...
- IOS中 浅谈iOS中MVVM的架构设计与团队协作
今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
随机推荐
- 文档翻译-Minimizing your app's Memory Footprint
原文地址:https://developer.apple.com/library/ios/technotes/tn2434/_index.html#//apple_ref/doc/uid/DTS400 ...
- 【Linux】设定一个能输入中文的英文环境!
引子:centos startx 进入桌面后使用中文输入法 这个解决方法太蠢了,而且只适用于centos等red系系统... 在此提供一个更加通用的方法 => 只要设置好系统的locale坏境变 ...
- NSFileManager 沙盒文件管理
文件夹创建,复制,移动,删除,检查是否存在,代码如下: 1.获取沙盒 document 路径,作为文件夹路径的基路径. NSString *document = NSSearchPathForDire ...
- WEB开发人员必知的20+HTML5技巧(转)
互联网科技发展的速度真可谓惊人的快,一个稍不留神,你就可能无法跟上它的步伐. HTML5的变化和更新也压倒不少人,这篇文章将向大家介绍一些最基本也非常必要的 HTML技巧. 1. 新的文档类型(Doc ...
- 【概率】COGS 1487:麻球繁衍
Description 万有引力定律: “使物体相互靠近的力的大小与物体的质量成正比——而物体的质量又由同一种力决定.这是一个有趣并且有益的例子,说明了科学是如何用A证明B,再用B证明A的.”——安布 ...
- Apache URL rewrite 配置
下面是Apache的配置过程,可以参考下:1.httpd.conf配置文件中加载了mod_rewrite.so模块,使用虚拟主机 #LoadModule rewrite_module modules/ ...
- Flume学习——Flume的架构
Flume有三个组件:Source.Channel 和 Sink.在源码中对应同名的三个接口. When a Flume source receives an event, it stores it ...
- ExtJS4 MVC开发教程:搭建开发环境
原文地址:http://www.lihuai.net/qianduan/extjs/864.html 博主系列教程:http://www.lihuai.net/qianduan/extjs 在所有的J ...
- [itint5]字符串匹配
http://www.itint5.com/oj/#15 用hash来做,目前为止做到最好也是case16超时(20w的规模),即使分桶也超时.注意计算hashcode时,'a'要算成1,否则如果'a ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...