redis EXPIRE command


  Translation results:

UK[ɪkˈspaɪə(r)] US[ɪkˈspaɪr]

##vi.Expiration; documents, agreements, etc. (due to expiration) become invalid; expire; die

Third person singular: expires Present participle: expiring Past tense: expired Past participle: expired

redis EXPIRE commandsyntax

Function:Set the survival time for a given key. When the key expires (the survival time is 0), it will be automatically deleted.

Syntax: EXPIRE key seconds

Explanation: In Redis, keys with survival time are called "volatile" ( volatile). The lifetime can be removed by deleting the entire key using the DEL command, or overwritten by the SET and GETSET commands. This means that if a command only alters the value of a key with a lifetime instead of using If you replace it with a new key value, the lifetime will not be changed. For example, executing the INCR command on a key, executing the LPUSH command on a list, or executing the HSET command on a hash table, these operations will not modify the survival time of the key itself. On the other hand, if you use RENAME to rename a key, the survival time of the renamed key will be the same as before the rename. Another possibility of the RENAME command is to try to rename a key with a lifetime to another_key with a lifetime. At this time, the old another_key (and its lifetime) will be deleted, and then the old key will be renamed another_key , therefore, the survival time of the new another_key is the same as the original key. Use the PERSIST command to remove the key's lifetime without deleting the key, making the key a "persistent" key again.

Available versions: >= 1.0.0

Time complexity: O(1)

Return: Return 1 if setting is successful. When the key does not exist or the lifetime cannot be set for the key (for example, in a version of Redis earlier than 2.1.3 when you try to update the lifetime of the key), 0 is returned.

redis EXPIRE commandexample

redis> SET cache_page "www.google.com"
OK
redis> EXPIRE cache_page 30  # 设置过期时间为 30 秒
(integer) 1
redis> TTL cache_page    # 查看剩余生存时间
(integer) 23
redis> EXPIRE cache_page 30000   # 更新过期时间
(integer) 1
redis> TTL cache_page
(integer) 29996

Popular Recommendations

Home

Videos

Q&A