The string is the most basic type of Redis, and the string type is binary safe . This means that a Redis string can contain any data . For example, JPG images or serialized objects. From an internal implementation , a string can be treated as a byte array with a maximum limit of 1G bytes . The following is the definition of a string type.
struct SDSHDR {
Long Len;
Long free;
Char buf[];
};
BUF is a char array for storing the actual string contents. In fact, char is equivalent to byte in C # and is a byte. Len is the length of the BUF array, and free is the number of available bytes remaining in the array. This makes it possible to understand why the string type is binary safe. Because it's essentially a byte array. Of course it can contain any data. In addition, the string type can be handled by a partial command by Int. such as incr and other commands. There are other types of redis like list,set,sorted set, hash they contain elements and can only be string types. If you use only string types, Redis can be seen as a memcached that adds persistence. of course, Redis operates much more than memcached for string types.
Set key value sets the value of the key corresponding to string type value, and returns OK to indicate success
setnx Key value Ibid, if key already exists, returns 0. NX is not exist's meaning
get key gets the string value corresponding to key, if key does not exist return nil
Getset The value of key value atom, and returns the old value of key. If key does not exist return nil
mget key1 key2 ... keyN gets the value of more than one key at a time, if the corresponding key does not exist, the corresponding return nil
mset key1 value1 ... keyN valuen set multiple key values at once, successful return OK means all values are set, failure returns 0 means no value is set
msetnx key1 value1 ... keyN Valuen Ibid, but does not overwrite existing key
incr Key does a Gaga operation on the value of key and returns the new value. Note INCR A value that is not an int returns an error incr a nonexistent key, setting key to 1
decr key Ibid, but do is reduce the operation, DECR one does not exist key, then set key to 1
Incrby key integer with INCR, plus the specified value, key does not exist when the key is set, and that the original value is 0
Decrby key integer with DECR, minus the specified value. Decrby is purely for readability, we can achieve the same effect by Incrby a negative value, and vice versa.
Append key value appends value to the string value of the specified key, returning the length of the new string value
substr key Start End returns the string value of the truncated key, noting that the value of key is not modified and the subscript is 0-based
Redis Learning Note (iii)-String type of data type