博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
阿里云redis 密钥登录_如何使Redis中的密钥失效
阅读量:2517 次
发布时间:2019-05-11

本文共 5494 字,大约阅读时间需要 18 分钟。

阿里云redis 密钥登录

介绍 (Introduction)

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

如何使用本指南 (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 . 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连接到托管数据库。

设置密钥过期 (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 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 ),可以使用以下命令:

  • 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 .

有关Redis命令的更多信息,请参阅关于系列教程。

翻译自:

阿里云redis 密钥登录

转载地址:http://xihgb.baihongyu.com/

你可能感兴趣的文章
struts2 配置(部分)
查看>>
python代码迷之错误(ModuleNotFoundError: No module named 'caffe.proto')
查看>>
nodejs adm-zip 解压文件 中文文件名乱码 问题解决
查看>>
MapReduce-文本输入
查看>>
<Bootstrap> 学习笔记六. 栅格系统使用案例
查看>>
可能的出栈序列问题
查看>>
vector--C++ STL 学习
查看>>
蜕变成蝶~Linux设备驱动之异步通知和异步I/O
查看>>
jquery简单开始
查看>>
IOS 在不打开电话服务的时候,可以响应服务器的推送消息,从而接收服务器的推送消息...
查看>>
置顶的博客
查看>>
ionic2 native app 更新用户头像暨文件操作
查看>>
SQL Server R2 地图报表制作(一)
查看>>
ZeroMQ——一个轻量级的消息通信组件
查看>>
JavaScript中数组和json的复制
查看>>
C语言多线程编程二
查看>>
转载:从集群计算到云计算
查看>>
服务器文件管理
查看>>
作业2
查看>>
ios上架报错90080,90087,90209,90125 解决办法
查看>>