Redis tutorial of vii: Key operation command

  • 2020-05-06 11:58:57
  • OfStack

Overview:

In previous posts in this series,           focused on commands related to Redis data types, such as String, List, Set, Hashes, and Sorted-Set. One thing these commands have in common is that all operations are for Value, which is associated with Key. This blog post will focus on the Redis command related to Key. Learning these commands is an important foundation for learning Redis and a powerful tool to fully exploit the potential of Redis.
          in this blog post, as always, we will give a detailed list of all relevant commands and typical examples for our current study and future reference.

ii. List of related commands:

命令原型 时间复杂度 命令描述 返回值
KEYS pattern O(N) 时间复杂度中的N表示数据库中Key的数量。获取所有匹配pattern参数的Keys。需要说明的是,在我们的正常操作中应该尽量避免对该命令的调用,因为对于大型数据库而言,该命令是非常耗时的,对Redis服务器的性能打击也是比较大的。pattern支持glob-style的通配符格式,如*表示任意一个或多个字符,?表示任意字符,[abc]表示方括号中任意一个字母。 匹配模式的键列表。
DEL key [key ...] O(N) 时间复杂度中的N表示删除的Key数量。从数据库删除中参数中指定的keys,如果指定键不存在,则直接忽略。还需要另行指出的是,如果指定的Key关联的数据类型不是String类型,而是List、Set、Hashes和Sorted Set等容器类型,该命令删除每个键的时间复杂度为O(M),其中M表示容器中元素的数量。而对于String类型的Key,其时间复杂度为O(1)。 实际被删除的Key数量。
EXISTS key  O(1) 判断指定键是否存在。 1表示存在,0表示不存在。
MOVE key db  O(1) 将当前数据库中指定的键Key移动到参数中指定的数据库中。如果该Key在目标数据库中已经存在,或者在当前数据库中并不存在,该命令将不做任何操作并返回0。   移动成功返回1,否则0。
RENAME key newkey  O(1) 为指定指定的键重新命名,如果参数中的两个Keys的命令相同,或者是源Key不存在,该命令都会返回相关的错误信息。如果newKey已经存在,则直接覆盖。   
RENAMENX key newkey O(1) 如果新值不存在,则将参数中的原值修改为新值。其它条件和RENAME一致。 1表示修改成功,否则0。
PERSIST key O(1) 如果Key存在过期时间,该命令会将其过期时间消除,使该Key不再有超时,而是可以持久化存储。 1表示Key的过期时间被移出,0表示该Key不存在或没有过期时间。
EXPIRE key seconds  O(1)  该命令为参数中指定的Key设定超时的秒数,在超过该时间后,Key被自动的删除。如果该Key在超时之前被修改,与该键关联的超时将被移除。  1表示超时被设置,0则表示Key不存在,或不能被设置。
EXPIREAT key timestamp  O(1)  该命令的逻辑功能和EXPIRE完全相同,唯一的差别是该命令指定的超时时间是绝对时间,而不是相对时间。该时间参数是Unix timestamp格式的,即从1970年1月1日开始所流经的秒数。 1表示超时被设置,0则表示Key不存在,或不能被设置。 
TTL key  O(1) 获取该键所剩的超时描述。  返回所剩描述,如果该键不存在或没有超时设置,则返回-1。
RANDOMKEY O(1)   从当前打开的数据库中随机的返回一个Key。 返回的随机键,如果该数据库是空的则返回nil。
TYPE key  O(1)  获取与参数中指定键关联值的类型,该命令将以字符串的格式返回。 返回的字符串为string、list、set、hash和zset,如果key不存在返回none。
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]  O(N+M*log(M))  这个命令相对来说是比较复杂的,因此我们这里只是给出最基本的用法,有兴趣的网友可以去参考redis的官方文档。 返回排序后的原始列表。

Example of command:

    1. KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX:
   


    # in Shell Start from the command line Redis Client tools.
    /> redis-cli
    # Clear the currently selected database for understanding the following examples.
    redis 127.0.0.1:6379> flushdb
    OK
    # add String Type of simulation data.
    redis 127.0.0.1:6379> set mykey 2
    OK
    redis 127.0.0.1:6379> set mykey2 "hello"
    OK
    # add Set Type of simulation data.
    redis 127.0.0.1:6379> sadd mysetkey 1 2 3
    (integer) 3
    # add Hash Type of simulation data.
    redis 127.0.0.1:6379> hset mmtest username "stephen"
    (integer) 1
    # According to the schema in the parameter, get all that conforms to the schema in the current database key From the output, it can be seen that the command is not distinguished from Key The associated Value Type.
    redis 127.0.0.1:6379> keys my*
    1) "mysetkey"
    2) "mykey"
    3) "mykey2"
    # Two were deleted Keys .
    redis 127.0.0.1:6379> del mykey mykey2
    (integer) 2
    # Take a look at what you just deleted Key Does it still exist? From the return result, mykey It has been deleted.
    redis 127.0.0.1:6379> exists mykey
    (integer) 0
    # Check what's not deleted Key To compare with the results of the above command.
    redis 127.0.0.1:6379> exists mysetkey
    (integer) 1
    # Will be in the current database mysetkey The key into the ID for 1 In the database, you can see from the results that the move has been successful.
    redis 127.0.0.1:6379> move mysetkey 1
    (integer) 1
    # Open the ID for 1 Database.
    redis 127.0.0.1:6379> select 1
    OK
    # So let's look at what we just moved Key Whether it exists or not, it already exists from the result returned.
    redis 127.0.0.1:6379[1]> exists mysetkey
    (integer) 1
    # re-open ID for 0 Default database.
    redis 127.0.0.1:6379[1]> select 0
    OK
    # Take a look at what's just been removed Key If it no longer exists, it has been removed from the returned result.
    redis 127.0.0.1:6379> exists mysetkey
    (integer) 0
    # Prepare new test data.    
    redis 127.0.0.1:6379> set mykey "hello"
    OK
    # will mykey renamed mykey1
    redis 127.0.0.1:6379> rename mykey mykey1
    OK
    # Due to the mykey It has been renamed. Get it again and it will return nil .
    redis 127.0.0.1:6379> get mykey
    (nil)
    # Gets by the new key name.
    redis 127.0.0.1:6379> get mykey1
    "hello"
    # Due to the mykey It no longer exists, so an error message is returned.
    redis 127.0.0.1:6379> rename mykey mykey1
    (error) ERR no such key
    # for renamenx Ready to test key
    redis 127.0.0.1:6379> set oldkey "hello"
    OK
    redis 127.0.0.1:6379> set newkey "world"
    OK
    # Due to the newkey Already exists, so the command was not executed successfully.
    redis 127.0.0.1:6379> renamenx oldkey newkey
    (integer) 0
    # To view newkey The value of and found that it was also not by renamenx Coverage.
    redis 127.0.0.1:6379> get newkey
    "world"
  
         
    2. PERSIST/EXPIRE/EXPIREAT/TTL:        
   

    # Test data for the following examples.
    redis 127.0.0.1:6379> set mykey "hello"
    OK
    # Sets the timeout for this key to 100 Seconds.
    redis 127.0.0.1:6379> expire mykey 100
    (integer) 1
    # through ttl Command to see how many seconds are left.
    redis 127.0.0.1:6379> ttl mykey
    (integer) 97
    # immediately persist Command, the key with the timeout becomes the persistent key, which is the Key The timeout of.
    redis 127.0.0.1:6379> persist mykey
    (integer) 1
    #ttl The return value of
    redis 127.0.0.1:6379> ttl mykey
    (integer) -1
    # For the back of the expire The command prepares the data.
    redis 127.0.0.1:6379> del mykey
    (integer) 1
    redis 127.0.0.1:6379> set mykey "hello"
    OK
    # Sets the key's timeout to be 100 Seconds.
    redis 127.0.0.1:6379> expire mykey 100
    (integer) 1
    # with ttl The command looks at how many seconds are left, and from the result you can see that there are still left 96 Seconds.
    redis 127.0.0.1:6379> ttl mykey
    (integer) 96
    # The timeout time for reupdating the key is 20 Second, the return value indicates that the command executed successfully.
    redis 127.0.0.1:6379> expire mykey 20
    (integer) 1
    # Then use ttl Just to be sure, you can see from the results that it has been updated.
    redis 127.0.0.1:6379> ttl mykey
    (integer) 17
    # Immediately update the value of the key to invalidate its timeout.
    redis 127.0.0.1:6379> set mykey "world"
    OK
    # from ttl The result is that the key's timeout is also invalid after the last command that modified the key was executed.
    redis 127.0.0.1:6379> ttl mykey
    (integer) -1

    3. TYPE/RANDOMKEY/SORT:
   

    # Due to the mm The key does not exist in the database, so the command returns none .
    redis 127.0.0.1:6379> type mm
    none
    #mykey Is of type string and therefore returns string .
    redis 127.0.0.1:6379> type mykey
    string
    # Prepare a value of set Key of type.
    redis 127.0.0.1:6379> sadd mysetkey 1 2
    (integer) 2
    #mysetkey The key is set , so returns a string set .
    redis 127.0.0.1:6379> type mysetkey
    set
    # Returns any key in the database.
    redis 127.0.0.1:6379> randomkey
    "oldkey"
    # Clean the currently open database.
    redis 127.0.0.1:6379> flushdb
    OK
    # Returns because there is no data left nil .
    redis 127.0.0.1:6379> randomkey
    (nil)
 


Related articles: