The Redis client, as its name implies, Redis's clients, mainly encapsulates some operations for Redis.
At present, the relatively extensive servicestack.redis do not learn, incredibly began to charge.
As the wheel of the devil, is can not endure ah. So I decided to build my own wheels.
Redis Communication Protocol |
Let's get a Redis official protocol address: Http://redisdoc.com/topic/protocol.html
The key is my part and we can get the following information:
1.TCP protocol
2. Default Port 6379
3. The command ends with \ r \ n
Implementing Redis Interaction (Get, Set) |
Set command description: http://redisdoc.com/string/set.html
Get Command Description: http://redisdoc.com/string/get.html
TCP Interactive selection of C # TcpClient
|
Implementing set Directives |
The code intends to say something about:
1. Create TcpClient
2. Connect to Redis (127.0.0.1:6379)
3. Send instruction Set test csharp\r\n (note \ r \ n is the first communication protocol mentioned, the command to the end of \ r \ n)
4. Using UTF8 to encode and decode
5. Receive return information
Ps:get directive is similar, I will not post it.
1 varTcpClient =NewSystem.Net.Sockets.TcpClient ();2 3Tcpclient.connect ("127.0.0.1",6379);4 5 stringSetCommand ="Set Test csharp\r\n";6 tcpClient.Client.Send (Encoding.UTF8.GetBytes (SetCommand));7 System.Diagnostics.Trace.Write (SetCommand);8 byte[] buffer =New byte[ the];9 tcpClient.Client.Receive (buffer);TenSystem.Diagnostics.Trace.Write (Encoding.UTF8.GetString (buffer). Replace (" /",""));
View Code
1. One of the key ideas in the first line is the singleton pattern, which is that I want to have only one redis Client globally. (multiple words can be directly used redisclient)
2.RedisClient connection address is freely specified.
3. Remove the client from the singleton to easily and rudely go to TA. Client. Set (Key,value)
1 varClient = RedisSingleton.GetInstance.Client =NewClient.redisclient ("127.0.0.1",6379);2Client. Set ("Test","Framework.redis");3 varValue = client. Get ("Test");4Trace.Write ("Client Get:"+ value);
|
Reconstruct a redismanager and redisclient |
Redismanager is actually a simple singleton pattern that encapsulates a globally unique object. If you don't want to be globally unique, use redisclient directly.
1 Public classRedissingleton2 {3 #regionSingle case4 5 Private StaticRedissingleton _redissingleton =NULL;6 Private Static Object_locker =New Object();7 8 Public StaticRedissingleton getinstance9 {Ten Get One { A if(_redissingleton = =NULL) - { - Lock(_locker) the { - if(_redissingleton = =NULL) - { -_redissingleton =NewRedissingleton (); + } - } + } A at return_redissingleton; - } - } - - #endregion - in PublicRedisclient Client {Get;Set; } -}
View Code
Redisclient is really the code that interacts with Redis (because it's just a rough demo, so the code is not very beautiful, forgive me!). will continue to refine my self-made redis)
1 Public classredisclient2 {3 PrivateTcpClient _tcpclient =NULL;4 5 PublicRedisclient (stringServerIP,intServerPort)6 {7_tcpclient =NewTcpClient ();8 _tcpclient.connect (ServerIP, serverport);9 }Ten One Public voidSet (stringKeystringvalue) A { - stringSetCommand ="Set"+ key +" "+ Value +"\ r \ n"; - _tcpclient.client.send (Encoding.UTF8.GetBytes (SetCommand)); the Logger.info (SetCommand); - varresult =Getraw (); - if(Result! ="+ok") - { + Throw NewException ("Redis Error:"+result); - } + } A at Public stringGet (stringkey) - { - stringSetCommand ="Get"+ key +"\ r \ n"; - _tcpclient.client.send (Encoding.UTF8.GetBytes (SetCommand)); - Logger.info (SetCommand); - stringValue =Getraw (); inLogger.info ("Get"+ key +"Value:"+value); - returnValue. Split (New string[] {"\ r \ n"}, Stringsplitoptions.none) [1]; to } + - Private stringGetraw () the { * byte[] buffer =New byte[ the]; $ _tcpclient.client.receive (buffer);Panax Notoginseng stringValue = Encoding.UTF8.GetString (buffer). Replace (" /",""). TrimEnd ("\ r \ n". ToCharArray ()); - returnvalue; the } +}
View Code
Finally, we like the advertising link |
That's all the code. I don't share the project address on my Oschina, because I'm working on an open-source MVC project that requires Redis, so I built this wheel.
If you are interested in joining us, please Dabigatran: 7424099
Oh, I'm sorry, there's a picture and the truth.
"Wheel Maniac" teaches you to build your own Redis Client