備注:三篇博文結合閱讀,簡單理解並且使用,如果想深入學習,請多參考文章中給出的博文地址。
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.cnblogs.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 else 118 { 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 : class 130 { 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 }