天天看点

Redis 常用命令Redis 常用命令

Redis 常用命令

测试连通

ping

ping

[message]

CONNECTION

Ping the server

>> ping

"PONG"
           

字符串(strings)

set

set

set

key value [expiration EX seconds|PX milliseconds] [ NX| XX]

NX: Only set the key if it does not already exist.

XX: Only set the key if it already exist.

STRING

Set the string value of a key

>> set name Lone

"OK"
// 设置key 300秒后过期
>> set name Lone ex 300

"OK"
// 查看过期剩余时长
>> ttl name

(integer) 296
// 清除过期时间
>> persist name

(integer) 1
>> ttl name

(integer) -1
           

mset

mset

key value [key value …]

STRING

Set multiple keys to multiple values

>> mset sex 1 age 18

"OK"
           

get

get

key

STRING

Get the value of a key

>> get name

"Lone"
           

mget

mget

key [key …]

STRING

Get the values of all the given keys

>> mget name age sex

1) "Lone"
2) "18"
3) "1"
           

getset

getset

key value

STRING

Set the string value of a key and return its old value

>> getset name Chloe

"Lone"
           

散列(hashes)

hset

hset

key field value

HASH

Set the string value of a hash field

>> hset user name Lone

(integer) 1
           

hmset

hmset

key field value [field value …]

HASH

Set multiple hash fields to multiple values

>> hmset user sex 1 age 18

"OK"
           

hget

hget

key field

HASH

Get the value of a hash field

>> hget user name

"Lone"
           

hmget

hmget

key field [field …]

HASH

Get the values of all the given hash fields

>> hmget user name sex 

1) "Lone"
2) "1"
           

hgetall

hgetall

key

HASH

Get all the fields and values in a hash

>> hgetall user

1) "name"
2) "Lone"
3) "sex"
4) "1"
5) "age"
6) "18"
           

列表(lists)

lpush

lpush

key value [value …]

LIST

Prepend one or multiple values to a list

>> lpush students Lone Chloe

(integer) 2
           

rpush

rpush

key value [value …]

LIST

Append one or multiple values to a list

>> rpush students May Lily

(integer) 4
           

llen

llen

key

LIST

Get the length of a list

>> llen students

(integer) 4
           

lrange

lrange

key start stop

LIST

Get a range of elements from a list

>> lrange students 0 1

1) "Chloe"
2) "Lone"
           

lrem

lrem

key count value

LIST

Remove elements from a list

>> lrem students 1 Lily

(integer) 1
           

lpop

lpop

key

LIST

Remove and get the first element in a list

>> lpop students

"Chloe"
           

rpop

rpop

key

LIST

Remove and get the last element in a list

>> rpop students

"May"
           

集合(sets)

sadd

sadd

key member [member …]

SET

Add one or more members to a set

>> sadd letters aaa bbb ccc ddd eee aaa

(integer) 5
           

smembers

smembers

key

SET

Get all the members in a set

scard

scard

key

SET

Get the number of members in a set

>> scard letters

(integer) 5
           

srem

srem

key member [member …]

SET

Remove one or more members from a set

>> srem letters aaa ccc

(integer) 2
           

有序集合(sorted sets)

zadd

zadd

key

[ NX| XX] [ CH] [ INCR] score member [score member …]

SORTED_SET

Add one or more members to a sorted set, or update its score if it already exists

>> zadd score 7 lone 3 chloe 6 lily 10 lucy

(integer) 4
           

zrange

zrange

key start stop

[ WITHSCORES]

SORTED_SET

Return a range of members in a sorted set, by index

>> zrange score 0 -1

1) "chloe"
2) "lily"
3) "lone"
4) "lucy"

>> zrange score 0 4

1) "chloe"
2) "lily"
3) "lone"
4) "lucy"

// 返回分数 3 < score < 11 的成员
>> zrange score (3 (11  BYSCORE

1) "lily"
2) "lone"
3) "lucy"
           

通用命令

expire

expire

key seconds

GENERIC

Set a key’s time to live in seconds

>> expire user 10

(integer) 1
           

expireat

expireat

key timestamp

GENERIC

Set the expiration for a key as a UNIX timestamp

del

del

key [key …]

GENERIC

Delete a key

>> del a la

(integer) 2