Redis 是一个开源的使用ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库。Redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。
本文主要讲述的是如何使用C#语言来进行Redis分布式缓存的程序编写。首先,需要从github下载最新的32/64位安装,解压后根据自己机器的实际情况选择32位或者64位,例如:我机器是64位win7,于是将64bit下所有文件拷贝到D盘根目录下新建文件夹redis中,或者直接下载Redis服务(可以将此服务设置为windows系统服务,下载
Redis服务安装软件,安装即可。),我个人比较喜欢这个。
第一步,从Nuget下载Redis,我用的是ServiceStack.Redis,新版本是5.0.2,我怕不稳定,下载了5.0.0
添加后自动引用Dll
第二步,在配置文件添加redis的相关配置
<configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <section name="RedisConfig" type="GTA.FTRTP.Common.Redis.RedisConfigInfo, GTA.FTRTP.Common" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60" MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false"> </RedisConfig> <connectionStrings>
第三步,添加一些基本的读写操作
public class RedisConfigInfo: ConfigurationSection { public static RedisConfigInfo GetConfig() { RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig"); return section; } public static RedisConfigInfo GetConfig(string sectionName) { RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig"); if (section == null) throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); return section; } /// <summary> /// 可写的Redis链接地址 /// </summary> [ConfigurationProperty("WriteServerList", IsRequired = false)] public string WriteServerList { get { return (string)base["WriteServerList"]; } set { base["WriteServerList"] = value; } } /// <summary> /// 可读的Redis链接地址 /// </summary> [ConfigurationProperty("ReadServerList", IsRequired = false)] public string ReadServerList { get { return (string)base["ReadServerList"]; } set { base["ReadServerList"] = value; } } ………… }
public class RedisManager { /// <summary> /// redis配置文件信息 /// </summary> private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig(); private static PooledRedisClientManager prcm; /// <summary> /// 静态构造方法,初始化链接池管理对象 /// </summary> static RedisManager() { CreateManager(); } /// <summary> /// 创建链接池管理对象 /// </summary> private static void CreateManager() { string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ","); string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ","); prcm = new PooledRedisClientManager(readServerList, writeServerList, new RedisClientManagerConfig { MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize, MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize, AutoStart = redisConfigInfo.AutoStart, }); } private static string[] SplitString(string strSource, string split) { return strSource.Split(split.ToArray()); } /// <summary> /// 客户端缓存操作对象 /// </summary> public static IRedisClient GetClient() { if (prcm == null) CreateManager(); return prcm.GetClient(); } }
第四步,在controller直接引用贴出部分代码
var redis = RedisManager.GetClient(); var redsiClass = redis.Get<Class>("All");//判断是否已经缓存 if (redsiClass != null) { model = redsiClass; } else { var entity = db.Class.FirstOrDefault(e => e.ClassId == StringKeys.UniversityId); var getClassList = ClassRepository.GetClassListByParentId(StringKeys.UniversityId).OrderBy(a => a.ClassId); var firstOrDefault = getClassList.FirstOrDefault(); model.CollageId = firstOrDefault != null ? firstOrDefault.ClassId : 0; redis.Set<Class>("All", model);//没有则进行添加 db.Dispose(); }
第五步,如果是有新的数据生成应该删除缓存存数据到数据库的
public void Remove(string value) { var redis = RedisManager.GetClient(); redis.Remove(value); }
以上就是redis简单的使用。
如果想要具体的实现步骤可以进CSDN下载相关源码:http://download.csdn.net/download/songlhua/10266722?from=singlemessage&isappinstalled=0#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2751b6d1