Redis是一個開源的使用ANSIC語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。在此基礎(chǔ)上,redis支持各種不同方式的排序。與memcached一樣,為了保證效率,數(shù)據(jù)都是緩存在內(nèi)存中。區(qū)別的是redis會周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎(chǔ)上實(shí)現(xiàn)了master-slave(主從)同步。
Redis支持主從同步。數(shù)據(jù)可以從主服務(wù)器向任意數(shù)量的從服務(wù)器上同步,從服務(wù)器可以是關(guān)聯(lián)其他從服務(wù)器的主服務(wù)器。這使得Redis可執(zhí)行單層樹復(fù)制。存盤可以有意無意的對數(shù)據(jù)進(jìn)行寫操作。
Redis支持兩種持久化方式:
(1):snapshotting(快照)也是默認(rèn)方式.(把數(shù)據(jù)做一個備份,將數(shù)據(jù)存儲到文件)
(2)Append-only file(縮寫aof)的方式
快照是默認(rèn)的持久化方式,這種方式是將內(nèi)存中數(shù)據(jù)以快照的方式寫到二進(jìn)制文件中,默認(rèn)的文件名稱為dump.rdb.可以通過配置設(shè)置自動做快照持久化的方式。我們可以配置redis在n秒內(nèi)如果超過m個key鍵修改就自動做快照.
aof方式:由于快照方式是在一定間隔時間做一次的,所以如果redis意外down掉的話,就會丟失最后一次快照后的所有修改。aof比快照方式有更好的持久化性,是由于在使用aof時,redis會將每一個收到的寫命令都通過write函數(shù)追加到文件中,當(dāng)redis重啟時會通過重新執(zhí)行文件中保存的寫命令來在內(nèi)存中重建整個數(shù)據(jù)庫的內(nèi)容。
以cmd安裝方法:
1.下載安裝包:https://github.com/dmajkic/redis/downloads
2.安裝包下載后根據(jù)操作系統(tǒng)選擇對應(yīng)版本文件,里面會有幾個dll分別為:
redis-server.exe:服務(wù)程序 redis-check-dump.exe:本地?cái)?shù)據(jù)庫檢查 redis-check-aof.exe:更新日志檢查 redis-benchmark.exe:性能測試,用以模擬同時由N個客戶端發(fā)送M個 SETs/GETs 查詢.redis-cli.exe: 服務(wù)端開啟后,我們的客戶端就可以輸入各種命令測試了
首先以管理員身份打開cmd (窗口+R),進(jìn)入到安裝包下載的位置。輸入:redis-server.exeredis.conf 開啟Redis服務(wù)。提示信息沒有報(bào)錯表示啟動成功。

此窗口要保持開啟狀態(tài),如果關(guān)閉Redis服務(wù)也會相即被關(guān)閉。使用客戶端測試一下數(shù)據(jù)。

現(xiàn)在來觀察Redis是怎么持久化存儲數(shù)據(jù)到硬盤上。(快照是默認(rèn)的持久化方式,默認(rèn)的文件名稱為dump.rdb)

可以看到Redis服務(wù)端在一段時間后將數(shù)據(jù)庫保存在磁盤上,文件為:dump.rdb。
以weindows服務(wù)安裝Redis方法:
下載Redis服務(wù)安裝包:https://github.com/rgl/redis/downloads
下載完成后直接點(diǎn)擊.exe下一步下一步OK。安裝完后我們會在windows服務(wù)中找到Redis Service服務(wù)。注意啟動服務(wù)后在進(jìn)行相關(guān)測試。

在調(diào)用Redis服務(wù)前需要準(zhǔn)備三個DLL。下載地址:【Redis調(diào)用驅(qū)動】在項(xiàng)目中引用即可。
使用Redis中存儲常用的5種數(shù)據(jù)類型:String,Hash,List,SetSorted set編寫實(shí)例代碼

static void Main(string[] args){ //在Redis中存儲常用的5種數(shù)據(jù)類型:String,Hash,List,SetSorted set RedisClient client = new RedisClient("172.21.0.192", 6379); client.FlushAll(); #region string client.Add<string>("StringValueTime", "我已設(shè)置過期時間噢30秒后會消失", DateTime.Now.AddMilliseconds(30000)); while (true) { if (client.ContainsKey("StringValueTime")) { Console.WriteLine("String.鍵:StringValue,值:{0} {1}", client.Get<string>("StringValueTime"), DateTime.Now); Thread.Sleep(10000); } else { Console.WriteLine("鍵:StringValue,值:我已過期 {0}", DateTime.Now); break; } } client.Add<string>("StringValue", " String和Memcached操作方法差不多"); Console.WriteLine("數(shù)據(jù)類型為:String.鍵:StringValue,值:{0}", client.Get<string>("StringValue")); Student stud = new Student() { id = "1001", name = "李四" }; client.Add<Student>("StringEntity", stud); Student Get_stud = client.Get<Student>("StringEntity"); Console.WriteLine("數(shù)據(jù)類型為:String.鍵:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name); #endregion #region Hash client.SetEntryInHash("HashID", "Name", "張三"); client.SetEntryInHash("HashID", "Age", "24"); client.SetEntryInHash("HashID", "Sex", "男"); client.SetEntryInHash("HashID", "Address", "上海市XX號XX室"); List<string> HaskKey = client.GetHashKeys("HashID"); foreach (string key in HaskKey) { Console.WriteLine("HashID--Key:{0}", key); } List<string> HaskValue = client.GetHashValues("HashID"); foreach (string value in HaskValue) { Console.WriteLine("HashID--Value:{0}", value); } List<string> AllKey = client.GetAllKeys(); //獲取所有的key。 foreach (string Key in AllKey) { Console.WriteLine("AllKey--Key:{0}", Key); } #endregion #region List /* * list是一個鏈表結(jié)構(gòu),主要功能是push,pop,獲取一個范圍的所有的值等,操作中key理解為鏈表名字。 * Redis的list類型其實(shí)就是一個每個子元素都是string類型的雙向鏈表。我們可以通過push,pop操作從鏈表的頭部或者尾部添加刪除元素, * 這樣list既可以作為棧,又可以作為隊(duì)列。Redis list的實(shí)現(xiàn)為一個雙向鏈表,即可以支持反向查找和遍歷,更方便操作,不過帶來了部分額外的內(nèi)存開銷, * Redis內(nèi)部的很多實(shí)現(xiàn),包括發(fā)送緩沖隊(duì)列等也都是用的這個數(shù)據(jù)結(jié)構(gòu) */ client.EnqueueItemOnList("QueueListId", "1.張三"); //入隊(duì) client.EnqueueItemOnList("QueueListId", "2.張四"); client.EnqueueItemOnList("QueueListId", "3.王五"); client.EnqueueItemOnList("QueueListId", "4.王麻子"); int q = client.GetListCount("QueueListId"); for (int i = 0; i < q; i++) { Console.WriteLine("QueueListId出隊(duì)值:{0}", client.DequeueItemFromList("QueueListId")); //出隊(duì)(隊(duì)列先進(jìn)先出) } client.PushItemToList("StackListId", "1.張三"); //入棧 client.PushItemToList("StackListId", "2.張四"); client.PushItemToList("StackListId", "3.王五"); client.PushItemToList("StackListId", "4.王麻子"); int p = client.GetListCount("StackListId"); for (int i = 0; i < p; i++) { Console.WriteLine("StackListId出棧值:{0}", client.PopItemFromList("StackListId")); //出棧(棧先進(jìn)后出) } #endregion #region Set無序集合 /* 它是string類型的無序集合。set是通過hash table實(shí)現(xiàn)的,添加,刪除和查找,對集合我們可以取并集,交集,差集 */ client.AddItemToSet("Set1001", "小A"); client.AddItemToSet("Set1001", "小B"); client.AddItemToSet("Set1001", "小C"); client.AddItemToSet("Set1001", "小D"); HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1001"); foreach (string item in hastsetA) { Console.WriteLine("Set無序集合ValueA:{0}", item); //出來的結(jié)果是無須的 } client.AddItemToSet("Set1002", "小K"); client.AddItemToSet("Set1002", "小C"); client.AddItemToSet("Set1002", "小A"); client.AddItemToSet("Set1002", "小J"); HashSet<string> hastsetB = client.GetAllItemsFromSet("Set1002"); foreach (string item in hastsetB) { Console.WriteLine("Set無序集合ValueB:{0}", item); //出來的結(jié)果是無須的 } HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "Set1001", "Set1002" }); foreach (string item in hashUnion) { Console.WriteLine("求Set1001和Set1002的并集:{0}", item); //并集 } HashSet<string> hashG = client.GetIntersectFromSets(new string[] { "Set1001", "Set1002" }); foreach (string item in hashG) { Console.WriteLine("求Set1001和Set1002的交集:{0}", item); //交集 } HashSet<string> hashD = client.GetDifferencesFromSet("Set1001", new string[] { "Set1002" }); //[返回存在于第一個集合,但是不存在于其他集合的數(shù)據(jù)。差集] foreach (string item in hashD) { Console.WriteLine("求Set1001和Set1002的差集:{0}", item); //差集 } #endregion #region SetSorted 有序集合 /* sorted set 是set的一個升級版本,它在set的基礎(chǔ)上增加了一個順序的屬性,這一屬性在添加修改.元素的時候可以指定, * 每次指定后,zset(表示有序集合)會自動重新按新的值調(diào)整順序。可以理解為有列的表,一列存 value,一列存順序。操作中key理解為zset的名字. */ client.AddItemToSortedSet("SetSorted1001", "1.劉仔"); client.AddItemToSortedSet("SetSorted1001", "2.星仔"); client.AddItemToSortedSet("SetSorted1001", "3.豬仔"); List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted1001"); foreach (string item in listSetSorted) { Console.WriteLine("SetSorted有序集合{0}", item); } #endregion}View Code輸出結(jié)果:

Redis的存儲容災(zāi)性比較完善,所支持的存儲數(shù)據(jù)類型比較全。比較坑的是版本2.X之下都不支持服務(wù)器集群,只能單機(jī)。在Redis 3.0中服務(wù)器集群功能才亮相。 操作起來總體感覺比較簡單容易上手。
新聞熱點(diǎn)
疑難解答
圖片精選