|
第一步:
下载windows版本的Redis:https://github.com/MSOpenTech/Redis。
第二步:
在命令行执行:D:\redis-2.6\redis-server.exe。
第三步:
这里有教程:https://github.com/ServiceStack/ServiceStack.Redis。
C#版本的客户端类库
Write、Read和Remove测试
代码下载:http://yunpan.cn/QtNrcGxnPRVdV。
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Threading;
7
8 using ServiceStack.Redis;
9 using ServiceStack.Text;
10 using ServiceStack.Redis.Generic;
11
12 namespace RedisStudy
13 {
14 public class User
15 {
16 public string Name { get; set; }
17 }
18
19 class Program
20 {
21 static void Main(string[] args)
22 {
23 Write();
24 Read();
25 Remove();
26 }
27
28 private static void Write()
29 {
30 using (var redisClient = new RedisClient())
31 {
32 IRedisTypedClient redis = redisClient.As();
33
34 var users = redis.Lists["urn:users"];
35
36 users.Add(new User { Name = "段光伟" });
37 users.Add(new User { Name = "段光宇" });
38
39 redis.Save();
40 }
41 }
42
43 private static void Read()
44 {
45 using (var redisClient = new RedisClient())
46 {
47 IRedisTypedClient redis = redisClient.As();
48
49 var users = redis.Lists["urn:users"];
50
51 Console.WriteLine(users.Count);
52
53 redis.Save();
54 }
55 }
56
57 private static void Remove()
58 {
59 using (var redisClient = new RedisClient())
60 {
61 IRedisTypedClient redis = redisClient.As();
62
63 var users = redis.Lists["urn:users"];
64
65 redis.RemoveEntry(users);
66 }
67 }
68 }
69 }
发布订阅测试
1 static void Main(string[] args)
2 {
3 var messagesReceived = 0;
4 var maxMessage = 5;
5 var channelName = "幸福框架";
6
7 using (var redisConsumer = new RedisClient())
8 {
9 using (var subscription = redisConsumer.CreateSubscription())
10 {
11 subscription.OnSubscribe = channel =>
12 {
13 Console.WriteLine(String.Format("订阅频道:'{0}'", channel));
14 };
15 subscription.OnUnSubscribe = channel =>
16 {
17 Console.WriteLine(String.Format("取消订阅频道:'{0}'", channel));
18 };
19 subscription.OnMessage = (channel, msg) =>
20 {
21 Console.WriteLine(String.Format("从频道:'{0}'获取了消息:'{1}'", channel, msg));
22
23 if (++messagesReceived == maxMessage)
24 {
25 subscription.UnSubscribeFromAllChannels();
26 }
27 };
28
29 ThreadPool.QueueUserWorkItem(x =>
30 {
31 Thread.Sleep(200);
32 Console.WriteLine("开始发布消息");
33
34 using (var redisPublisher = new RedisClient())
35 {
36 for (var i = 1; i |
|
|