阿里云redis 密钥登录_如何使Redis中的密钥失效

阿里云redis 密钥登录

介绍 (Introduction)

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密钥是永久性的,这意味着Redis服务器将继续存储它们,除非手动将其删除。 但是,在某些情况下,您已经设置了密钥,但是您知道要在经过一定时间后才将其删除。 换句话说,您希望密钥是可变的 。 本教程介绍了如何设置密钥的到期时间,检查直到密钥到期的剩余时间以及取消密钥的到期设置。

如何使用本指南 (How To Use This Guide)

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 How To Install and Secure Redis on Ubuntu 18.04. 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 — Redli, for example — the exact output of certain commands may differ.

本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南如何在Ubuntu 18.04上安装和保护Redis的 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如Redli) ,则某些命令的确切输出可能会有所不同。

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 Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.

另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的托管数据库产品文档 。 然后, 您必须 安装Redli 设置TLS隧道才能通过TLS连接到托管数据库。

设置密钥过期 (Setting Keys to Expire)

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秒后过期:

  • set key_melon "cantaloupe"

    设置key_melon“哈密瓜”
  • expire key_melon 450

    过期key_melon 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 Unix timestamp 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 EpochConverter or UnixTimestamp.com.

或者,您可以使用expireat命令将密钥设置为在特定的时间点到期。 它使用Unix时间戳作为参数,而不是到期前的秒数。 Unix时间戳是自Unix纪元 (即1970年1月1日UTC时间00:00:00)以来的秒数。可以使用许多在线工具来查找特定日期和时间的Unix时间戳,例如EpochConverterUnixTimestamp.com

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 ),可以使用以下命令:

  • expireat key_melon 1746131400

    expireat key_melon 1746131400

Note that if the timestamp you pass to expireat has already occurred, it will delete the key immediately.

请注意,如果传递给expireat的时间戳已经发生,它将立即删除密钥。

检查密钥过期的时间 (Checking How Long Until a Key Expires)

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香港专业教育学院”:

  • ttl key_melon

    ttl key_melon

   
   
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 ,它将返回直到密钥过期的时间(以毫秒为单位):

  • pttl key_melon

    pttl key_melon

   
   
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.

如果尚未将密钥设置为过期,则ttlpttl都将返回(integer) -1如果密钥不存在,则ttlpttl都将返回(integer) -1 (integer) -2

持久键 (Persisting Keys)

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:

如果密钥已设置为过期,则任何覆盖密钥内容的命令(例如setgetset )都将清除密钥的超时值。 要手动清除键的超时,请使用persist命令:

  • persist key_melon

    坚持key_melon

The persist command will return (integer) 1 if it completed successfully, indicating that the key will no longer expire.

如果成功完成, persist命令将返回(integer) 1 ,表示密钥将不再过期。

结论 (Conclusion)

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 How to Manage a Redis Database.

有关Redis命令的更多信息,请参阅关于如何管理Redis数据库的系列教程。

翻译自: https://www.digitalocean.com/community/cheatsheets/how-to-expire-keys-in-redis

阿里云redis 密钥登录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值