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

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

redis 哈希里存哈希

介绍 (Introduction)

is an open-source, in-memory key-value data store. A Redis is a data type that represents a mapping between a string field and a string value. Hashes can hold many field-value pairs and are designed to not take up much space, making them ideal for representing data objects. For example, a hash might represent a customer, and include fields like name, address, email, or customer_id.

是一个开源的内存中键值数据存储。 Redis 是一种数据类型,表示字符串字段和字符串值之间的映射。 哈希可以容纳许多字段-值对,并且设计为不占用太多空间,因此使其非常适合表示数据对象。 例如,哈希可能代表一个客户,并且包含诸如nameaddressemailcustomer_id类的字段。

This tutorial will go over how to manage hashes in Redis, from creating them to retrieving and deleting the data held within a hash.

本教程将介绍如何在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 Hashes)

To create a hash, run the hset command. This command accepts the name of the hash key, the field string, and corresponding value string as arguments:

要创建哈希,请运行hset命令。 此命令接受哈希键的名称,字段字符串和相应的值字符串作为参数:

  • hset poet:Verlaine nationality French

    诗人:Verlaine国籍法国

Note: In this example and the following ones, poet:Verlaine is the hash key. Dots, dashes, and colons are commonly used to make multi-word keys and fields more readable. It’s helpful to make sure that your keys follow a consistent and easily readable format.

注意:在此示例及以下示例中, poet:Verlaine是哈希键。 点,破折号和冒号通常用于使多字键和字段更具可读性。 确保您的密钥遵循一致且易于阅读的格式很有帮助。

hset returns (integer) 1 if the field specified is a new field and the value was set correctly:

如果指定的字段是新字段并且值设置正确,则hset返回(integer) 1

Output   
(integer) 1

If, however, you fail to include a value, field, or name for the hash key, hset will return an error.

但是,如果您未能包含哈希键的值,字段或名称,则hset将返回错误。

Also, note that hset will overwrite the contents of the hash if it already exists:

另外,请注意,如果hset已经存在,则hset将覆盖其内容:

  • hset poet:Verlaine nationality Francais

    诗人:凡尔兰国籍弗朗西斯

If the field already exists and its value was updated successfully, hset will return (integer) 0:

如果该字段已经存在并且其值已成功更新,则hset将返回(integer) 0

Output   
(integer) 0

You can also use hsetnx to add fields to hashes, but it will only work if the field does not yet exist. If the specified field does already exist, the hsetnx won’t have any effect and will return (integer) 0:

您也可以使用hsetnx将字段添加到哈希中,但是只有在字段不存在的情况下它才起作用。 如果指定的字段已经存在,则hsetnx将不起作用,并且将返回(integer) 0

  • hsetnx poet:Verlaine nationality French

    hsetnx诗人:法国国籍
Output   
(integer) 0

To set multiple field/value pairs to a given set, use the hmset command followed by the corresponding field/value strings:

要将多个字段/值对设置为给定的集合,请使用hmset命令,后跟相应的字段/值字符串:

  • hmset poet:Verlaine born 1844 died 1896 genre Decadent

    hmset poet:Verlaine出生于1844年,去世了1896年

hmset will just return OK if it was successful.

如果成功, hmset将仅返回OK

从哈希中检索信息 (Retrieving Information from Hashes)

You can determine if a field exists for a given hash with the hexists command:

您可以使用hexists命令确定给定哈希的字段是否存在:

  • hexists poet:Verlaine nationality

    赫克斯主义者诗人:Verlaine国籍

hexists will return (integer) 1 if the field does exist, and (integer) 0 if it doesn’t.

如果该字段确实存在,则hexists将返回(integer) 1如果不存在,则返回(integer) 1 (integer) 0

To return a field’s value, run the hget command followed by the hash key and the field whose value you want to retrieve:

要返回字段的值,请运行hget命令,后跟哈希键和要检索其值的字段:

  • hget poet:Verlaine nationality

    诗人:Verlaine国籍
Output   
"Francais"

hmget uses the same syntax, but can return the values of multiple fields

hmget使用相同的语法,但可以返回多个字段的值

  • hmget poet:Verlaine born died

    hmget诗人:凡尔赛出生
Output   
1) "1844"2) "1896"

If the hash you pass to hget or hmget does not exist, both commands will return (nil):

如果传递给hgethmget的哈希不存在,则两个命令都将返回(nil)

  • hmget poet:Dickinson born died

    hmget诗人:狄金森生前去世
Output   
1) (nil)2) (nil)

To obtain a list of all the fields held within a certain hash, run the hkeys command:

要获取特定哈希中包含的所有字段的列表,请运行hkeys命令:

  • hkeys poet:Verlaine

    hkeys诗人:Verlaine
Output   
1) "nationality"2) "born"3) "died"4) "genre"

Conversely, run hvals to retrieve a list of values held within a hash:

相反,运行hvals以检索哈希值中包含的值的列表:

  • hvals poet:Verlaine

    赫瓦尔斯诗人:Verlaine
Output   
1) "French"2) "1844"3) "1896"4) "Decadent"

To return a list of every field held by a hash and their associated values, run hgetall:

要返回哈希表及其相关值的每个字段的列表,请运行hgetall

  • hgetall poet:Verlaine

    hgetall诗人:Verlaine
Output   
1) "nationality"2) "French"3) "born"4) "1844"5) "died"6) "1896"7) "genre"8) "Decadent"

You can find the number of fields in a hash by running hlen, which stands for “hash length”:

您可以通过运行hlen来找到哈希中的字段数, hlen代表“ h ash len gth”:

  • hlen poet:Verlaine

    海伦诗人:Verlaine
Output   
(integer) 4

You can find the length of the value string associated with a field with hstrlen, which stands for “hash string length”:

您可以使用hstrlen与字段关联的值字符串的长度,该字段代表“ h ash string len gth”:

  • hstrlen poet:Verlaine nationality

    hstrlen诗人:Verlaine国籍
Output   
(integer) 8

hlen will return (integer) 0 if the hash does not exist.

如果哈希不存在, hlen将返回(integer) 0

从哈希中删除字段 (Removing Fields from Hashes)

To delete a field from a hash, run the hdel command. hdel can accept multiple fields as arguments, and will return an integer indicating how many fields were removed from the hash:

要从哈希表中删除字段,请运行hdel命令。 hdel可以接受多个字段作为参数,并将返回一个整数,指示从哈希中删除了多少个字段:

  • hdel poet:Verlaine born died

    诗人:Verlaine出生去世
Output   
(integer) 2

If you pass a field that does not exist to hdel, it will ignore that field but delete any other existing fields you specify.

如果将不存在的字段传递给hdel ,它将忽略该字段,但会删除您指定的任何其他现有字段。

结论 (Conclusion)

This guide details a number of commands used to create and manage hashes 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://oshgb.baihongyu.com/

你可能感兴趣的文章
Linux下获取本机IP地址的代码
查看>>
(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误
查看>>
flex布局
查看>>
python-----python的文件操作
查看>>
java Graphics2d消除锯齿,使字体平滑显示
查看>>
控件中添加的成员变量value和control的区别
查看>>
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>
课堂练习之买书打折最便宜
查看>>
定义函数
查看>>
网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP
查看>>
MQTT协议笔记之mqtt.io项目HTTP协议支持
查看>>
(转)jQuery中append(),prepend()与after(),before()的区别
查看>>
Tecplot: Legend和图像中 Dashed/Dash dot/Long dash 等虚线显示没有区别的问题
查看>>
win8 开发之旅(2) --连连看游戏开发 项目错误的总结
查看>>
视频转换工具ffmpeg
查看>>
一、 object c -基础学习第一天 如何定义一个类
查看>>
C#调用C++编译的DLL详解
查看>>