Redis common commands (1) - Key

THE

Form: DEL key [key ...]

Role: delete one or more keys. Non-existing keys are ignored.

Return value: The number of deleted keys.

Example:

192.168.1.100:6379> set testkey 1

OK

# If the deletion is successful, return the number of deleted keys

192.168.1.100:6379> del testkey

(integer) 1

# key does not exist return 0

192.168.1.100:6379> del testkey

(integer) 0

 

EXISTS

Format: EXISTS key

Function: Check whether the key exists.

Return value: Returns 1 if exists, otherwise returns 0.

Example:

192.168.1.100:6379> set testkey 1

OK

192.168.1.100:6379> exists testkey

(integer) 1

192.168.1.100:6379> del testkey

(integer) 1

192.168.1.100:6379> exists testkey

(integer) 0

 

EXPIRE

Format: EXPIRE key seconds

Function: Set the time-to-live for a given key in seconds. When the key expires, it will be automatically deleted. Execute the EXPIRE command on a key that already has a time-to-live, and the new time-to-live will replace the old time-to-live. The unit of PEXPIRE command is milliseconds.

Return value: return 1 if the setting is successful. Returns 0 when the key does not exist or the time-to-live cannot be set for the key.

Example:

192.168.1.100:6379> set testkey 1

OK

192.168.1.100:6379> expire testkey 5

(integer) 1

192.168.1.100:6379> exists testkey

(integer) 1

192.168.1.100:6379> exists testkey

(integer) 0

 

EXPIREAT

Format: EXPIREAT key timestamp

Function: Set the time-to-live for the key, the time is the UNIX timestamp, the unit is seconds. The PEXPIREAT command unit is milliseconds.

Return value: return 1 if the setting is successful. Returns 0 when the key does not exist or the time-to-live cannot be set for the key.

Example:

192.168.1.100:6379> set testkey 1

OK

192.168.1.100:6379> expireat testkey 1486456903

(integer) 1

192.168.1.100:6379> exists testkey

(integer) 1

192.168.1.100:6379> exists testkey

(integer) 0

 

KEYS

Format: KEYS pattern

Function: Find a list of keys that meet the requirements of the pattern format. pattern can use the following wildcards

? matches only one arbitrary character.

* matches any character.

[] matches optional characters.

\x escapes x.

Return value: a list of keys that meet the requirements of pattern format.

Example:

192.168.1.100:6379> set testkey 1

OK

192.168.1.100:6379> keys test*

1) "testkey"

(1.92s)

192.168.1.100:6379> keys test?ey

1) "testkey"

(1.72s)

192.168.1.100:6379> del 'testkey'

(integer) 1

192.168.1.100:6379> keys test*

(empty list or set)

(1.71s)

 

PERSIST

Format: PERSIST key

Effect: Remove the time-to-live for the given key.

Return value: 1 is returned on success, 0 is returned on failure or the key does not exist.

Example:

192.168.1.100:6379> persist testkey

(integer) 0

192.168.1.100:6379> set testkey 1

OK

192.168.1.100:6379> expire testkey 15

(integer) 1

192.168.1.100:6379> persist testkey

(integer) 1

192.168.1.100:6379> exists testkey

(integer) 1

 

SORT

格式:SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]

Function: Sort lists, sets, and ordered sets, and return the sorted results or save them to destination. Sorts numerically by default.

Return value: the number of sorted results or destination elements.

Example:

# Initialization data

192.168.1.100:6379> sadd testkey 4

(integer) 1

192.168.1.100:6379> sadd testkey 3

(integer) 1

192.168.1.100:6379> sadd testkey 8

(integer) 1

 

# Sort the collection, default is ascending

192.168.1.100:6379> sort testkey

1) "3"

2) "4"

3) "8"

 

# sort the collection in descending order

192.168.1.100:6379> sort testkey DESC

1) "8"

2) "4"

3) "3"

 

# Use limit to limit the range

192.168.1.100:6379> sort testkey DESC limit 1 1

1) "4"

 

# Initialization data

192.168.1.100:6379> set testkey_4 100

OK

192.168.1.100:6379> set testkey_3 50

OK

192.168.1.100:6379> set testkey_8 10

OK

 

# Use by to specify the reference key for sorting, and use * to match the value of testkey

192.168.1.100:6379> sort testkey by testkey_ * DESC

1) "4"

2) "3"

3) "8"

 

# use get to return the associated data

192.168.1.100:6379> sort testkey by testkey_ * DESC get testkey_ *

1) "100"

2) "50"

3) "10"

 

#sort sorts by number by default, which will be converted to double-precision numbers. For strings, using sort directly will report an error. Adding alpha can require sort to sort in lexicographical order.

192.168.1.100:6379> sadd testkey a

(integer) 1

192.168.1.100:6379> sadd testkey b

(integer) 1

192.168.1.100:6379> sadd testkey c

(integer) 1

192.168.1.100:6379> sort testkey

(error) ERR One or more scores can't be converted into double

192.168.1.100:6379> sort testkey alpha

1) "a"

2) "b"

3) "c"

 

TTL

Format: TTL key

Function: Query the remaining time of the key with the time-to-live set, in seconds.

Return value: Returns the remaining time under normal circumstances, returns -1 if the key does not set the time-to-live, and returns -2 if the key does not exist.

Example:

192.168.1.100:6379> set testkey 1

OK

192.168.1.100:6379> expire testkey 50

(integer) 1

192.168.1.100:6379> ttl testkey

(integer) 47

192.168.1.100:6379> ttl testkey

(integer) 29

192.168.1.100:6379> ttl testkey

(integer) -2

 

TYPE

Format: TYPE key

Role: return the type of key

Return value: type of key

Example:

# string type

192.168.1.100:6379> set testkey1 2

OK

192.168.1.100:6379> type testkey1

string

 

#collection type

192.168.1.100:6379> sadd testkey2 d

(integer) 1

192.168.1.100:6379> type testkey2

set

 

#list type

192.168.1.100:6379> lpush testkey3 d

(integer) 1

192.168.1.100:6379> type testkey3

list

 

# hash type

192.168.1.100:6379> hset testkey4 tt

(integer) 1

192.168.1.100:6379> type testkey4

hash

 

# Sorted collection type

192.168.1.100:6379> zadd testkey5 100 d

(integer) 1

192.168.1.100:6379> type testkey5

zset

 

Original address: http://caiguoqing.org/post/103

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326743094&siteId=291194637