Implementation and expired Mechanism (six) Redis databases - Design and Implementation of Redis

Redis server database implementation -

redisServer at startup default startup 16 databases, each redisClient db has its own connection, recorded in property redisClient db's, clients can switch db their own links.

Understandably, in fact, each has a dic dictionary db stored key and value, our set is also the key to set the value inside, delete, update empathy. When access to the key, maintains LRU, dirty update information values ​​as shown below

Other commands, such as FLUSHDB command, in fact, delete the contents of dict.

Expired mechanism

There's a db in dict * expires attribute is used to save the key expiration time can be set by PEXPIREAT command, PERSIST clear.

So how should remove expired keys do?

       Traditional There are three deletion policy, timed to clear (occupied cpu), inert purge (waste of memory), regularly delete (this deadline not determined). The use of inert Redis periodically delete and delete the two strategies.

Inert Delete:

              Before each visit take a look at is not expired, expired, then delete it, not expired do nothing.

Periodically delete:

              Redis.c file has a serverCron function, call server.hz ​​times per second, performing some regular tasks, which databasesCron calls regularly delete expired activeExpireCycle this function () in accordance with a certain strategy every time some random check of some key database operating

In addition, the AOF, RDB key in how to deal with it expired?

       Expired keys are excluded when generating the RDB, RDB file loading, if the main library is loaded exclude expired key, if it is loaded from the library are loaded, but the Lord will be deleted from the time synchronization. ,

              AOF generated when generating a key will expire DEL command, rewriting is not the time to rewrite expired keys;

              Lord, be removed from the primary server sends the time synchronization to all servers expired key command, only to delete from the server only when receiving the command, so if a key has expired, may be able to access the library to get it from the value.

Send notification function to achieve database

       This feature allows users to subscribe to certain types of notifications, or some or all key operations.

 

Published 47 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/nanchengyu/article/details/89337965