本文共 6017 字,大约阅读时间需要 20 分钟。
redis中只能保存字符串
is an open-source, in-memory key-value data store. In Redis, are the most basic type of value you can create and manage. This tutorial provides an overview of how to create and retrieve strings, as well as how to manipulate the values held by string keys.
是一个开源的内存中键值数据存储。 在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连接到托管数据库。
Keys that hold strings can only hold one value; you cannot store more than one string in a single key. However, strings in Redis are binary-safe, meaning a Redis string can hold any kind of data, from alphanumeric characters to JPEG images. The only limit is that strings must be 512 MB long or less.
存放字符串的键只能存放一个值。 您不能在一个键中存储多个字符串。 但是,Redis中的字符串是二进制安全的,这意味着Redis字符串可以保存任何类型的数据,从字母数字字符到JPEG图像。 唯一的限制是字符串的长度必须小于或等于512 MB。
To create a string, use the set
command. For example, the following set
command creates a key named key_Welcome1
that holds the string "Howdy"
:
要创建字符串,请使用set
命令。 例如,以下set
命令创建一个名为key_Welcome1
的键,其中包含字符串"Howdy"
:
Output OK
To set multiple strings in one command, use mset
:
要在一个命令中设置多个字符串,请使用mset
:
You can also use the append
command to create strings:
您还可以使用append
命令创建字符串:
If the string was created successfully, append
will output an integer equal to how many characters the string includes:
如果成功创建了字符串,则append
将输出一个整数,该整数等于该字符串包含的字符数:
Output (integer) 16
Note that append
can also be used to change the contents of strings. See the section on for details on this.
请注意, append
也可用于更改字符串的内容。 有关详细信息,请参见有关的部分。
To retrieve a string, use the get
command:
要检索字符串,请使用get
命令:
Output "Howdy"
To retrieve multiple strings with one command, use mget
:
要使用一个命令检索多个字符串,请使用mget
:
Output 1) "Howdy"2) "there"3) "partners,"4) "welcome to Texas"
For every key passed to mget
that doesn’t hold a string value or doesn’t exist at all, the command will return nil
.
对于传递给mget
每个不包含字符串值或根本不存在的键,命令将返回nil
。
If a string is made up of an integer, you can run the incr
command to increase it by one:
如果字符串由整数组成,则可以运行incr
命令将其增加一:
Output (integer) 4
Similarly, you can use the incrby
command to increase a numeric string’s value by a specific increment:
同样,可以使用incrby
命令将数字字符串的值增加特定的增量:
Output (integer) 20
The decr
and decrby
commands work the same way, but they decrease the integer stored in a numeric string:
decr
和decrby
命令以相同的方式工作,但是它们减少了存储在数字字符串中的整数:
Output (integer) 19
Output (integer) 3
If an alphabetic string already exists, append
will append the value onto the end of the existing value and return the new length of the string. To illustrate, the following command appends ", y'all"
to the string held by the key key_Welcome4
, so now the string will read "welcome to Texas, y'all"
:
如果字母字符串已经存在,则append
将把该值附加到现有值的末尾,并返回该字符串的新长度。 为了说明这一点,以下命令将", y'all"
附加到键key_Welcome4
所持有的字符串上,因此现在该字符串将显示为"welcome to Texas, y'all"
:
Output (integer) 15
You can also append integers to a string holding a numeric value. The following example appends 45
to 3
, the integer held in key_1
, so it will then hold 345
. In this case, append
will also return the new length of the string, rather than its new value:
您也可以将整数附加到包含数字值的字符串中。 下面的示例将45
保留在key_1
的整数3
key_1
,因此它将保留345
。 在这种情况下, append
也将返回字符串的新长度,而不是其新值:
Output (integer) 3
Because this key still only holds a numeric value, you can perform the incr
and decr
operations on it. You can also append alphabetic characters to an integer string, but if you do this then running incr
and decr
on the string will produce an error as the string value is no longer an integer.
由于此键仍仅保留数字值,因此可以对其执行incr
和decr
操作。 您也可以将字母字符附加到整数字符串中,但是如果这样做,则在字符串上运行incr
和decr
会产生错误,因为字符串值不再是整数。
This guide details a number of commands used to create and manage strings 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://sphgb.baihongyu.com/