redis source code analysis (six) - Redis database, the realization of key expired

Realization of the database

We look at the code server.h / redisServer

struct redisServer{
    ...

    //保存 db 的数组
    redisDb *db;
    
    //db 的数量
    int dbnum;
    ...
}

Look redisDb code:

typedef struct redisDb {
    dict *dict;                 /* The keyspace for this DB */
    dict *expires;              /* Timeout of keys with a timeout set */
    dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP)*/
    dict *ready_keys;           /* Blocked keys that received a PUSH */
    dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */
    int id;                     /* Database ID */
    long long avg_ttl;          /* Average TTL, just for stats */
} redisDb;

Overall, the redis server contains a number (default 16) redisDb database.

Here Insert Picture Description
Redis is a key value kv stored on the database. Dictionary dict which holds all key database right, this place is called keyspace literal translation is "key space."

So we can so that in redisDb we use dict (dictionary) to maintain the key space.

keyspace the key database is the key, each key is a string object. Note that is not a string, but the string object.

The value is the value keyspace database, this value can be redis, the string object a list of objects, the object hash table, or a collection of objects ordered object.

Database read and write operations

So for additions and deletions to change search data, it is to a large map of the additions and deletions keyspace change search.

When we execute:

redis SET mobile "13800000000"
In fact, for the keyspace it adds a key that contains the string "mobile" string object, value as a String object that contains the characters "13800000000" of.

Figure:

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-eYHieGwP-1573628639724) (media / 15663864485138 / 15663865237031.jpg)]

For excision investigation, nothing to say. The map is similar to java operation, most programmers should be able to understand.

Of particular note is that the key again for read and write operations when, Redis need to do some additional maintenance actions:

  • Maintenance hit and miss two counters. Cache hit ratio statistics for the Redis.
  • Update key LRU time, active time record last key.
  • If you find that key has expired, Redis key to delete the expired and then perform the remaining operations. When reading
  • If a customer WATCH operations performed on this key, the key will marked as dirty, let Affairs noted that the key has been to turn over.
  • No modification will increase once dirty 1.
  • If the database server is turned on after the notification, the key is modified, it will send a notification in accordance with the configuration.

Expired key implementation

One of the major characteristics of the use Redis as a cache for the key-value pairs that you can set an expiration time.

In Redis with the expiration time associated command

  • EXPIRE setting key survival time in seconds
  • EXPIREAT Setting key expiration time in seconds
  • PEXPIRE survival time unit setting key millisecond
  • PEXPIREAT setting key expiration time in milliseconds

In fact, these commands, the underlying commands are implemented by the REXPIREAT.

Use the dict * expires in redisDb, the store expiration time. Keyspace where key points to the key (in language pointer c), is a value of type long long timestamp, the calibration key expiration time point, in milliseconds.

If we add an expiration time for the mobile above.

redis PEXPIREAT mobile 1521469812000
This time it will add a key-value pair in the dictionary expired. As shown below:

Here Insert Picture Description
For very simple decision logic expired:

  1. key exists in the dictionary expires.
  2. If the key exists, value is less than the current system timestamp timestamp.
Published 257 original articles · won praise 223 · views 320 000 +

Guess you like

Origin blog.csdn.net/csdn_kou/article/details/103049537