1下载redis的安装包并按照操作安装
2开启 右击我的电脑→管理→服务→站到redis service服务 将其开启
注意:redis服务开启后其默认的ip和端口号为127.0.0.1:6379
3在项目中添加redis文件夹,在里面主要封装了三个类:
RedisConfigInfo:读取配置文件的信息
RedisManager:redis的管理
RedisOperation:redis的操作,主要是写入和读取缓存
4在webconfig中添加配置信息
在<configSections>节点里添加<section name="RedisConfig" type="CacheDemo.Redis.RedisConfigInfo, CacheDemo" />
作用:声明redis
在<configuration>中填写redis的配置信息
<RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60" MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="60" RecordeLog="false">
</RedisConfig>
5实现
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; namespace CacheDemo.Redis { 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; } } /// <summary> /// 最大写链接数 /// </summary> [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)] public int MaxWritePoolSize { get { int _maxWritePoolSize = (int)base["MaxWritePoolSize"]; return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5; } set { base["MaxWritePoolSize"] = value; } } /// <summary> /// 最大读链接数 /// </summary> [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)] public int MaxReadPoolSize { get { int _maxReadPoolSize = (int)base["MaxReadPoolSize"]; return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5; } set { base["MaxReadPoolSize"] = value; } } /// <summary> /// 自动重启 /// </summary> [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)] public bool AutoStart { get { return (bool)base["AutoStart"]; } set { base["AutoStart"] = value; } } /// <summary> /// 本地缓存到期时间,单位:秒 /// </summary> [ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)] public int LocalCacheTime { get { return (int)base["LocalCacheTime"]; } set { base["LocalCacheTime"] = value; } } /// <summary> /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项 /// </summary> [ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)] public bool RecordeLog { get { return (bool)base["RecordeLog"]; } set { base["RecordeLog"] = value; } } } }
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CacheDemo.Redis { 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(); } } }
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CacheDemo.Redis { public static class RedisOperation { /// <summary> /// 添加缓存 /// </summary> /// <typeparam name="T">缓存的对象类型</typeparam> /// <param name="obj">缓存的对象</param> /// <param name="second">缓存的超时时间</param> /// <param name="name">缓存记得名称</param> public static void Set<T>(this T obj, int second, string name) where T : class { using (var redisClient = RedisManager.GetClient()) { var redis = redisClient.Get<T>(name); if (redis == null) { redisClient.Set(name, obj); //设置缓存时间 redisClient.ExpireEntryAt(name, DateTime.Now.AddSeconds(second)); } } } public static T Get<T>(string name) where T : class { using (var redisClient = RedisManager.GetClient()) { return redisClient.Get<T>(name); } } } }