本文共 5494 字,大约阅读时间需要 18 分钟。
阿里云redis 密钥登录
is an open-source, in-memory key-value data store. Redis keys are persistent by default, meaning that the Redis server will continue to store them unless they are deleted manually. There may, however, be cases where you’ve set a key but you know you will want to delete it after a certain amount of time has passed; in other words, you want the key to be volatile. This tutorial explains how to set keys to expire, check the time remaining until a key’s expiration, and cancel a key’s expiration setting.
是一个开源的内存中键值数据存储。 默认情况下,Redis密钥是永久性的,这意味着Redis服务器将继续存储它们,除非手动将其删除。 但是,在某些情况下,您已经设置了密钥,但是您知道要在经过一定时间后才将其删除。 换句话说,您希望密钥是可变的 。 本教程介绍了如何设置密钥的到期时间,检查直到密钥到期的剩余时间以及取消密钥的到期设置。
This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.
本指南以备有完整示例的备忘单形式编写。 我们鼓励您跳至与您要完成的任务相关的任何部分。
The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on . We will demonstrate how these commands behave by running them with redis-cli
, the Redis command line interface. Note that if you’re using a different Redis interface — , for example — the exact output of certain commands may differ.
本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli
运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如 ,则某些命令的确切输出可能会有所不同。
Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our . Then, you must either or in order to connect to the Managed Database over TLS.
另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的 。 然后, 您必须 或 才能通过TLS连接到托管数据库。
You can set an expiration time for an existing key with the expire
command, which takes the name of the key and the number of seconds until expiration as arguments. To demonstrate this, run the following two commands. The first creates a string key named key_melon
with a value of "cantaloupe"
, and the second sets it to expire after 450 seconds:
您可以使用expire
命令设置现有密钥的到期时间,该命令将密钥名称和直到到期的秒数作为参数。 为了证明这一点,请运行以下两个命令。 第一个创建一个名为key_melon
的字符串键,其值为"cantaloupe"
key_melon
"cantaloupe"
,第二个将其设置为在450秒后过期:
If the timeout was set successfully, the expire
command will return (integer) 1
. If setting the timeout failed, it will instead return (integer) 0
.
如果成功设置了超时, expire
命令将返回(integer) 1
。 如果设置超时失败,它将返回(integer) 0
。
Alternatively, you can set the key to expire at a specific point in time with the expireat
command. Instead of the number of seconds before expiration, it takes a as an argument. A Unix timestamp is the number of seconds since the Unix epoch, or 00:00:00 UTC on January 1, 1970. There are a number of tools online you can use to find the Unix timestamp of a specific date and time, such as or .
或者,您可以使用expireat
命令将密钥设置为在特定的时间点到期。 它使用作为参数,而不是到期前的秒数。 Unix时间戳是自Unix纪元 (即1970年1月1日UTC时间00:00:00)以来的秒数。可以使用许多在线工具来查找特定日期和时间的Unix时间戳,例如或 。
For example, to set key_melon
to expire at 8:30pm GMT on May 1, 2025 (represented by the Unix timestamp 1746131400
), you could use the following command:
例如,要将key_melon
设置为在格林尼治标准时间2025年5月1日晚上8:30到期(由Unix时间戳记1746131400
),可以使用以下命令:
Note that if the timestamp you pass to expireat
has already occurred, it will delete the key immediately.
请注意,如果传递给expireat
的时间戳已经发生,它将立即删除密钥。
Any time you set a key to expire, you can check the time remaining until expiry (in seconds) with ttl
, which stands for “time to live”:
你设置一键到期任何时候,你可以检查剩余到期为止(以秒为单位)与时间ttl
,它表示“T IME 牛逼 0 1香港专业教育学院”:
Output (integer) 433
For more granular information, you can run pttl
which will instead return the amount of time until a key expires in milliseconds:
有关更详细的信息,可以运行pttl
,它将返回直到密钥过期的时间(以毫秒为单位):
Output (integer) 431506
Both ttl
and pttl
will return (integer) -1
if the key hasn’t been set to expire and (integer) -2
if the key does not exist.
如果尚未将密钥设置为过期,则ttl
和pttl
都将返回(integer) -1
如果密钥不存在,则ttl
和pttl
都将返回(integer) -1
(integer) -2
。
If a key has been set to expire, any command that overwrites the contents of a key — like set
or getset
— will clear a key’s timeout value. To manually clear a key’s timeout, use the persist
command:
如果密钥已设置为过期,则任何覆盖密钥内容的命令(例如set
或getset
)都将清除密钥的超时值。 要手动清除键的超时,请使用persist
命令:
The persist
command will return (integer) 1
if it completed successfully, indicating that the key will no longer expire.
如果成功完成, persist
命令将返回(integer) 1
,表示密钥将不再过期。
This guide details a number of commands used to manipulate and check key persistence in Redis. If there are other related commands, arguments, or procedures you’d like to see outlined in this guide, please ask or make suggestions in the comments below.
本指南详细介绍了用于在Redis中操作和检查键持久性的许多命令。 如果您想在本指南中概述其他相关的命令,参数或过程,请在下面的注释中提出疑问或提出建议。
For more information on Redis commands, see our tutorial series on .
有关Redis命令的更多信息,请参阅关于系列教程。
翻译自:
阿里云redis 密钥登录
转载地址:http://xihgb.baihongyu.com/