SoFunction
Updated on 2025-03-01

The expiration time of Redis key and the permanent and effective implementation

Set the expiration time of the key

1. Use the `EXPIRE` command:

The EXPIRE command is used to set the expiration time (in seconds) of a key.

grammar:EXPIRE key seconds
Example:EXPIRE mykey 60

The above command sets the expiration time of `mykey` to 60 seconds.

Use the `SET` command and `EX` parameter:

When creating a key, you can directly set the expiration time using the EX` parameter of the `SET` command.

grammar:SET key value EX seconds
Example:SET mykey "myvalue" EX 60

The above command sets the value of `mykey` to `"myvalue"` and sets its expiration time to 60 seconds.

Use the `PEXPIRE` command:

The `PEXPIRE` command is used to set the expiration time (in milliseconds) of the key.

grammar:PEXPIRE key milliseconds
Example:PEXPIRE mykey 60000

The above command sets the expiration time of `mykey` to 60,000 milliseconds (i.e. 60 seconds).

Use the `SET` command and `PX` parameter:

Similarly, the expiration time (in milliseconds) can be set directly using the `PX` parameter of the `SET` command.

grammar:SET key value PX milliseconds

Example:

SET mykey "myvalue" PX 60000

The above command sets the value of `mykey` to `"myvalue"` and sets its expiration time to 60,000 milliseconds (i.e. 60 seconds).

Use the `EXPIREAT` command:

The EXPIREAT command is used to set the key to expire at a specific point in time (in seconds of a Unix timestamp).

grammar:EXPIREAT key timestamp
Example:EXPIREAT mykey 1629964800

The above command sets `mykey` expires when Unix timestamp 1629964800 (corresponding UTC time).

Use the `PEXPIREAT` command

The `PEXPIREAT` command is similar to `EXPIREAT`, but the timestamp is in milliseconds.

grammar:PEXPIREAT key milliseconds_timestamp
 Example:PEXPIREAT mykey 1629964800000

The above command sets `mykey` expires when the Unix millisecond timestamp is 1629964800000 (corresponding UTC time).

Set the key to permanently valid

If you want to make the key permanently valid (i.e., there is no expiration time), you can use the following method:

Simply set the key without setting the expiration time

Using the `SET` command without specifying the `EX` or `PX` parameter, the key will be set to permanently valid.

grammar:SET key value

Example:

SET mykey "myvalue"

The above command sets `mykey` to be permanently valid and will not expire.

The expiration time of removing existing keys

If the key has already set an expiration time, you can use the `PERSIST` command to make it permanent.

grammar:PERSIST key

Example:

PERSIST mykey

The above command will remove the expiration time of `mykey` to make it permanently valid.

Summarize

Redis is a memory-based high-performance key-value storage system. In Redis, the key can be set to expire time or saved permanently. This article will introduce the expiration time and permanent valid settings of keys in Redis.

  • Expiry time setting: In Redis, you can use the EXPIRE command to set the expiration time of the key. The syntax of the EXPIRE command is: EXPIRE key seconds, where key is the key name to set the expiration time, and seconds is the number of seconds to expire. For example, to set the key named "username" to expire after 10 seconds, you can use the following command: EXPIRE username 10

    In addition, you can also use the PEXPIRE command to set the expiration time of the key. The syntax is: PEXPIRE key millionseconds, where key is the key name to set the expiration time, and milliseconds is the number of milliseconds of the expiration time. For example, to set the key named "username" to expire after 100 milliseconds, you can use the following command: PEXPIRE username 100

    After setting the expiration time, Redis will automatically delete the key after the specified time, so it can be used in scenarios such as implementing cache policies or timing task scheduling.

  • Permanently valid settings: In Redis, keys can be permanently valid by not setting the expiration time. You can use the SET command to set a key that never expires, and its syntax is: SET key value. For example, to set a key named "username" to be valid permanently, you can use the following command: SET username "john"

    At this point, the key will never expire unless the value of the key is manually deleted or rewrited. This setting is suitable for some long-term and effective data, such as configuration information, user information, etc.

  • A summary of the expiration time and permanent validity of keys:

    • The expiration time of the key can be set through the EXPIRE command, and the expiration time of the key (in milliseconds) can be set through the PEXPIRE command.
    • After setting the expiration time, Redis will automatically delete the key after the specified time.
    • The key can be set to permanently valid through the SET command, that is, the expiration time is not set.
    • A permanently valid key will remain in Redis unless it is manually deleted or rewrites its value.
    • Expiration time and permanently valid settings can be selected according to actual needs.

Redis provides flexible key expiration time and permanent and effective setting methods, and you can choose the appropriate method according to specific business needs. At the same time, setting the expiration time reasonably can effectively manage memory and avoid invalid storage of data.

This is the end of this article about the expiration time and permanent valid implementation of Redis key. For more relevant expiration time and permanent valid content of Redis key, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!