Redis common command

Before you can use Redis, you need to install Redis first. For details on how to install Redis, refer to my other article Redis Installation and Use (Windows version).

Operation is the key

View which keys the current library has

keys *
Copy the code

Remove the key

del key 
Copy the code

Set the key expiration time

expire key second
Copy the code

Check that the specified key life cycle has expired. Return -2 Permanent -1

ttl key
Copy the code


Manipulation library

In Redis, there are 16 libraries by default, named 0-15

Select the specific library

select index
Copy the code

Clear the current library

flushdb
Copy the code

Clear all libraries

flushall
Copy the code


String Common commands

Setting a single value

set key value
Copy the code

Get a single value

get key
Copy the code

Set multiple values at once

mset k1 v1 k2 v2 k3 v3...
Copy the code

Take multiple values based on multiple keys

mget k1 k2 k3
Copy the code

On the 1

incr key
Copy the code

Since the minus 1

decr key
Copy the code

Increments the specified number

incrby key number
Copy the code

Subtracts the specified number

decrby key number
Copy the code


List Common commands

Add data (left), final effect V3 v2 V1

lpush key v1 v2 v3
Copy the code

Add data (add on the right), resulting in v1, V2, v3

rpush key v1 v2 v3
Copy the code

Remove the far left

lpop key
Copy the code

Remove the far right

rpop key
Copy the code

Query all data in the collection

lrange key 0 -1
Copy the code

Queries the specified index range data, including header and tail

lrange key startIndex endIndex
Copy the code

Queries the specified value against the index

lindex key index
Copy the code

Delete count values from the specified collection

  • Count >0: deletes from the left
  • Count =0: deletes all values in the specified collection
  • Count <0: delete starts on the right
lrem key count value
Copy the code

Set Common commands

Add data, note: The added data is unordered

sadd key v1 v2 v3
Copy the code

Query all data

smembers key
Copy the code

Deletes the specified value. You can delete multiple values

srem key v1 v2 ...
Copy the code

Common hash commands

Setting a single value

hset key field value
Copy the code

Get a single value

hget key field
Copy the code

Set multiple values at once

hmset key field1 value1 field2 value2...
Copy the code

Take multiple values at once

hmget key field1 field2...
Copy the code

Get all keys

hkeys  key
Copy the code

Get all values

hvals key
Copy the code

Get all keys and values

hgetall key
Copy the code

Sorted set Common command

Sorted set: Sorted and not repeatable

  • Each element is associated with a score of type double, which is how Redis sorts collections
  • The members of an ordered set are unique, but the score can be repeated.

Add data

zadd key score1 value1 score2 value2
Copy the code

Gets the number of ordered collection members

zcard key2
Copy the code

Computes the number of members in an ordered set with a specified interval fraction, including heads and tails

zcount key minScore MaxScore
Copy the code

Returns an ordered collection of members within a specified interval, including head and tail, by indexing the interval

zrange key startIndex endIndex 
Copy the code