参考自:https://blog.csdn.net/only_yu_yy/article/details/78873735
https://blog.csdn.net/fenghuoliuxing990124/article/details/84983694
1 using ServiceStack.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 var client = 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
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 }
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 }
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
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 }
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 }
SortedSet的应用场景
排行榜(如:微博热搜排行榜)
参考自: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存储。
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace RedisDemo
{
class StringDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
//---字符串---
//set key value
//summary: Set the string value of a key
client.Set<int>("pwd", 111);
//get key
//summary: Get the value of a key
int pwd = client.Get<int>("pwd");
Console.WriteLine(pwd);
//---对象---
var todos = client.As<Todo>();
Todo todo = new Todo
{
Id = todos.GetNextSequence(),
Content = "String Demo",
Order = 1
};
client.Set<Todo>("todo", todo);
var getTodo = client.Get<Todo>("todo");
Console.WriteLine(getTodo.Content);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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。
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace RedisDemo
{
class HashDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
//HSET key field value
//summary: Set the string value of a hash field
client.SetEntryInHash("test", "name", "ermao");
client.SetEntryInHash("test", "age", "26");
//---获取test哈希下的所有key---
//HKEYS key
//summary: Get all the fields in a hash
List<string> hashKeys = client.GetHashKeys("test");
Console.WriteLine("keys in test");
foreach (var item in hashKeys)
{
Console.WriteLine(item);
}
//---获取test哈希下的所有值---
//HVALS key
//summary: Get all the values in a hash
List<string> hashValues = client.GetHashValues("test");
Console.WriteLine("values in test");
foreach (var item in hashValues)
{
Console.WriteLine(item);
}
//---获取test哈希下,第一个Key对应的值---
//HGET key field
//summary: Get the value of a hash field
string value = client.GetValueFromHash("test", hashKeys[0]);
Console.WriteLine($"test下的key{hashKeys[0]}对应的值{value}");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Hash的应用场景
商品详情页
4、Redis基本数据类型:List
list是一个链表结构,key可以理解为链表的名字,然后往这个名字所对应的链表里加值。,list可以以队列 / 栈的形式进行工作。
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace RedisDemo
{
class ListDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
//---队列的使用(先进先出)---
client.EnqueueItemOnList("name", "zhangsan");
client.EnqueueItemOnList("name", "lisi");
long count = client.GetListCount("name");
for (int i = 0; i < count; i++)
{
Console.WriteLine(client.DequeueItemFromList("name"));
}
//---栈的使用(先进后出)---
client.PushItemToList("course", "Math");
client.PushItemToList("course", "English");
long count2 = client.GetListCount("course");
for (int i = 0; i < count2; i++)
{
Console.WriteLine(client.PopItemFromList("course"));
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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实现的,添加,删除和查找,对集合我们可以取并集,交集,差集。
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace RedisDemo
{
class SetDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
//SADD key member [member ...]
//summary: Add one or more members to a set
client.AddItemToSet("s1", "abc");
client.AddItemToSet("s1", "qwer");
client.AddItemToSet("s1", "asdf");
client.AddItemToSet("s1", "hjkl");
client.AddItemToSet("s1", "zxc");
//SMEMBERS key
//summary: Get all the members in a set
HashSet<string> hashSet = client.GetAllItemsFromSet("s1");
foreach (var item in hashSet)
{
Console.WriteLine(item);
}
client.AddItemToSet("s2", "qwer");
client.AddItemToSet("s2", "wasd");
//SUNION key [key ...]
//summary: Add multiple sets
HashSet<string> hashSetUnion = client.GetUnionFromSets(new string[] { "s1", "s2" });
Console.WriteLine("---并集---");
foreach (var item in hashSetUnion)
{
Console.WriteLine(item);
}
//SINTER key [key ...]
//summary: Intersect multiple sets
HashSet<string> hashSetInter = client.GetIntersectFromSets(new string[] { "s1", "s2" });
Console.WriteLine("---交集---");
foreach (var item in hashSetInter)
{
Console.WriteLine(item);
}
//SDIFF key [key ...]
//summary: Subtract multiple sets
HashSet<string> hashSetDifference = client.GetDifferencesFromSet("s1", new string[] { "s2" });
Console.WriteLine("---差集---");
foreach (var item in hashSetDifference)
{
Console.WriteLine(item);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Set的应用场景
随机事件(如:抽奖)、共同好友、推荐好友等
6、Redis基本数据类型:SortedSet
set是一种非常方便的结构,但是数据无序,redis提供了一个sorted set,每一个添加的值都有一个对应的分数,放进去的值按照该分数升序存在一个集合中,可以通过这个分数进行相关排序的操作。
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace RedisDemo
{
class SortedSetDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
//ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
//summary: Add one or more members to a sorted set, or update its score if it already exists
client.AddItemToSortedSet("grade", "Chinese", 82);
client.AddItemToSortedSet("grade", "Math", 96);
client.AddItemToSortedSet("grade", "English", 91);
client.AddItemToSortedSet("grade", "History", 97);
//ZREVRANGE key start stop [WITHSCORES]
//summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
List<string> sortedList = client.GetAllItemsFromSortedSetDesc("grade");
foreach (var item in sortedList)
{
Console.WriteLine(item);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
SortedSet的应用场景
排行榜(如:微博热搜排行榜)
————————————————
版权声明:本文为CSDN博主「weixin_41834782」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41834782/article/details/107555138