C# Redis五種資料型別的操作--簡易易懂
前段時間學習了Redis,一直在忙著工作和一些其他的事情,這兩天有空了,就把這學習到的技術分享出來給大家,謝謝
- Redis是什麼?
- Redis的特點
- Redis使用場景
- Redis資料型別
一、.Redis是什麼?
Redis是一個完全免費開源的,基於記憶體的高效能的key-value儲存系統,可以用作資料庫、快取和訊息中介軟體。支援多種型別的資料結構.
Redis內建資料持久化、LRU驅動事件、事物、主從複製、哨兵機制、叢集、自動分割槽、lua指令碼提供高可用性..
Redis全稱為:Remote Dictionary Server (遠端資料服務)
Redis是一種非關係型資料庫
二、Redis的特點
Redis以記憶體作為資料儲存介質,讀寫資料的效率極高。速度快:使用標準c語言編寫,所有資料在記憶體儲存,讀速度:110000次/s 寫速度:81000次/s
Redis跟memcache不同的是,儲存在Redis中的資料是持久化的,斷電或重啟,資料也不會丟失。
Redis的儲存分為記憶體儲存、磁碟儲存和log檔案。
Redis可以從磁碟重新將資料載入到記憶體中,也可以通過配置檔案對其進行配置,因此,redis才能實現持久化。
Redis支援主從模式,可以配置叢集,更利於支撐大型的專案。
Redis是單執行緒:一次只能執行一條命令,拒絕長命令(因為Redis基於記憶體,不牽扯磁碟IO操作限制)
三、Redis應用場景
快取: 配合關係型資料庫做快取記憶體(string),會話快取(最常用)
訊息佇列
活動排行榜,計數器: 使用者點贊,評論數,投票,網站訪問量,點選率等(zset)
釋出,訂閱訊息(訊息通知)
商品列表,評論列表
分散式鎖: 分散式環境下,訪問共享資源(string)
分散式session: 分散式環境下,需要session共享(string)
使用者資訊,釋出文章資訊等(hash)
朋友圈,微博時間線,自動補全聯絡人(list)
抽獎系統,給使用者新增標籤,給標籤新增使用者、共同關注
GEO(計算兩地距離,外賣小哥距你還有多少米)
四、Redis資料型別(這裡的案例都是用C#控制檯程式做的,不是特別全面)
Redis有五種資料型別(String,Hash,Set,ZSet,List )
首先搭建一個控制檯應用程式,新增應用(Nuget裡面去找)如圖:
就會得到相應的引用
1.String
/// <summary> /// string /// </summary> public static void TestString() { using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //清空Redis裡面的所有快取 client.FlushAll(); //儲存 client.Set<string>("name", "admin"); client.Set("password", "123456"); //讀取 string name = client.Get<string>("name"); string pwd = client.Get<string>("password"); Console.WriteLine(name); Console.WriteLine(pwd); } }
//結果如圖
2.Hash
/// <summary> /// Hash /// </summary> public static void TestHash() { using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //清空Redis裡面的所有快取 client.FlushAll(); //配置資料 Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add("a", "1"); dictionary.Add("b", "2"); dictionary.Add("c", "3"); dictionary.Add("d", "4"); dictionary.Add("e", "5"); //快取 client.SetRangeInHash("dictionary", dictionary); //追加 client.SetEntryInHash("dictionary", "666", "fgh"); //獲取存入的資料 Dictionary<string, string> hashData = client.GetAllEntriesFromHash("dictionary"); foreach (var item in hashData) { Console.WriteLine($"Key是:{item.Key}------Vaule值是:{item.Value}"); } } }
//結果如圖
3.Set
/// <summary> /// Set /// </summary> public static void TestSet() { using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //清空Redis裡面的所有快取 client.FlushAll(); client.AddItemToSet("微信A", "好友A"); client.AddItemToSet("微信A", "好友B"); client.AddItemToSet("微信A", "好友C"); client.AddItemToSet("微信A", "好友D"); client.AddItemToSet("微信A", "好友2"); client.AddItemToSet("微信B", "好友1"); client.AddItemToSet("微信B", "好友A"); client.AddItemToSet("微信B", "好友D"); client.AddItemToSet("微信B", "好友F"); client.AddItemToSet("微信B", "好友G"); //獲取交集(獲取相同的好友) var setunion = client.GetIntersectFromSets("微信A", "微信B"); Console.WriteLine("微信A和微信B的共同好友為:"); foreach (var item in setunion) { Console.WriteLine(item); } } }
//結果為
//這是快取客戶端看到的快取內容
4.Zset
/// <summary> /// Zset /// </summary> public static void TestZSet() { using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //清空Redis裡面的所有快取 client.FlushAll(); client.AddItemToSortedSet("主播安妮", "粉絲1", 50); client.AddItemToSortedSet("主播安妮", "粉絲2", 20); client.AddItemToSortedSet("主播安妮", "粉絲3", 68); client.AddItemToSortedSet("主播安妮", "粉絲4", 31); client.IncrementItemInSortedSet("主播安妮", "粉絲4", new Random().Next(200, 500)); var TopList = client.GetAllItemsFromSortedSetDesc("主播安妮"); Console.WriteLine("刷禮物排行榜為"); foreach (var item in TopList) { Console.WriteLine(item); } } }
//結果為
5.List
/// <summary> /// List /// </summary> public static void TestList() { using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //清空Redis裡面的所有快取 redisClient.FlushAll(); //隊(在前面加入) client.EnqueueItemOnList("QueueList", "列印任務1"); client.EnqueueItemOnList("QueueList", "列印任務2"); client.EnqueueItemOnList("QueueList", "列印任務3"); client.EnqueueItemOnList("QueueList", "列印任務4"); //獲取QueueList快取個數 long q = client.GetListCount("QueueList"); for (int i = 0; i < q; i++) { //【先進先出】取值的時候先取 “入棧操作1-->2-->3-->4” Console.WriteLine("QueueList出隊值:{0}", client.DequeueItemFromList("QueueList")); } Console.WriteLine("---------------------------------------------------------------"); //棧(在後面加入) client.PushItemToList("StackList", "入棧操作1"); client.PushItemToList("StackList", "入棧操作2"); client.PushItemToList("StackList", "入棧操作3"); client.PushItemToList("StackList", "入棧操作4"); long p = client.GetListCount("StackList"); for (int i = 0; i < p; i++) { //【後進先出】取值的時候先取 “入棧操作4-->3-->2-->1” Console.WriteLine("StackList出棧值:{0}", client.PopItemFromList("StackList")); } } }
//結果
//這個比較複雜一點,需要結合輸出值對應著看