博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Redis中管理列表
阅读量:2507 次
发布时间:2019-05-11

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

介绍 (Introduction)

is an open-source, in-memory key-value data store. In Redis, a is a collection of strings sorted by insertion order, similar to . This tutorial covers how to create and work with elements in Redis lists.

是一个开源的内存中键值数据存储。 在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连接到托管数据库。

建立清单 (Creating Lists)

A key can only hold one list, although any list can hold over four billion elements. Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush command or the tail (the “right” end) with rpush. You can also use lpush or rpush to create a new list:

密钥只能包含一个列表,尽管任何列表都可以包含40亿个元素。 Redis的读取列表由左到右,你可以添加新的列表元素的列表(“左”端)与头lpush命令或尾巴(“正确”的年底)与rpush 。 您还可以使用lpushrpush创建新列表:

  • lpush key value

    按键 值

Both commands will output an integer showing how many elements are in the list. To illustrate, run the following commands to create a list containing the dictum “I think therefore I am”:

这两个命令都将输出一个整数,以显示列表中有多少个元素。 为了说明这一点,请运行以下命令以创建包含“我认为是我”的​​格言的列表:

  • lpush key_philosophy1 "therefore"

    lpush key_philosophy1“因此”
  • lpush key_philosophy1 "think"

    lpush key_philosophy1“认为”
  • rpush key_philosophy1 "I"

    rpush key_philosophy1“ I”
  • lpush key_philosophy1 "I"

    lpush key_philosophy1“ I”
  • rpush key_philosophy1 "am"

    rpush key_philosophy1“ am”

The output from the last command will read:

最后一条命令的输出将显示为:

Output   
(integer) 5

Note that you can add multiple list elements with a single lpush or rpush statement:

请注意,您可以使用单个lpushrpush语句添加多个列表元素:

  • rpush key_philosophy1 "-" "Rene" "Decartes"

    rpush key_philosophy1“-”“ Rene”“十进制”

The lpushx and rpushx commands are also used to add elements to lists, but will only work if the given list already exists. If either command fails, it will return (integer) 0:

lpushxrpushx命令还用于将元素添加到列表,但是仅在给定列表已经存在的情况下才起作用。 如果任何一个命令失败,它将返回(integer) 0

  • rpushx key_philosophy2 "Happiness" "is" "the" "highest" "good" "–" "Aristotle"

    rpushx key_philosophy2“幸福”“是”“该”“最高”“好”“ –”“亚里斯多德”
Output   
(integer) 0

To change an existing element in a list, run the lset command followed by the key name, the index of the element you want to change, and the new value:

要更改列表中的现有元素,请运行lset命令,后跟键名,要更改的元素的索引以及新值:

  • lset key_philosophy1 5 "sayeth"

    lset key_philosophy1 5“ sayeth”

If you try adding a list element to an existing key that does not contain a list, it will lead to a clash in data types and return an error. For example, the following set command creates a key holding a string, so the following attempt to add a list element to it with lpush will fail:

如果尝试将list元素添加到不包含列表的现有键中,则它将导致数据类型冲突并返回错误。 例如,以下set命令创建一个包含字符串的键,因此以下使用lpush向其添加列表元素的尝试将失败:

  • set key_philosophy3 "What is love?"

    set key_philosophy3“什么是爱?”
  • lpush key_philosophy3 "Baby don't hurt me"

    lpush key_philosophy3“宝贝不要伤害我”
Output   
(error) WRONGTYPE Operation against a key holding the wrong kind of value

It isn’t possible to convert Redis keys from one data type to another, so to turn key_philosophy3 into a list you would need to delete the key and start over with an lpush or rpush command.

不可能将Redis密钥从一种数据类型转换为另一种数据类型,因此要将key_philosophy3转换为列表,您需要删除该密钥并使用lpushrpush命令重新开始。

从列表中检索元素 (Retrieving Elements from a List)

To retrieve a range of items in a list, use the lrange command followed by a start and a stop offset. Each offset is a zero-based index, meaning that 0 represents the first element in the list, 1 represents the next, and so on.

要检索列表中的项目范围,请使用lrange命令,后跟一个开始和一个停止偏移量。 每个偏移量都是从零开始的索引,这意味着0表示列表中的第一个元素, 1表示下一个元素,依此类推。

The following command will return all the elements from the example list created in the previous section:

以下命令将从上一节创建的示例列表中返回所有元素:

  • lrange key_philosophy1 0 7

    lrange key_philosophy1 0 7
Output   
1) "I"2) "think"3) "therefore"4) "I"5) "am"6) "sayeth"7) "Rene"8) "Decartes"

The offsets passed to lrange can also be negative numbers. When used in this case, -1 represents the final element in the list, -2 represents the second-to-last element in the list, and so on. The following example returns the last three elements of the list held in key_philosophy1:

传递给lrange的偏移量也可以为负数。 在这种情况下使用时, -1表示列表中的最后一个元素, -2表示列表中的倒数第二个元素,依此类推。 以下示例返回key_philosophy1中保存的列表的最后三个元素:

  • lrange key_philosophy1 -3 -1

    lrange key_philosophy1 -3 -1
Output   
1) "I"2) "am"3) "sayeth"

To retrieve a single element from a list, you can use the lindex command. However, this command requires you to supply the element’s index as an argument. As with lrange, the index is zero-based, meaning that the first element is at index 0, the second is at index 1, and so on:

要从列表中检索单个元素,可以使用lindex命令。 但是,此命令要求您提供元素的索引作为参数。 与lrange ,索引从零开始,这意味着第一个元素在索引0 ,第二个元素在索引1 ,依此类推:

  • lindex key_philosophy1 4

    lindex key_philosophy1 4
Output   
"am"

To find out how many elements are in a given list, use the llen command, which is short for “list length”:

要查找给定列表中有多少个元素,请使用llen命令,该命令是“ l ist len gth”的缩写:

  • llen key_philosophy1

    llen key_philosophy1
Output   
(integer) 8

If the value stored at the given key does not exist, llen will return an error.

如果存储在给定键上的值不存在,则llen将返回错误。

从列表中删除元素 (Removing Elements from a List)

The lrem command removes the first of a defined number of occurrences that match a given value. To experiment with this, create the following list:

lrem命令除去与给定值匹配的已定义次数的第一个。 要对此进行试验,请创建以下列表:

  • rpush key_Bond "Never" "Say" "Never" "Again" "You" "Only" "Live" "Twice" "Live" "and" "Let" "Die" "Tomorrow" "Never" "Dies"

    rpush key_Bond“从不”,“说”,“从不”,“再次”,“您”,“仅”,“ Live”,“ Twice”,“ Live”,“ Let”,“ Die”,“ Tomorrow”,“ Never”,“ Dies”

The following lrem example will remove the first occurence of the value "Live":

以下lrem示例将删除值"Live"的首次出现:

  • lrem key_Bond 1 "Live"

    lrem key_Bond 1“ Live”

This command will output the number of elements removed from the list:

此命令将输出从列表中删除的元素数:

Output   
(integer) 1

The number passed to an lrem command can also be negative. The following example will remove the last two occurences of the value "Never":

传递给lrem命令的数字也可以为负数。 以下示例将删除值"Never"的最后两个出现:

  • lrem key_Bond -2 "Never"

    lrem key_Bond -2“从不”
Output   
(integer) 2

The lpop command removes and returns the first, or “leftmost” element from a list:

lpop命令删除并返回列表中的第一个或“最左边”的元素:

  • lpop key_Bond

    lpop key_Bond
Output   
"Never"

Likewise, to remove and return the last or “rightmost” element from a list, use rpop:

同样,要从列表中删除并返回最后或“最右边”的元素,请使用rpop

  • rpop key_Bond

    rpop key_Bond
Output   
"Dies"

Redis also includes the rpoplpush command, which removes the last element from a list and pushes it to the beginning of another list:

Redis还包括rpoplpush命令,该命令从列表中删除最后一个元素,并将其推入另一个列表的开头:

  • rpoplpush key_Bond key_AfterToday

    rpoplpush key_Bond key_AfterToday
Output   
"Tomorrow"

If the source and destination keys passed to rpoplpush command are the same, it will essentially rotate the elements in the list.

如果传递给rpoplpush命令的源键和目标键相同,则它将实质上旋转列表中的元素。

结论 (Conclusion)

This guide details a number of commands that you can use to create and manage lists 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命令的更多信息,请参阅关于系列教程。

翻译自:

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

你可能感兴趣的文章
MySQL innert join、left join、right join等理解
查看>>
sdc时序约束
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>