Redis是一個開源(BSD許可),內存存儲的數據結構服務器,可用做數據庫,高速緩存和消息隊列代理。它支持字符串、哈希表、列表、集合、有序集合,位圖,hyperloglogs等數據類型。內置複製、Lua腳本、LRU收回、事務以及不一樣級別磁盤持久化功能,同時經過Redis Sentinel提供高可用,經過Redis Cluster提供自動分區。sd html
Redis官網上相關安裝、命令、配置等信息寫的已經很是清楚(查看官網) 我就簡單介紹下。git
(1)下載github
Windows 環境生成好的文件下載地址下載地址 (這裏版本比較老舊。你能夠經過其餘途徑下載別人生成好的文件。這裏有redis-2.8.17 ), 這裏版本可能已經比較老舊,新版本能夠本身下載源碼生成github.com下載地址(msvs文件夾裏有解決方案能夠用Vs2013生成,這裏須要更新到update5)。redis
下載完文件解壓目錄以下:數據庫
(2)安裝:緩存
配置文件中幾個比較經常使用配置文件服務器
1. port 6379 端口號 數據結構
2. bind 127.0.0.1 IP 負載均衡
3. requirepass 訪問的密碼 測試
4. maxheap 記得把這個配置節點打開,否者redis 服務沒法啓動。例如maxheap 1024000000(我本身用3.0.504測試時須要註釋掉這個節點)
5. timeout:請求超時時間
其餘配置說明。配置文件中有比較清楚的英文描述。看不懂英文?。不要緊。下載中有配中文描述。
配置好配置文件。直接點擊startup.bat就能夠了。這時你會看到以下
當你看到這個頁面的時候說明。你的服務已經成功啓動了。(這個頁面不能關。關了服務就中止了)
若是沒有一閃而沒。可能你配置或在其餘錯誤。咱們就老老實實的輸入cmd-》文件目錄-》執行命令。根據日誌找問題並解決問題
下面咱們來作個存儲測試。
另開個窗口(服務窗口不能關閉)
測試結果以下
客戶端語句以下
$ redis-cli -h host -p port -a password
若是須要作多臺機器負載均衡只須要在另外一臺機器上配置信息
# slaveof <masterip> <masterport> 這個節點配置上就能夠了。
一、下載相關dll
二、經常使用方法整理
public class RedisCacheHelper { private static readonly PooledRedisClientManager pool = null; private static readonly string[] writeHosts = null; private static readonly string[] readHosts = null; public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]); public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]); static RedisCacheHelper() { var redisWriteHost = ConfigurationManager.AppSettings["redis_server_write"]; var redisReadHost = ConfigurationManager.AppSettings["redis_server_read"]; if (!string.IsNullOrEmpty(redisWriteHost)) { writeHosts = redisWriteHost.Split(','); readHosts = redisReadHost.Split(','); if (readHosts.Length > 0) { pool = new PooledRedisClientManager(writeHosts, readHosts, new RedisClientManagerConfig() { MaxWritePoolSize = RedisMaxWritePool, MaxReadPoolSize = RedisMaxReadPool, AutoStart = true }); } } } public static void Add<T>(string key, T value, DateTime expiry) { if (value == null) { return; } if (expiry <= DateTime.Now) { Remove(key); return; } try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Set(key, value, expiry - DateTime.Now); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "存儲", key); } } public static void Add<T>(string key, T value) { RedisCacheHelper.Add<T>(key, value, DateTime.Now.AddDays(1)); } public static void Add<T>(string key, T value, TimeSpan slidingExpiration) { if (value == null) { return; } if (slidingExpiration.TotalSeconds <= 0) { Remove(key); return; } try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Set(key, value, slidingExpiration); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "存儲", key); } } public static T Get<T>(string key) { if (string.IsNullOrEmpty(key)) { return default(T); } T obj = default(T); try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; obj = r.Get<T>(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "獲取", key); } return obj; } public static void Remove(string key) { try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Remove(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "刪除", key); } } public static bool Exists(string key) { try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; return r.ContainsKey(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "是否存在", key); } return false; } public static IDictionary<string, T> GetAll<T>(IEnumerable<string> keys) where T : class { if (keys == null) { return null; } keys = keys.Where(k => !string.IsNullOrWhiteSpace(k)); if (keys.Count() == 1) { T obj = Get<T>(keys.Single()); if (obj != null) { return new Dictionary<string, T>() { { keys.Single(), obj } }; } return null; } if (!keys.Any()) { return null; } try { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; return r.GetAll<T>(keys); } } } catch (Exception ex) { string msg = string.Format("{0}:{1}發生異常!{2}", "cache", "獲取", keys.Aggregate((a, b) => a + "," + b)); } return null; } }
設置配置信息
這時你就能夠
RedisCacheHelper.Add<string>(key, value);
這樣調用了。