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..