redis中只能保存字符串_如何在Redis中管理字符串

redis中只能保存字符串

介绍 (Introduction)

Redis is an open-source, in-memory key-value data store. In Redis, strings 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是一个开源的内存中键值数据存储。 在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连接到托管数据库。

创建和管理字符串 (Creating and Managing Strings)

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"

  • set key_Welcome1 "Howdy"

    设置key_Welcome1“ Howdy”

   
   
Output
OK

To set multiple strings in one command, use mset:

要在一个命令中设置多个字符串,请使用mset

  • mset key_Welcome2 "there" key_Welcome3 "partners,"

    mset key_Welcome2“那里” key_Welcome3“伙伴”

You can also use the append command to create strings:

您还可以使用append命令创建字符串:

  • append key_Welcome4 "welcome to Texas"

    追加key_Welcome4“欢迎来到德克萨斯州”

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 manipulating strings for details on this.

请注意, append也可用于更改字符串的内容。 有关详细信息,请参见有关处理字符串的部分。

检索字符串 (Retrieving Strings)

To retrieve a string, use the get command:

要检索字符串,请使用get命令:

  • get key_Welcome1

    取得key_Welcome1

   
   
Output
"Howdy"

To retrieve multiple strings with one command, use mget:

要使用一个命令检索多个字符串,请使用mget

  • mget key_Welcome1 key_Welcome2 key_Welcome3 key_Welcome4

    mget key_Welcome1 key_Welcome2 key_Welcome3 key_Welcome4

   
   
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

操纵弦 (Manipulating Strings)

If a string is made up of an integer, you can run the incr command to increase it by one:

如果字符串由整数组成,则可以运行incr命令将其增加一:

  • set key_1 3

    设置key_1 3
  • incr key_1

    incr key_1

   
   
Output
(integer) 4

Similarly, you can use the incrby command to increase a numeric string’s value by a specific increment:

同样,可以使用incrby命令将数字字符串的值增加特定的增量:

  • incrby key_1 16

    incrby key_1 16

   
   
Output
(integer) 20

The decr and decrby commands work the same way, but they decrease the integer stored in a numeric string:

decrdecrby命令以相同的方式工作,但是它们减少了存储在数字字符串中的整数:

  • decr key_1

    decr key_1

   
   
Output
(integer) 19
  • decrby key_1 16

    decrby key_1 16

   
   
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"

  • append key_Welcome4 ", y'all"

    追加key_Welcome4“,大家”

   
   
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也将返回字符串的新长度,而不是其新值:

  • append key_1 45

    追加key_1 45

   
   
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.

由于此键仍仅保留数字值,因此可以对其执行incrdecr操作。 您也可以将字母字符附加到整数字符串中,但是如果这样做,则在字符串上运行incrdecr会产生错误,因为字符串值不再是整数。

结论 (Conclusion)

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

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

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

redis中只能保存字符串

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值