[Wheel mad magic] Hands-on teach you how to create Redis Client, crazy redis
Redis Client, as its name suggests, is a redis Client that encapsulates some Redis operations.
However, the widely used ServiceStack. Redis does not learn well, and charges have actually started.
As a wheel madman, it is intolerable. So I decided to build my own wheels.
Redis communication protocol |
First give a Redis official communication protocol address: http://redisdoc.com/topic/protocol.html
The key is my part. We can get the following information:
1. tcp protocol
2. Default port 6379
3. The command ends with \ r \ n
Achieve Redis interaction (Get, Set) |
The Set command Description: http://redisdoc.com/string/set.html
Get command Description: http://redisdoc.com/string/get.html
TcpClient for Tcp interaction in C #
|
Implement the Set command |
The Code intent is roughly described as follows:
1. Create TcpClient
2. Connect to Redis (127.0.0.1: 6379)
3. Send the command set test csharp \ r \ n (note that \ r \ n is mentioned at the beginning of the communication protocol, and the command ends with \ r \ n)
4. Use UTF8 for encoding and decoding
5. Receive returned information
PS: The get command is similar, so I will not post it.
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. The first line of thinking is the singleton mode, that is, I want to have only one Redis Client. (RedisClient can be used directly if there are more than one)
2. The connection address of RedisClient can be freely specified
3. Remove the Client from the single instance and you can simply and roughly access the 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 and RedisClient Reconstruction |
RedisManager is actually a simple Singleton mode, which encapsulates a globally unique object. If you do not want to be globally unique, you can simply use RedisClient.
1 public class RedisSingleton 2 {3 # region Singleton Example 4 5 private static RedisSingleton _ redisSingleton = null; 6 private static object _ locker = new object (); 7 8 public static RedisSingleton GetInstance 9 {10 get11 {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 # endregion28 29 public RedisClient Client {get; set;} 30}View Code
RedisClient is the real code for interacting with Redis (because this is just a simple and crude Demo, the Code is not very beautiful, sorry! Will continue to improve my self-built 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 (setCo Mmand); 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
Finally, we like the advertising process. |
I will not share the Project address on my oschina because I am currently working on an open-source mvc project that requires Redis, so I made such a wheel myself.
If you are interested in joining us, add group: 7424099
Oh, sorry. I have a picture with the truth.