|
Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作。
而目前用的比较广泛的 ServiceStack.Redis 不学好,居然开始收费了。
作为轮子狂魔,是可忍孰不可忍啊。于是我决定自己造轮子了。
先给个Redis官方的通信协议地址:http://redisdoc.com/topic/protocol.html
关键是我截图的部分,我们可以得到以下几个信息:
1.tcp协议
2.默认端口6379
3.命令以 \r\n 结尾
Set命令说明:http://redisdoc.com/string/set.html
Get命令说明:http://redisdoc.com/string/get.html
C#的Tcp交互选用TcpClient
代码意图大概说一下:
1.创建TcpClient
2.连接Redis (127.0.0.1:6379)
3.发送指令 set test csharp\r\n (注意\r\n是一开始通信协议就提到的,命令以\r\n结尾)
4.使用UTF8来编码和解码
5.接收返回信息
PS:get指令类似,我就不贴出来了
1 var tcpClient = new System.Net.Sockets.TcpClient();
2
3 tcpClient.Connect("127.0.0.1", 6379);
4
5 string setCommand = "set test csharp\r\n";
6 tcpClient.Client.Send(Encoding.UTF8.GetBytes(setCommand));
7 System.Diagnostics.Trace.Write(setCommand);
8 byte[] buffer = new byte[256];
9 tcpClient.Client.Receive(buffer);
10 System.Diagnostics.Trace.Write(Encoding.UTF8.GetString(buffer).Replace("\0", ""));
View Code
1.第一行有个关键思维是单例模式,即我希望全局只有一个Redis Client。(多个的话可以直接用RedisClient)
2.RedisClient的连接地址可自由指定
3. 从单例中取出Client就可以简单粗暴的上ta。client.Set(key,value)
1 var client = RedisSingleton.GetInstance.Client = new Client.RedisClient("127.0.0.1", 6379);
2 client.Set("test", "Framework.Redis");
3 var value = client.Get("test");
4 Trace.Write("client get:" + value);
| 重构一个RedisManager和RedisClient | RedisManager 其实就是个简单的单例模式,封装了一个全局唯一的对象而已。如果不想全局唯一,直接用RedisClient就可以了。
1 public class RedisSingleton
2 {
3 #region 单例
4
5 private static RedisSingleton _redisSingleton = null;
6 private static object _locker = new object();
7
8 public static RedisSingleton GetInstance
9 {
10 get
11 {
12 if (_redisSingleton == null)
13 {
14 lock (_locker)
15 {
16 if (_redisSingleton == null)
17 {
18 _redisSingleton = new RedisSingleton();
19 }
20 }
21 }
22
23 return _redisSingleton;
24 }
25 }
26
27 #endregion
28
29 public RedisClient Client { get; set; }
30 }
View Code RedisClient 是真正的与Redis交互的代码(因为这只是个简单粗暴的Demo,所以代码不太美观,见谅!之后会继续完善我的这个自造的Redis)
1 public class RedisClient
2 {
3 private TcpClient _tcpClient = null;
4
5 public RedisClient(string serverIP, int serverPort)
6 {
7 _tcpClient = new TcpClient();
8 _tcpClient.Connect(serverIP, serverPort);
9 }
10
11 public void Set(string key, string value)
12 {
13 string setCommand = "set " + key + " " + value + "\r\n";
14 _tcpClient.Client.Send(Encoding.UTF8.GetBytes(setCommand));
15 Logger.Info(setCommand);
16 var result = GetRaw();
17 if (result != "+OK")
18 {
19 throw new Exception("redis error:" + result);
20 }
21 }
22
23 public string Get(string key)
24 {
25 string setCommand = "get " + key + "\r\n";
26 _tcpClient.Client.Send(Encoding.UTF8.GetBytes(setCommand));
27 Logger.Info(setCommand);
28 string value = GetRaw();
29 Logger.Info("get " + key + " value:" + value);
30 return value.Split(new string[] { "\r\n" }, StringSplitOptions.None)[1];
31 }
32
33 private string GetRaw()
34 {
35 byte[] buffer = new byte[256];
36 _tcpClient.Client.Receive(buffer);
37 string value = Encoding.UTF8.GetString(buffer).Replace("\0", "").TrimEnd("\r\n".ToCharArray());
38 return value;
39 }
40 }
View Code
全部代码就这么多我就不分享我的oschina上的项目地址了,因为目前正在做一个开源的mvc项目需要使用Redis,所以自己造了这么个轮子。
如果你有兴趣加入我们,请加群:7424099
哦,不好意思,补上有图有真相
|
|
|