Redis (1) Data structure String string

Redis basic data structure
Redis has five basic data structures, namely: String (string), list (list), set (collection), hash (hash) and zset (ordered collection).

A String

All Redis data structures use a unique key string as the name , and then use this unique key value to obtain the response value data structure. The difference between different types of data structures lies in the value structure.

The string structure is very widely used, and a common use is to cache user information. We serialize the user information structure into a string using JSON, and then stuff the serialized string into redis for caching. Similarly, fetching user information will go through a deserialization process.
Redis strings are dynamic strings, which can be modified. The internal implementation is similar to Java's ArrayList. It is actually an abstract type called simple dynamic string (SDS), which contains strings in the redis database. The key-value pairs of values ​​are all implemented by SDS.

data structure

struct sdshdr{
    
    
    //记录buf数组中已使用字节的数量
    int len;
    //记录buf数组中未使用的数量
    int free;
    //字节数组,用于保存字符串
    char buf[];
}

SDS expansion mechanism:
When the string is less than 1M, the expansion is to double the existing space, and the actual capacity is (current capacity*2+1) an extra byte is used to store the empty string.
If it exceeds 1M, expand the capacity At a time, it will only expand 1M more space at a time, and the actual capacity is (current capacity+1M+1byte). It should be noted that the maximum length of the string is 512M.

Why use SDS instead of strings?

  1. C string does not record its own length information, so in order to obtain the length of a C string, the program must traverse the entire string.
  2. Binary security, the characters in the C string must conform to a certain encoding (such as ASCII), and apart from the end of the string, the string cannot contain null characters, otherwise the null character first read by the program will be mistaken for It is the end of the string. These restrictions make the C string can only store text data, but cannot store binary data such as pictures, audio, video, and compressed files.
  3. Compatible with some C string functions. Although SDS APIs are binary safe, they also follow the convention of ending C strings with a null character: these APIs always set the end of the data saved by the SDS to a null character, and always When allocating space for the buf array, one more byte is allocated to accommodate this null character. This is to allow those SDSs that save text data to reuse a part of the functions defined by the <string.h> library

Two redis set() method parameters

Redis command link

redis 127.0.0.1:6379> SET KEY VALUE [EX seconds] [PX milliseconds] [NX|XX]

EX seconds − Set the specified expiration time (in seconds).
PX milliseconds-Set the specified expiration time (in milliseconds).
NX-Set the key only if the key does not exist.
XX-Only set if the key already exists.

Example

redis 127.0.0.1:6379> SET mykey "redis" EX 60 NX

Guess you like

Origin blog.csdn.net/GreedySnaker/article/details/115076084