备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地址。
1.前言
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/
2.代码展示
1 // 源文件头信息: 2 //3 // Copyright(c)2014-2034 Kencery.All rights reserved. 4 // 个人博客:http://www.cnblogs.com/hanyinglong 5 // 创建人:韩迎龙(kencery) 6 // 创建时间:2015-8-18 7 // 8 9 using System; 10 using System.Collections.Generic; 11 using System.Configuration; 12 using ServiceStack.Redis; 13 14 namespace KenceryCommonMethod 15 { 16 ///17 /// Redis缓存读取设置 封装 18 /// 23 public static class RedisHelper 24 { 25 ///19 /// 22 ///Kencery 20 ///2015-8-18 21 ///26 /// 创建Redis连接池管理对象(添加ServiceStack.Interfaces.dll、ServiceStack.Redis.dll) 27 /// 28 /// 只写服务器 29 /// 只读服务器 30 ///31 private static PooledRedisClientManager CreateRedisManager(IEnumerable readWriteHosts, 32 IEnumerable 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 /// 44 /// 调用CreateRedisManager方法,创建连接池管理对象,Redis服务器地址在配置文件中配置(创建只读,只写连接池) 45 /// 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 ///46 /// 52 /// 给缓存中添加数据,使用:RedisHelper.Set(key,值(需要存放的值)); 53 /// 54 ///缓存类型 55 /// 键 56 /// 值 57 public static void Set(string key, T val) 58 { 59 using (IRedisClient rdc = Prcm.GetClient()) 60 { 61 rdc.Set (key, val); 62 } 63 } 64 65 /// 66 /// 读取缓存中的数据,使用:var result=RedisHelper.Get 68 ///(key); 67 /// 返回读取的数据 69 /// 键 70 ///71 public static T Get (string key) where T : class 72 { 73 using (IRedisClient rdc = Prcm.GetReadOnlyClient()) 74 { 75 return rdc.Get (key); 76 } 77 } 78 79 /// 80 /// 删除缓存中的数据,使用 RedisHelper.Remove(key); 81 /// 82 /// 键 83 public static void Remove(string key) 84 { 85 using (IRedisClient rdc = Prcm.GetClient()) 86 { 87 rdc.Remove(key); 88 } 89 } 90 91 ///92 /// 缓存中是否包含查询的键数据,使用 var isTrue=RedisHelper.ContainsKey(key); 93 /// 94 /// 键 95 ///如果包含,返回true,否则返回false 96 public static bool ContainsKey(string key) 97 { 98 using (IRedisClient rdc = Prcm.GetReadOnlyClient()) 99 {100 return rdc.ContainsKey(key);101 }102 }103 104 ///105 /// 给缓存中添加Object对象,使用:RedisHelper.Add(key,值(需要存放的值))106 /// 107 /// 键108 /// 值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 ///125 /// 根据key刷新缓存中的数据信息,使用:RedisHelper.RefreshCache(key)126 /// 127 ///缓存类型 128 /// 键129 public static void RefreshCache(string key) where T : class130 {131 using (IRedisClient rdc = Prcm.GetClient())132 {133 var value = rdc.Get (key);134 rdc.Remove(key);135 rdc.Set (key, value);136 }137 }138 139 /// 140 /// 根据key集合信息读取缓存中的键值对,返回字典形式的数据存放,使用:RedisHelper.GetList(keys);141 /// 142 /// key集合143 ///返回字典集合 144 public static DictionaryGetList(List keys)145 {146 using (IRedisClient rdc = Prcm.GetReadOnlyClient())147 {148 return rdc.GetValuesMap (keys);149 }150 }151 152 /// 153 /// 将字典集合添加到缓存中,使用:RedisHelper.Set(values);154 /// 155 /// 字典集合信息156 public static void Set(Dictionaryvalues)157 {158 using (IRedisClient rdc = Prcm.GetReadOnlyClient())159 {160 rdc.SetAll(values);161 }162 }163 164 }165 }