Home  >  Article  >  Web Front-end  >  How to operate key commands in redis

How to operate key commands in redis

坏嘻嘻
坏嘻嘻Original
2018-09-15 11:33:261701browse

The content of this article is about how to operate the key command in redis. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

redis command operation on key

## del key1 key2…key3
Function: This command is used to delete one key or multiple keys when the key exists

Return value: If the key does not exist, the return value is 0. If the key exists, you will delete it. Number of success

dump key
Function: serialize given key

Return value: Return the serialized value

exists key
Function: Detect whether the key exists

Return value: Returns 1 if it exists, 0 if it does not exist

expire key1 10
Function: Set the expiration time for key1 to 10 seconds

Return value: Return 1 if the setting is successful, and the value corresponding to the key taken out after expiration is nil

pexpire key1 10
Function: Set expiration for key1 The time is 10 milliseconds

Return value: 1 is returned if the setting is successful. The value corresponding to the key taken out after expiration is nil

pexpireat key milliseconds -timestamp
Function: Set the timestamp of key expiration, one millisecond timer


    redis 127.0.0.1:6379> SET w3c1 redis
    OK
    redis 127.0.0.1:6379> SET w3c2 mysql
    OK
    redis 127.0.0.1:6379> SET w3c3 mongodb
    OK
    redis 127.0.0.1:6379> KEYS w3c*    1) "w3c3"
    2) "w3c1"
    3) "w3c2"
keys pattern
Function: Find all keys that match the given pattern (pattern)


persist key
#Function: Set the key to be permanently valid


move key db
Function: Move the current database to the database db

Return value: Return 1 successfully, Returns 0 on failure

pttl key
Function: in milliseconds Return the remaining expiration time of the key

Note: ttl key returns the remaining expiration time in seconds

##randomkey Function: Randomly return a database from the current database
Return value:



rename key newkey Function: Modify the name of the key to newkey
Return value: Return ok successfully



##renamenx key newkey
    # newkey 不存在,改名成功

    redis> SET player "MPlyaer"
    OK

    redis> EXISTS best_player
    (integer) 0

    redis> RENAMENX player best_player
    (integer) 1


    # newkey存在时,失败

    redis> SET animal "bear"
    OK

    redis> SET favorite_animal "butterfly"
    OK

    redis> RENAMENX animal favorite_animal
    (integer) 0

    redis> get animal    "bear"

    redis> get favorite_animal    "butterfly"
Function: Only when the key does not exist, rename the key to newkey

type keyFunction: Return the value type stored in key

The above is the detailed content of How to operate key commands in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn