Redis notes 2-stand-alone database

database

Introduce how the server saves the database, how the client switches the database, how the database saves key-value pairs, and how to add, delete, and modify the database

Database in server

The Redis server saves all databases in the db array of the server state redisServer structure. Each item in the db array is a redisDb structure, which represents a database.

struct redisServer{
    
    
  redisDb *db; //一个数组,保存所有数据库
  int dbnum;  //服务器数据库数量
}

When initializing the server, determine how many databases to create according to the dbnum attribute of redisServer, the default is 16

Switch database

The target database of the Redis client is number 0 by default. The SELECT command is executed to switch the target database.
Inside the server, the db attribute of the client state redisClient structure records the current target database of the client. This attribute is a pointer to the redisDb structure:

typedef struct redisClient{
    
    
  redisDb *db; //记录客户端正在使用的数据库
}

Database keyspace

Redis is a KV database server. Each database of the server is represented by a redisDb structure. The dictionary dict of the redisDb structure stores all key-value pairs in the database. This dictionary is called the key space.

typedef struct redisDb{
    
    
  dcit *dict; //保存数据库中所有键值对
}

The key space corresponds directly to the database seen by the user

  • The key of the key space is also the key of the database, and each key is a string object
  • The value of the key space is also the value of the database, which can be a string, a list, a hash table, a collection, or an ordered collection
    . The key space of the database is a dictionary, so the addition, deletion, modification, and query of the database are all performed by operating the dictionary achieve

Key space operation

redis> SET string_key, object_value1 //Add key
redis> DEL string_key //Delete key
redis> SET string_key, object_value2 //Update key
redis> GET string_key //Get value for key

FLUSHDB: Empty the database
RANDOMKEY: Random return key
DBSIZE: Return the number of database keys
LRANGE
EXISTS
RENAME
KEYS

(to be added)

Key survival/expiration time

The expiration time is a UNIX timestamp. When the key expiration time comes, the server automatically deletes the key from the database. The
TTL/PTTL command accepts a key with lifetime or expiration time and returns its remaining lifetime

Set expiration time

Redis provides 4 commands to set the expiration time:

  • EXPIRE<key> <ttl>: Set the survival time of the key to ttl seconds.
  • PEXPIRE<key> <ttl>: Set the key lifetime to ttl milliseconds.
  • EXPIREAT<key> <timestamp>: Set the expiration time of the key to timestamp seconds.
  • PEXPIREAT<key> <timestamp>: Set the expiration time of the key to timestamp milliseconds timestamp.

Save expiration time

There is an expires dictionary data structure in redisDb to store the expiration time of all keys, which is also called an expired dictionary.

  • The key of the expired dictionary is a pointer to a key object in the key space
  • The value of the expiration dictionary is an integer of type long long, which stores the expiration time of the database key pointed to by the key (Unix timestamp with millisecond precision)
typedef struct redisDb{
    
    
  dict *expires; //过期字典,保存键的过期时间
}

RDB endurance

AOF persistence

event

Client

server

(To be added)

Guess you like

Origin blog.csdn.net/MinutkiBegut/article/details/115238830