"Redis Design and implementation"-database

Source: Internet
Author: User
Tags redis redis server

1. Database structure in the server

The Redis server stores all databases in the DB array of the server state redisserver structure, represented by the REDISDB structure as a database

    struct redisServer {        // ...        // 一个数组,保存着服务器中的所有数据库        redisDb *db;    }

Redis Server creates 16 databases by default, and the target database for Redis clients is database number No. 0 by default.

2. Switching the database

The SELECT command is used to switch the database

    redis> SELECT 2    OK    redis[2]>            //切换到了2号数据库

Attention:
Before performing a Redis command, especially a dangerous command such as FLUSHDB, it is a good idea to execute a SELECT command, display the switch to the specified database, and then perform the other naming.

3. Database Key Space

Redis is a key-value pair of database servers, and each database in the server is represented by a REDISDB structure. Where the Dict Dictionary of the REDISDB structure holds all the key-value pairs in the database, we refer to this dictionary as the key space.

    typedef struct redisDb {        // ...        // 数据库键空间,保存着数据库中所有的键值对        dict *dict;    }
    • Key Space key is the key of the database, each key is a string object
    • The value of the key space is the value of the database, and each value can be a string object, a list object, a Hashtable object, a collection object, and any Redis object in an ordered collection object.
4. Key Expires 4.1 set key expiration command

Redis has four different commands for setting the lifetime or expiration time of a key

    • EXPIRE <key>
    • Pexpire
    • Expireat
    • Pexpireat

EXPIRE, Pexpire, expireat Three commands are implemented by invoking the Pexpireat command.

4.2 Remove Key Expiration command
    • The persist command is used to remove the expiration time for key keys
      Usage: PERSIST
    redis>EXPIRE msg 1000   // 设置键msg1000秒后过期    1    redis>TTL msg    996    redis>PERSIST msg     1    redis>TTL msg    -1      // 返回值为-1,说明msg没有设定过期时间
4.3 Calculate and return remaining time to live
    • The TTL command returns the remaining life time of the key in seconds
    • The Pttl command returns the remaining lifetime of the key in milliseconds

"Redis Design and implementation"-database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.