我是靠谱客的博主 单身故事,这篇文章主要介绍.NET平台下Redis使用(七)【StackExchange.Redis测试Redis五种数据类型】信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀Program.cs代码:RedisDemo.cs代码:RedisHelper.cs代码:UserInfoDto.cs代码:运行结果如图:,现在分享给大家,希望可以做个参考。
信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀
Program.cs代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Program
{
static void Main(string[] args)
{
//字符串
//RedisDemo.StringTest();
//Hash
//RedisDemo.HashTest();
//List
RedisDemo.ListTest();
Console.ReadKey();
//Set
//RedisDemo.SetTest();
//SortedSet
//RedisDemo.SortedSet();
}
}
RedisDemo.cs代码:
复制代码
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
public class RedisDemo
{
public static void StringTest()
{
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "StringTest";
var db = conn.GetDatabase();
if (db.KeyExists(key))
db.KeyDelete(key);
db.StringAppend(key, DateTime.Now.ToString());
Console.WriteLine("写入字符串结束");
Console.ReadLine();
Console.WriteLine(db.StringGet(key));
Console.ReadLine();
}
}
public static void HashSetTest()
{
List<UserInfoDto> list = new List<UserInfoDto>();
for (int i = 0; i < 100; i++)
{
list.Add(new UserInfoDto()
{
Id = i,
LastLoginTime = DateTime.Now,
Password = "password" + i.ToString(),
StaffId = "StaffId_" + i.ToString(),
StaffName = "StaffName_" + i.ToString()
});
}
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "HashSetTest";
var db = conn.GetDatabase();
db.KeyDelete(key);
//string listKey = IdentityMap.CreateKey<UserInfoDto>();
HashEntry[] items = new HashEntry[list.Count];
for (int i = 0; i < list.Count - 1; i++)
{
string json = JsonConvert.SerializeObject(list[i]);
db.HashSet(key, list[i].Id, json);
//db.HashSet(key, "password", list[i].Password);
//db.HashSet(key, "StaffId", list[i].StaffId);
Console.WriteLine(db.HashGet(key, list[i].Id));
}
}
Console.ReadLine();
}
public static void SetTest()
{
List<UserInfoDto> list = new List<UserInfoDto>();
DateTime dt = DateTime.Now;
for (int i = 0; i < 10; i++)
{
list.Add(new UserInfoDto()
{
Id = i,
LastLoginTime = dt,
Password = "password" + i.ToString(),
StaffId = "StaffId_" + i.ToString(),
StaffName = "StaffName_" + i.ToString()
});
}
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "SetTest:";
var db = conn.GetDatabase();
db.KeyDelete(key);
//string listKey = IdentityMap.CreateKey<UserInfoDto>();
HashEntry[] items = new HashEntry[list.Count];
for (int i = 0; i < list.Count; i++)
{
string json = JsonConvert.SerializeObject(list[i]);
db.KeyDelete(key + list[i].Id.ToString());
//db.SetAdd(key + list[i].Id.ToString(), json);
db.SetAdd(key + list[i].Id.ToString() + ":name", list[i].StaffName);
db.SetAdd(key + list[i].Id.ToString() + ":StaffId", list[i].StaffId);
var result = db.SetScan(key, "*password99*").FirstOrDefault();
Console.WriteLine(result);
}
}
Console.WriteLine("Complete!");
Console.ReadLine();
}
public static void ListTest()
{
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "ListTest";
var db = conn.GetDatabase();
for (int i = 0; i < 10; i++)
{
db.ListLeftPush(key, "string_"+i.ToString());
}
Console.WriteLine("写入完成");
Console.ReadLine();
while (0 != db.ListLength(key))
{
//从redis数据库弹出List里的数据
var str = db.ListRightPop(key);
Console.WriteLine(str);
}
Console.ReadLine();
}
}
public static void SortedSet()
{
List<UserInfoDto> list = new List<UserInfoDto>();
for (int i = 0; i < 10; i++)
{
list.Add(new UserInfoDto()
{
Id = i,
LastLoginTime = DateTime.Now,
Password = "password" + i.ToString(),
StaffId = "StaffId_" + i.ToString(),
StaffName = "StaffName_" + i.ToString()
});
}
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "SortedSetTest:";
var db = conn.GetDatabase();
db.KeyDelete("SortedSetTest");
foreach (var item in list)
{
string json = JsonConvert.SerializeObject(item);
db.KeyDelete(key + item.Id.ToString());
db.KeyDelete("SortedSetTest" + item.Id.ToString() + ":name");
db.KeyDelete("SortedSetTest" + item.Id.ToString() + ":StaffId");
//db.SetAdd(key + list[i].Id.ToString(), json);
db.SortedSetAdd(key + item.Id.ToString() + ":name", item.StaffName, item.Id);
db.SortedSetAdd(key + item.Id.ToString() + ":StaffId", item.StaffName, item.Id);
}
Console.WriteLine("写入完成");
Console.ReadLine();
Console.WriteLine("读取两条记录");
var result = db.SortedSetRangeByRank(key, 9, 10);
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}
var result2 = db.SortedSetRangeByRankWithScores(key, 0, -1, Order.Descending);
var result3 = db.SortedSetScan(key, "*99*", 10).ToList();
for (int i = 0; i < result3.Count; i++)
{
Console.WriteLine(result3[i]);
}
Console.ReadLine();
}
}
}
RedisHelper.cs代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class RedisHelper
{
static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse("127.0.0.1" + ":" + "6379");
static ConnectionMultiplexer redisConn;
public static ConnectionMultiplexer RedisConn
{
get
{
return ConnectionMultiplexer.Connect(configurationOptions);
}
}
}
UserInfoDto.cs代码:
复制代码
1
2
3
4
5
6
7
8
public class UserInfoDto
{
public int Id { get; set; }
public string StaffId { get; set; }
public string StaffName { get; set; }
public string Password { get; set; }
public System.DateTime LastLoginTime { get; set; }
}
运行结果如图:
最后
以上就是单身故事最近收集整理的关于.NET平台下Redis使用(七)【StackExchange.Redis测试Redis五种数据类型】信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀Program.cs代码:RedisDemo.cs代码:RedisHelper.cs代码:UserInfoDto.cs代码:运行结果如图:的全部内容,更多相关.NET平台下Redis使用(七)【StackExchange.Redis测试Redis五种数据类型】信念之于人,犹翅膀之于鸟,信念是飞翔内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复