Write Redis client by yourself (C # implementation) 4,

Source: Internet
Author: User

Write Redis client by yourself (C # implementation) 4,
Integer reply

 

An integer response is an integer that starts with ":" and ends with a string ending with CRLF.

For example, ": 0 \ r \ n" and ": 1000 \ r \ n" are all integer replies.

Two of the return integer response commands are INCR and LASTSAVE. The returned integer has no special meaning. An auto-increment integer of the incr return key, and LASTSAVE returns a UNIX timestamp, the only limit of the returned values is that these numbers must be represented by 64-bit signed integers.

Integer reply is also widely used to indicate logical truth and logical false. For example, EXISTS and SISMEMBER use the return value 1 to indicate true, and 0 to indicate false. Other commands, such as SADD, SREM, and SETNX, return 1 only when the operation is actually executed; otherwise, return 0. The following command returns an integer reply: SETNX, DEL, EXISTS, INCR, INCRBY, DECR, DECRBY, DBSIZE, LASTSAVE, RENAMENX, MOVE, LLEN, SADD, SREM, SISMEMBER, and SCARD.

 

Code Implementation of integer reply:

/// <Summary> /// integer reply /// </summary> /// <param name = "client"> </param> /// <returns> </returns> public static Int64 ReplyLong (Socket client) {BufferedStream s = new BufferedStream (new NetworkStream (client); int B = s. readByte (); // read the first byte string result; switch (B) {// the first byte of integer reply (integer reply) is ":" case ':': return long. parse (ReadLine (s); // the first byte of error reply is "-" case '-': result = ReadLine (s ); throw new Exception (result); // throw an Exception default: break;} return 0 ;}

 

ThereforeINCROperation Code implementation:

/// <Summary> /// + 1 operation /// </summary> /// <param name = "client"> </param> /// <param name = "key"> </param> // <returns> </returns> public static Int64 INCR (this Socket client, string key) {SendCmd (client, "INCR", Encoding. UTF8.GetBytes (key); return ReplyLong (client );}

ThereforeLASTSAVEOperation Code implementation:

/// <summary>/// LastSave/// </summary>/// <param name="client"></param>/// <param name="key"></param>/// <returns></returns>public static DateTime LASTSAVE(this Socket client) {    SendCmd(client, "LASTSAVE");    long l =  ReplyLong(client);    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));    return startTime.AddSeconds(l);}

ThereforeEXISTSOperation Code implementation:

public static bool EXISTS(this Socket client, string key) {    SendCmd(client, "EXISTS", Encoding.UTF8.GetBytes(key));    return ReplyLong(client) == 0;}

ThereforeSISMEMBEROperation Code implementation:

/// <Summary> /// determine whether the member element is a member of the set key. /// </summary> /// <returns> </returns> public static bool SISMEMBER (this socket client, string key, string member) {SendCmd (client, "SISMEMBER", Encoding. UTF8.GetBytes (key), Encoding. UTF8.GetBytes (member); return ReplyLong (client) = 0 ;}

For other SETNX, DEL, EXISTS, INCR, INCRBY, DECR, DECRBY, DBSIZE, LASTSAVE, RENAMENX, MOVE, LLEN, SADD, SREM, SISMEMBER, and SCARD integer operations, refer to the above Code..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.