基於.Net Core的Redis:基本資料型別及其應用場景與命令列操作
參考自:https://blog.csdn.net/only_yu_yy/article/details/78873735
https://blog.csdn.net/fenghuoliuxing990124/article/details/84983694
1、使用的Redis客戶端為:ServiceStack.Redis
開啟“程式包管理器控制檯”,輸入並執行“Install-Package ServiceStack.Redis”即可。
2、Redis基本資料型別:String
String型別是最常用的資料型別,在Redis中以KKey/Value儲存。
1 usingView CodeServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Text; 5 6 namespace RedisDemo 7 { 8 class StringDemo 9 { 10 public static void Start() 11 { 12 var redisMangement = new RedisManagerPool("127.0.0.1:6379"); 13 varclient = redisMangement.GetClient(); 14 15 //---字串--- 16 //set key value 17 //summary: Set the string value of a key 18 client.Set<int>("pwd", 111); 19 //get key 20 //summary: Get the value of a key 21 int pwd = client.Get<int>("pwd"); 22 Console.WriteLine(pwd); 23 24 //---物件--- 25 var todos = client.As<Todo>(); 26 Todo todo = new Todo 27 { 28 Id = todos.GetNextSequence(), 29 Content = "String Demo", 30 Order = 1 31 }; 32 client.Set<Todo>("todo", todo); 33 var getTodo = client.Get<Todo>("todo"); 34 Console.WriteLine(getTodo.Content); 35 } 36 } 37 }
String的應用場景
計數器:許多運用都會使用redis作為計數的基礎工具,他可以實現快速計數、查詢快取的功能。
比如:優酷視訊的播放:incr video:videoId:playTimes
或者:文章瀏覽量:incr article:aricleId:clickTimes
或者粉絲數量:取關 decr author:authorId:fansNumber
3、Redis基本資料型別:Hash
Hash在Redis採用 (HashId,Key,Value)進行儲存,一個HashId 可以包含多個key,一個key對應著一個value。
1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Text; 5 6 namespace RedisDemo 7 { 8 class HashDemo 9 { 10 public static void Start() 11 { 12 var redisMangement = new RedisManagerPool("127.0.0.1:6379"); 13 var client = redisMangement.GetClient(); 14 15 //HSET key field value 16 //summary: Set the string value of a hash field 17 client.SetEntryInHash("test", "name", "ermao"); 18 client.SetEntryInHash("test", "age", "26"); 19 20 //---獲取test雜湊下的所有key--- 21 //HKEYS key 22 //summary: Get all the fields in a hash 23 List<string> hashKeys = client.GetHashKeys("test"); 24 Console.WriteLine("keys in test"); 25 foreach (var item in hashKeys) 26 { 27 Console.WriteLine(item); 28 } 29 30 //---獲取test雜湊下的所有值--- 31 //HVALS key 32 //summary: Get all the values in a hash 33 List<string> hashValues = client.GetHashValues("test"); 34 Console.WriteLine("values in test"); 35 foreach (var item in hashValues) 36 { 37 Console.WriteLine(item); 38 } 39 40 //---獲取test雜湊下,第一個Key對應的值--- 41 //HGET key field 42 //summary: Get the value of a hash field 43 string value = client.GetValueFromHash("test", hashKeys[0]); 44 Console.WriteLine($"test下的key{hashKeys[0]}對應的值{value}"); 45 } 46 } 47 }View Code
Hash的應用場景
商品詳情頁
4、Redis基本資料型別:List
list是一個連結串列結構,key可以理解為連結串列的名字,然後往這個名字所對應的連結串列里加值。,list可以以佇列 / 棧的形式進行工作。
1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Text; 5 6 namespace RedisDemo 7 { 8 class ListDemo 9 { 10 public static void Start() 11 { 12 var redisMangement = new RedisManagerPool("127.0.0.1:6379"); 13 var client = redisMangement.GetClient(); 14 15 //---佇列的使用(先進先出)--- 16 client.EnqueueItemOnList("name", "zhangsan"); 17 client.EnqueueItemOnList("name", "lisi"); 18 long count = client.GetListCount("name"); 19 for (int i = 0; i < count; i++) 20 { 21 Console.WriteLine(client.DequeueItemFromList("name")); 22 } 23 24 //---棧的使用(先進後出)--- 25 client.PushItemToList("course", "Math"); 26 client.PushItemToList("course", "English"); 27 long count2 = client.GetListCount("course"); 28 for (int i = 0; i < count2; i++) 29 { 30 Console.WriteLine(client.PopItemFromList("course")); 31 } 32 } 33 } 34 }View Code
List的應用場景
點贊:
建立一條微博內容:set user:1:post:91 “hello redis”;
點贊:
lpush post:91:good “kobe.png”
lpush post:91:good “jordan.png”
lpush post:91:good “James.png”
檢視有多少人點贊:llen post:91:good
檢視有哪些人點贊:lrange post:91:good 0 -1
5、Redis基本資料型別:Set
它是去重、無序集合。set是通過hash table實現的,新增,刪除和查詢,對集合我們可以取並集,交集,差集。
1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Text; 5 6 namespace RedisDemo 7 { 8 class SetDemo 9 { 10 public static void Start() 11 { 12 var redisMangement = new RedisManagerPool("127.0.0.1:6379"); 13 var client = redisMangement.GetClient(); 14 15 //SADD key member [member ...] 16 //summary: Add one or more members to a set 17 client.AddItemToSet("s1", "abc"); 18 client.AddItemToSet("s1", "qwer"); 19 client.AddItemToSet("s1", "asdf"); 20 client.AddItemToSet("s1", "hjkl"); 21 client.AddItemToSet("s1", "zxc"); 22 //SMEMBERS key 23 //summary: Get all the members in a set 24 HashSet<string> hashSet = client.GetAllItemsFromSet("s1"); 25 foreach (var item in hashSet) 26 { 27 Console.WriteLine(item); 28 } 29 30 client.AddItemToSet("s2", "qwer"); 31 client.AddItemToSet("s2", "wasd"); 32 33 //SUNION key [key ...] 34 //summary: Add multiple sets 35 HashSet<string> hashSetUnion = client.GetUnionFromSets(new string[] { "s1", "s2" }); 36 Console.WriteLine("---並集---"); 37 foreach (var item in hashSetUnion) 38 { 39 Console.WriteLine(item); 40 } 41 42 //SINTER key [key ...] 43 //summary: Intersect multiple sets 44 HashSet<string> hashSetInter = client.GetIntersectFromSets(new string[] { "s1", "s2" }); 45 Console.WriteLine("---交集---"); 46 foreach (var item in hashSetInter) 47 { 48 Console.WriteLine(item); 49 } 50 51 //SDIFF key [key ...] 52 //summary: Subtract multiple sets 53 HashSet<string> hashSetDifference = client.GetDifferencesFromSet("s1", new string[] { "s2" }); 54 Console.WriteLine("---差集---"); 55 foreach (var item in hashSetDifference) 56 { 57 Console.WriteLine(item); 58 } 59 } 60 } 61 }View Code
Set的應用場景
隨機事件(如:抽獎)、共同好友、推薦好友等
6、Redis基本資料型別:SortedSet
set是一種非常方便的結構,但是資料無序,redis提供了一個sorted set,每一個新增的值都有一個對應的分數,放進去的值按照該分數升序存在一個集合中,可以通過這個分數進行相關排序的操作。
1 using ServiceStack.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Text; 5 6 namespace RedisDemo 7 { 8 class SortedSetDemo 9 { 10 public static void Start() 11 { 12 var redisMangement = new RedisManagerPool("127.0.0.1:6379"); 13 var client = redisMangement.GetClient(); 14 15 //ZADD key [NX|XX] [CH] [INCR] score member [score member ...] 16 //summary: Add one or more members to a sorted set, or update its score if it already exists 17 client.AddItemToSortedSet("grade", "Chinese", 82); 18 client.AddItemToSortedSet("grade", "Math", 96); 19 client.AddItemToSortedSet("grade", "English", 91); 20 client.AddItemToSortedSet("grade", "History", 97); 21 //ZREVRANGE key start stop [WITHSCORES] 22 //summary: Return a range of members in a sorted set, by index, with scores ordered from high to low 23 List<string> sortedList = client.GetAllItemsFromSortedSetDesc("grade"); 24 foreach (var item in sortedList) 25 { 26 Console.WriteLine(item); 27 } 28 } 29 } 30 }View Code
SortedSet的應用場景
排行榜(如:微博熱搜排行榜)