1. Overview:
This article will mainly describe the Redis commands related to Key. Learning these commands is a very important foundation for learning Redis, and it is also a powerful tool to fully tap the potential of Redis. (Recommended: redis video tutorial)
2. Related command list:
Command prototype | Time complexity | Command description | Return value |
KEYS pattern | O(N) | The N in time complexity represents the number of Keys in the database. Get all Keys matching the pattern parameter. It should be noted that we should try to avoid calling this command in our normal operations, because for large databases, this command is very time-consuming and has a relatively large impact on the performance of the Redis server. pattern supports glob-style wildcard format, such as * represents any one or more characters, ? represents any character, and [abc] represents any letter in square brackets. | List of keys matching the pattern. |
DEL key [key ...] | O(N) | The N in time complexity represents the number of deleted Keys. Delete the keys specified in the parameters from the database. If the specified key does not exist, it will be ignored. It should also be pointed out that if the data type associated with the specified Key is not a String type, but a container type such as List, Set, Hashes, and Sorted Set, the time complexity of this command to delete each key is O(M), where M represents the number of elements in the container. For String type Key, its time complexity is O(1). | The actual number of deleted Keys. |
EXISTS key | O(1) | Determine whether the specified key exists. | 1 means it exists, 0 means it doesn't exist. |
MOVE key db | O(1) | Move the key specified in the current database to the database specified in the parameter. If the key already exists in the target database, or does not exist in the current database, this command will do nothing and return 0. | Returns 1 if the move is successful, otherwise 0. |
RENAME key newkey | O(1) | Rename the specified key. If the two Keys commands in the parameters are the same, Or the source Key does not exist, this command will return relevant error information. If newKey already exists, it will be overwritten directly. | |
RENAMENX key newkey | O(1) | If the new value does not exist, the original value in the parameter will be Modify to new value. Other conditions are consistent with RENAME. | 1 means the modification is successful, otherwise 0. |
PERSIST key | O(1) | If the Key has an expiration time, this command will eliminate its expiration time so that the Key will no longer There are timeouts, but persistent storage is possible. | 1 means that the Key's expiration time has been removed, 0 means that the Key does not exist or has no expiration time. |
EXPIRE key seconds | O(1) | This command sets the timeout seconds for the Key specified in the parameter. Afterwards, the Key is automatically deleted. If the Key is modified before the timeout occurs, the timeout associated with the key will be removed. | 1 means the timeout is set, 0 means the Key does not exist or cannot be set. |
EXPIREAT key timestamp | O(1) | The logical function of this command is exactly the same as EXPIRE, the only difference is the timeout specified by the command Time is absolute time, not relative time. The time parameter is in Unix timestamp format, which is the number of seconds that have elapsed since January 1, 1970. | 1 indicates that the timeout is set, 0 indicates that the Key does not exist or cannot be set. |
TTL key | O(1) | Get the remaining timeout description of the key. | Returns the remaining description. If the key does not exist or has no timeout setting, it returns -1. |
RANDOMKEY | O(1) | Randomly returns a Key from the currently opened database. | The random key returned, or nil if the database is empty. |
TYPE key | O(1) | Get the type of value associated with the key specified in the parameter. This command will return it in string format . | The returned strings are string, list, set, hash and zset. If the key does not exist, none is returned. |
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination] | O(N M*log(M)) | This command is relatively complex, so we only give the most basic usage here. Interested netizens can refer to the official documentation of redis. | Return the sorted original list. |
3. Command examples:
1. KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX:
#在Shell命令行下启动Redis客户端工具。 /> redis-cli #清空当前选择的数据库,以便于对后面示例的理解。 redis 127.0.0.1:6379> flushdb OK #添加String类型的模拟数据。 redis 127.0.0.1:6379> set mykey 2 OK redis 127.0.0.1:6379> set mykey2 "hello" OK #添加Set类型的模拟数据。 redis 127.0.0.1:6379> sadd mysetkey 1 2 3 (integer) 3 #添加Hash类型的模拟数据。 redis 127.0.0.1:6379> hset mmtest username "stephen" (integer) 1 #根据参数中的模式,获取当前数据库中符合该模式的所有key,从输出可以看出,该命令在执行时并不区分与Key关联的Value类型。 redis 127.0.0.1:6379> keys my* 1) "mysetkey" 2) "mykey" 3) "mykey2" #删除了两个Keys。 redis 127.0.0.1:6379> del mykey mykey2 (integer) 2 #查看一下刚刚删除的Key是否还存在,从返回结果看,mykey确实已经删除了。 redis 127.0.0.1:6379> exists mykey (integer) 0 #查看一下没有删除的Key,以和上面的命令结果进行比较。 redis 127.0.0.1:6379> exists mysetkey (integer) 1 #将当前数据库中的mysetkey键移入到ID为1的数据库中,从结果可以看出已经移动成功。 redis 127.0.0.1:6379> move mysetkey 1 (integer) 1 #打开ID为1的数据库。 redis 127.0.0.1:6379> select 1 OK #查看一下刚刚移动过来的Key是否存在,从返回结果看已经存在了。 redis 127.0.0.1:6379[1]> exists mysetkey (integer) 1 #在重新打开ID为0的缺省数据库。 redis 127.0.0.1:6379[1]> select 0 OK #查看一下刚刚移走的Key是否已经不存在,从返回结果看已经移走。 redis 127.0.0.1:6379> exists mysetkey (integer) 0 #准备新的测试数据。 redis 127.0.0.1:6379> set mykey "hello" OK #将mykey改名为mykey1 redis 127.0.0.1:6379> rename mykey mykey1 OK #由于mykey已经被重新命名,再次获取将返回nil。 redis 127.0.0.1:6379> get mykey (nil) #通过新的键名获取。 redis 127.0.0.1:6379> get mykey1 "hello" #由于mykey已经不存在了,所以返回错误信息。 redis 127.0.0.1:6379> rename mykey mykey1 (error) ERR no such key #为renamenx准备测试key redis 127.0.0.1:6379> set oldkey "hello" OK redis 127.0.0.1:6379> set newkey "world" OK #由于newkey已经存在,因此该命令未能成功执行。 redis 127.0.0.1:6379> renamenx oldkey newkey (integer) 0 #查看newkey的值,发现它也没有被renamenx覆盖。 redis 127.0.0.1:6379> get newkey "world"
2. PERSIST/EXPIRE/EXPIREAT/TTL:
#为后面的示例准备的测试数据。 redis 127.0.0.1:6379> set mykey "hello" OK #将该键的超时设置为100秒。 redis 127.0.0.1:6379> expire mykey 100 (integer) 1 #通过ttl命令查看一下还剩下多少秒。 redis 127.0.0.1:6379> ttl mykey (integer) 97 #立刻执行persist命令,该存在超时的键变成持久化的键,即将该Key的超时去掉。 redis 127.0.0.1:6379> persist mykey (integer) 1 #ttl的返回值告诉我们,该键已经没有超时了。 redis 127.0.0.1:6379> ttl mykey (integer) -1 #为后面的expire命令准备数据。 redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> set mykey "hello" OK #设置该键的超时被100秒。 redis 127.0.0.1:6379> expire mykey 100 (integer) 1 #用ttl命令看一下当前还剩下多少秒,从结果中可以看出还剩下96秒。 redis 127.0.0.1:6379> ttl mykey (integer) 96 #重新更新该键的超时时间为20秒,从返回值可以看出该命令执行成功。 redis 127.0.0.1:6379> expire mykey 20 (integer) 1 #再用ttl确认一下,从结果中可以看出果然被更新了。 redis 127.0.0.1:6379> ttl mykey (integer) 17 #立刻更新该键的值,以使其超时无效。 redis 127.0.0.1:6379> set mykey "world" OK #从ttl的结果可以看出,在上一条修改该键的命令执行后,该键的超时也无效了。 redis 127.0.0. 1:6379> ttl mykey (integer) -1
3. TYPE/RANDOMKEY/SORT:
#由于mm键在数据库中不存在,因此该命令返回none。 redis 127.0.0.1:6379> type mm none #mykey的值是字符串类型,因此返回string。 redis 127.0.0.1:6379> type mykey string #准备一个值是set类型的键。 redis 127.0.0.1:6379> sadd mysetkey 1 2 (integer) 2 #mysetkey的键是set,因此返回字符串set。 redis 127.0.0.1:6379> type mysetkey set #返回数据库中的任意键。 redis 127.0.0.1:6379> randomkey "oldkey" #清空当前打开的数据库。 redis 127.0.0.1:6379> flushdb OK #由于没有数据了,因此返回nil。 redis 127.0.0.1:6379> randomkey (nil)
For more redis knowledge, please pay attention redis introductory tutorial column.
The above is the detailed content of Detailed explanation of key related commands in redis. For more information, please follow other related articles on the PHP Chinese website!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
