This post, the sixth in the Redis series, covers the following:

  1. Number of databases
  2. Switching target database
  3. Set the expiration time of the key
  4. Remove the expiration time of the key

The first five articles in this series can be viewed by clicking on the following links:

Redis series (1) : Introduction to Redis and environment installation

Redis series (2) : Redis 5 data structures and common commands

Redis series 3: Redis persistence mechanism (RDB, AOF)

Redis series 4: Redis replication mechanism (master/slave replication)

Redis series (5) : Redis expiration key deletion strategy

1. Number of databases

By default, Redis server has 16 databases, respectively db0 to DB15, as shown in the following figure:

This number is determined by the Databases option in the configuration file. The default value is 16:

2. Switch to the target database

By default, the target database for the commands we execute is DB0. For example, we execute the following command:

set message "Hello,Redis"
Copy the code

The key-value pair will be stored in database DB0, as shown below:

If you want to switch the target database, use the SELECT command, for example, to switch to DB1, you can run the following command:

SELECT 1
Copy the code

Then execute the command as shown below:

The key-value pair will be stored in DB1, as shown below:

In actual use, it is recommended to follow the following specifications:

  1. Each application uses a different target database
  2. One application should not operate on multiple target databases

3. Set or remove the expiration time of the key

Redis provides seven key expiration commands, as shown below:

  1. TTL
  2. PTTL
  3. EXPIRE
  4. PEXPIRE
  5. EXPIREAT
  6. PEXPIREAT
  7. PERSIST

The following sections explain how to use each command.

3.1 TTL

TTL is the abbreviation of Time To Live. It is used To view the remaining lifetime of a key, in seconds.

If the key is not set to expire, -1 is returned.

If the key does not exist, -2 is returned.

3.2 PTTL

Similar to TTL, PTTL is used to view the remaining lifetime of a key in milliseconds.

If the key is not set to expire, -1 is returned.

If the key does not exist, -2 is returned.

3.3 the EXPIRE

Using the EXPIRE command, you can set the EXPIRE time (in seconds) for the specified key.

If the expiration time is set successfully, 1 is returned.

If the key does not exist, return 0.

3.4 PEXPIRE

PEXPIRE, like EXPIRE, is used to set an expiration time for a specified key, but in milliseconds.

If the expiration time is set successfully, 1 is returned.

If the key does not exist, return 0.

3.5 EXPIREAT

Using the EXPIREAT command, you can set the expiration time of a specified key, but specify the UNIX timestamp of a certain point in time in seconds.

If the expiration time is set successfully, 1 is returned.

If the key does not exist, return 0.

You can get the timestamp in seconds using Java code like this:

System.out.println(LocalDateTime.now().plusMinutes(2).toEpochSecond(ZoneOffset.ofHours(8)));
Copy the code

3.6 PEXPIREAT

PEXPIREAT is similar to EXPIREAT and is used to set the expiration time of a specified key, but to specify a UNIX timestamp in milliseconds.

If the expiration time is set successfully, 1 is returned.

If the key does not exist, return 0.

You can get a timestamp for milliseconds using Java code like this:

System.out.println(System.currentTimeMillis());

System.out.println(LocalDateTime.now().plusMinutes(2).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
Copy the code

3.7 PERSIST

The PERSIST command removes the expiration time of a specified key.

If the expiration time is removed successfully, return 1.

If the key does not exist or has no expiration time set for the key, 0 is returned.

4. Source code and reference

Redis Design and Implementation by Huang Jianhong

redis.io/commands