1 /// <summary>
2 /// redis 无序集合
3 /// </summary>
4 public static void Redis_Set()
5 {
6 RedisClient client = new RedisClient("127.0.0.1", 6379);
7 //清空数据库缓存,慎用
8 client.FlushAll();
9 #region Set无序集合
10 /*
11 它是string类型的无序集合。set是通过hash table实现的,添加,删除和查找,对集合我们可以取并集,交集,差集
12 */
13 client.AddItemToSet("Set1", "lily");
14 client.AddItemToSet("Set1", "lucy");
15 client.AddItemToSet("Set1", "hanmeimei");
16 client.AddItemToSet("Set1", "lilei");
17 HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1");
18 foreach (string item in hastsetA)
19 {
20 Console.WriteLine("Set无序集合ValueA:{0}", item); //出来的结果是无须的
21 }
22
23 client.AddItemToSet("Set2", "lilei");
24 client.AddItemToSet("Set2", "liming");
25 client.AddItemToSet("Set2", "lucy");
26 client.AddItemToSet("Set2", "mr wang");
27 HashSet<string> hastsetB = client.GetAllItemsFromSet("Set2");
28 foreach (string item in hastsetB)
29 {
30 Console.WriteLine("Set无序集合ValueB:{0}", item); //出来的结果是无序的
31 }
32 //求并集
33 HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "Set1", "Set2" });
34 //便利结果数据
35 foreach (string item in hashUnion)
36 {
37 Console.WriteLine("求Set1和Set2的并集:{0}", item); //并集
38 }
39 //求交集
40 HashSet<string> hashG = client.GetIntersectFromSets(new string[] { "Set1", "Set2" });
41 //便利结果数据
42 foreach (string item in hashG)
43 {
44 Console.WriteLine("求Set1和Set2的交集:{0}", item); //交集
45 }
46 //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
47 HashSet<string> hashD = client.GetDifferencesFromSet("Set1", new string[] { "Set2" });
48 foreach (string item in hashD)
49 {
50 Console.WriteLine("求Set1和Set2的差集:{0}", item); //差集
51 }
52
53 #endregion
54 }