概念
Windows安裝Redis
以cmd安裝方法:
1.下載安裝包:https://github.com/dmajkic/redis/downloads
2.安裝包下載后根據操作系統選擇對應版本文件,里面會有幾個dll分別為:
redis-server.exe:服務程序
redis-check-dump.exe:本地數據庫檢查
redis-check-aof.exe:更新日志檢查
redis-benchmark.exe:性能測試,用以模擬同時由N個客戶端發送M個 SETs/GETs 查詢.
redis-cli.exe: 服務端開啟后,我們的客戶端就可以輸入各種命令測試了
首先以管理員身份打開cmd (窗口+R),進入到安裝包下載的位置。輸入:redis-server.exe redis.conf 開啟Redis服務。提示信息沒有報錯表示啟動成功。
此窗口要保持開啟狀態,如果關閉Redis服務也會相即被關閉。使用客戶端測試一下數據。
現在來觀察Redis是怎么持久化存儲數據到硬盤上。(快照是默認的持久化方式,默認的文件名稱為dump.rdb)
可以看到Redis服務端在一段時間后將數據庫保存在磁盤上,文件為:dump.rdb。
以weindows服務安裝Redis方法:
下載Redis服務安裝包:https://github.com/rgl/redis/downloads
下載完成后直接點擊.exe下一步下一步OK。安裝完后我們會在windows服務中找到Redis Service服務。注意啟動服務后在進行相關測試。
在調用Redis服務前需要准備三個DLL。下載地址:【Redis調用驅動】在項目中引用即可。
使用Redis中存儲常用的5種數據類型:String,Hash,List,SetSorted set編寫實例代碼
安裝參考:https://www.cnblogs.com/caokai520/p/4409712.html
Redis配置文件說明參考:https://www.cnblogs.com/qq78292959/archive/2013/09/21/3331032.html
安裝客戶端
Redis Desktop Manager
一款基於Qt5的跨平台Redis桌面管理軟件
支持: Windows 7+, Mac OS X 10.10+, Ubuntu 14+
特點: C++ 編寫,響應迅速,性能好。但不支持數據庫備份與恢復。
項目地址: https://github.com/uglide/RedisDesktopManager
Redis Client
項目簡介: 使用Java編寫,功能豐富,缺點是性能稍差,網絡不好時,會不時斷線。
項目地址: https://github.com/caoxinyu/RedisClient
Redis Studio
項目簡介: 又一個C++編寫的redis管理工具,僅支持windows平台,支持xp操作系統。
項目地址: https://github.com/cinience/RedisStudio
redisClient的安裝及基本使用
RedisClient是Redis客戶端的GUI工具,使用Java swt和jedis編寫,可以方便開發者瀏覽Redis數據庫。該軟件支持簡體中文,非常適合國內用戶使用,不需要漢化就可以直接使用
Redis客戶端工具(RedisClient)功能
RedisClient將redis數據以資源管理器的界面風格呈現給用戶,可以幫助redis開發人員和維護人員方便的建立,修改,刪除,查詢redis數據,完全不需要了解redis命令。可以讓用戶方便的編輯數據,可以剪切,拷貝,粘貼redis數據,可以導入,導出redis數據,可以對redis數據排序。
1、使用服務器管理,支持服務器密碼認證
2、根據喜好管理redis數據
3、管理redis數據,包括:
新的redis數據:字符串String,列表List,哈希hash,集合set,有序集合Sorted set。
參考地址:https://blog.csdn.net/chinafire525/article/details/84104410
代碼示例
static void Main(string[] args) { //在Redis中存儲常用的5種數據類型:String,Hash,List,SetSorted set RedisClient client = new RedisClient("172.21.0.192", 6379); client.FlushAll(); #region string client.Add<string>("StringValueTime", "我已設置過期時間噢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("數據類型為: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("數據類型為: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是一個鏈表結構,主要功能是push,pop,獲取一個范圍的所有的值等,操作中key理解為鏈表名字。 * Redis的list類型其實就是一個每個子元素都是string類型的雙向鏈表。我們可以通過push,pop操作從鏈表的頭部或者尾部添加刪除元素, * 這樣list既可以作為棧,又可以作為隊列。Redis list的實現為一個雙向鏈表,即可以支持反向查找和遍歷,更方便操作,不過帶來了部分額外的內存開銷, * Redis內部的很多實現,包括發送緩沖隊列等也都是用的這個數據結構 */ client.EnqueueItemOnList("QueueListId", "1.張三"); //入隊 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出隊值:{0}", client.DequeueItemFromList("QueueListId")); //出隊(隊列先進先出) } 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")); //出棧(棧先進后出) } #endregion #region Set無序集合 /* 它是string類型的無序集合。set是通過hash table實現的,添加,刪除和查找,對集合我們可以取並集,交集,差集 */ 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); //出來的結果是無須的 } 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); //出來的結果是無須的 } 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" }); //[返回存在於第一個集合,但是不存在於其他集合的數據。差集] foreach (string item in hashD) { Console.WriteLine("求Set1001和Set1002的差集:{0}", item); //差集 } #endregion #region SetSorted 有序集合 /* sorted set 是set的一個升級版本,它在set的基礎上增加了一個順序的屬性,這一屬性在添加修改.元素的時候可以指定, * 每次指定后,zset(表示有序集合)會自動重新按新的值調整順序。可以理解為有列的表,一列存 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 }
輸出結果: