备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地址。
a.Redis是一个开源,先进的key-value(键/值对)存储,并且勇于构建高性能,可扩展的Web应用程序的完美解决方案
b.Redis和Memcached的对比
b.1 Redis数据库完全在内存中,使用磁盘仅用于持久性
b.2 相比较许多键值对存储,redis拥有更加丰富的数据类型,Redis提供的五种数据类型: strings、map、 list、sets、 sorted sets
b.3 Redis可以将数据复制到任意数量的从服务器
c.Redis拥有的优势
c.1 Redis的执行响应速度非常快
c.2 支持丰富的数据类型
c.3 原子性,保证了如果两个客户端同事访问的Redis服务器将获得更新后的值
c.4 多功能实用工具,Redis是一个多实用的工具,可以在多个用例如缓存,消息,队列实用,任何短暂的数据,应用程序。
d.Github下载地址:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/%E7%BC%93%E5%AD%98
e.Redis学习:http://www.redis.io/
1 // 源文件头信息: 2 // <copyright file="RedisHelper.cs"> 3 // Copyright(c)2014-2034 Kencery.All rights reserved. 4 // 个人博客:http://www.VEVb.com/hanyinglong 5 // 创建人:韩迎龙(kencery) 6 // 创建时间:2015-8-18 7 // </copyright> 8 9 using System; 10 using System.Collections.Generic; 11 using System.Configuration; 12 using ServiceStack.Redis; 13 14 namespace KenceryCommonMethod 15 { 16 /// <summary> 17 /// Redis缓存读取设置 封装 18 /// <auther> 19 /// <name>Kencery</name> 20 /// <date>2015-8-18</date> 21 /// </auther> 22 /// </summary> 23 public static class RedisHelper 24 { 25 /// <summary> 26 /// 创建Redis连接池管理对象(添加ServiceStack.Interfaces.dll、ServiceStack.Redis.dll) 27 /// </summary> 28 /// <param name="readWriteHosts">只写服务器</param> 29 /// <param name="readOnlyHosts">只读服务器</param> 30 /// <returns></returns> 31 private static PooledRedisClientManager CreateRedisManager(IEnumerable<string> readWriteHosts, 32 IEnumerable<string> readOnlyHosts) 33 { 34 //支持读写分离,均衡负载 35 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 36 { 37 MaxWritePoolSize = 5, //“写”链接池数 38 MaxReadPoolSize = 5, //“读”链接池数 39 AutoStart = true, 40 }); 41 } 42 43 /// <summary> 44 /// 调用CreateRedisManager方法,创建连接池管理对象,Redis服务器地址在配置文件中配置(创建只读,只写连接池) 45 /// <add key="RedisHosts" value="127.0.0.1:6379" /> 46 /// </summary> 47 private static readonly PooledRedisClientManager Prcm = CreateRedisManager( 48 ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries), 49 ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); 50 51 /// <summary> 52 /// 给缓存中添加数据,使用:RedisHelper.Set(key,值(需要存放的值)); 53 /// </summary> 54 /// <typeparam name="T">缓存类型</typeparam> 55 /// <param name="key">键</param> 56 /// <param name="val">值</param> 57 public static void Set<T>(string key, T val) 58 { 59 using (IRedisClient rdc = Prcm.GetClient()) 60 { 61 rdc.Set<T>(key, val); 62 } 63 } 64 65 /// <summary> 66 /// 读取缓存中的数据,使用:var result=RedisHelper.Get<T>(key); 67 /// </summary> 68 /// <typeparam name="T">返回读取的数据</typeparam> 69 /// <param name="key">键</param> 70 /// <returns></returns> 71 public static T Get<T>(string key) where T : class 72 { 73 using (IRedisClient rdc = Prcm.GetReadOnlyClient()) 74 { 75 return rdc.Get<T>(key); 76 } 77 } 78 79 /// <summary> 80 /// 删除缓存中的数据,使用 RedisHelper.Remove(key); 81 /// </summary> 82 /// <param name="key">键</param> 83 public static void Remove(string key) 84 { 85 using (IRedisClient rdc = Prcm.GetClient()) 86 { 87 rdc.Remove(key); 88 } 89 } 90 91 /// <summary> 92 /// 缓存中是否包含查询的键数据,使用 var isTrue=RedisHelper.ContainsKey(key); 93 /// </summary> 94 /// <param name="key">键</param> 95 /// <returns>如果包含,返回true,否则返回false</returns> 96 public static bool ContainsKey(string key) 97 { 98 using (IRedisClient rdc = Prcm.GetReadOnlyClient()) 99 {100 return rdc.ContainsKey(key);101 }102 }103 104 /// <summary>105 /// 给缓存中添加Object对象,使用:RedisHelper.Add(key,值(需要存放的值))106 /// </summary>107 /// <param name="key">键</param>108 /// <param name="value">值</param>109 public static void Add(string key, object value)110 {111 using (IRedisClient rdc = Prcm.GetClient())112 {113 if (!rdc.ContainsKey(key))114 {115 rdc.Add(key, value, DateTime.Now.AddMinutes(30));116 }117 else118 {119 rdc.Set(key, value);120 }121 }122 }123 124 /// <summary>125 /// 根据key刷新缓存中的数据信息,使用:RedisHelper.RefreshCache(key)126 /// </summary>127 /// <typeparam name="T">缓存类型</typeparam>128 /// <param name="key">键</param>129 public static void RefreshCache<T>(string key) where T : class130 {131 using (IRedisClient rdc = Prcm.GetClient())132 {133 var value = rdc.Get<T>(key);134 rdc.Remove(key);135 rdc.Set<T>(key, value);136 }137 }138 139 /// <summary>140 /// 根据key集合信息读取缓存中的键值对,返回字典形式的数据存放,使用:RedisHelper.GetList(keys);141 /// </summary>142 /// <param name="keys">key集合</param>143 /// <returns>返回字典集合</returns>144 public static Dictionary<string, string> GetList(List<string> keys)145 {146 using (IRedisClient rdc = Prcm.GetReadOnlyClient())147 {148 return rdc.GetValuesMap<string>(keys);149 }150 }151 152 /// <summary>153 /// 将字典集合添加到缓存中,使用:RedisHelper.Set(values);154 /// </summary>155 /// <param name="values">字典集合信息</param>156 public static void Set(Dictionary<string, string> values)157 {158 using (IRedisClient rdc = Prcm.GetReadOnlyClient())159 {160 rdc.SetAll(values);161 }162 }163 164 }165 }
新闻热点
疑难解答