本文共 3675 字,大约阅读时间需要 12 分钟。
redis 自增步数
When you have up and running, you can start using it!
启动并运行后,就可以开始使用它了!
The simplest way is to use redis-cli
, an application installed when you install Redis.
最简单的方法是使用redis-cli
,这是在安装Redis时安装的应用程序。
It’s a built-in way to write commands to Redis without having to set up an application in order to do so.
这是一种将命令写入Redis的内置方式,而无需为此设置应用程序。
You can connect to a remote Redis server using
redis-cli -h <host> -p <port> -a <password>
您可以使用
redis-cli -h <host> -p <port> -a <password>
连接到远程Redis服务器。
Once you’re in the Redis CLI app, you can start storing data into it.
进入Redis CLI应用程序后,就可以开始将数据存储到其中了。
Add a value using the structure SET <key> <value>
:
使用结构SET <key> <value>
添加一个值:
SET name "Flavio"
Retrieve a value using the structure GET <key>
:
使用结构GET <key>
检索值:
We can also check if a key exists using EXISTS <key>
:
我们还可以使用EXISTS <key>
检查密钥是否存在:
The command returns either 1 (exists) or 0 (does not exist).
该命令返回1(存在)或0(不存在)。
A variation of SET
allows us to only set a key if it does not exist yet:
SET
一种变体允许我们仅在尚不存在的情况下设置键:
SETNX name "Roger"
Delete a key using DEL <key>
:
使用DEL <key>
删除DEL <key>
:
You can list all keys inserted using KEYS *
您可以列出使用KEYS *
插入的所有键
Or you can filter using a pattern like KEYS n*
to only list keys starting with n
, for example.
或者,您可以使用类似KEYS n*
的模式进行过滤,以仅列出以n
开头的键。
Each value stored can hold up to 512 MB in value.
每个存储的值最多可容纳512 MB。
A key can be temporarily stored, and removed automatically when the timer ends:
可以将密钥临时存储,并在计时器结束时自动将其删除:
SETEX <key> <seconds> <value>
SETEX <key> <seconds> <value>
You can get the time remaining for a key to be cleared using TTL <key>
您可以使用TTL <key>
清除密钥的剩余时间。
In this example I set a name
key with Flavio
as value, and using TTL I can check how much time is left until the key will return the value. Once the timer expires, it results in a null value (nil
):
在此示例中,我设置了一个以Flavio
作为值的name
键,并使用TTL可以检查剩下多少时间直到该键返回该值。 计时器到期后,将产生一个空值( nil
):
You can also set an existing key to expire using EXPIRE <key> seconds>
.
您还可以使用EXPIRE <key> seconds>
将现有密钥设置为过期。
A numeric value can be incremented using INCR <key>
and decremented using DECR <key>
. You can also use INCRBY <key> <amount>
and DECRBY <key> <amount>
to increment a key value by a specific amount:
数值可以使用INCR <key>
递增,也可以使用DECR <key>
递减。 您还可以使用INCRBY <key> <amount>
和DECRBY <key> <amount>
将键值增加特定量:
Those commands are very well suited for high concurrent operations where many clients might interact with the same data, to ensure atomic transactions.
这些命令非常适合于高并发操作,在该操作中许多客户端可能与同一数据进行交互,以确保原子事务 。
The most common example is when 2 different clients try to increment the same number.
最常见的示例是两个不同的客户端尝试增加相同的数字时。
On a database like PostgreSQL or MongoDB you first get the number value, you increment it, then you make a request to the server to increment it.
在PostgreSQL或MongoDB之类的数据库上,您首先要获得数字值,然后对其进行递增,然后向服务器发出请求以使其递增。
Say the value is 1
. If two clients read the value using GET
then they call SET
to increment it independently, in the end if there is nothing preventing the concurrent change to happen, the result will be 2
. Redis prevents this problem at the root.
说值是1
。 如果两个客户端使用GET
读取值,则它们调用SET
进行独立递增,最后如果没有什么阻止并发更改发生,则结果将为2
。 Redis从根本上防止了此问题。
So far we’ve worked with simple data types like integers and strings.
到目前为止,我们已经使用了简单的数据类型,例如整数和字符串。
Redis can support more complex structures.
Redis可以支持更复杂的结构。
Let’s see in the next lessons how to work with:
让我们在下一课中了解如何使用:
翻译自:
redis 自增步数
转载地址:http://ixmgb.baihongyu.com/