Redis series (two) – syntax and command

Redis series article:
redis series (1) – installing and starting
redis series (two) – syntax and command

I. brief introduction

The key types supported by Redis include the String character type, the map hash type, the list list type, the set collection type, and the sortedset ordered set type. This article will sum up the use of these key types, and introduce the Keys command, although the syntax is simple, but because of the excessive number, we also need more practice.

Two and String character types

1, assignment

Syntax: SET, key, value> set test 123 OK

2, value

Syntax: GET key> get test; 123"

3, take value and assign

Syntax: GETSET, key, value> GetSet S2 222; 111;> get S2; 222"

4 、 setting / getting multiple key values


  • MSET, key, value, [key, value… ]
  • MGET key [key… ]> Mset, K1, V1, K2, V2, K3, OK,>, get, K1, V1,>, mget, K1, K3, 1) V1, V3,) v3"

5, delete

Syntax: DEL key> del test (integer) 1

6, numerical increase or decrease

A. increment digit

When the stored string is an integer, redis provides a useful command, INCR, whose function is to increase the current key value and return the incremented value.

Syntax: INCR key> incr num (integer) 1; incr; num (integer) 2;> incr num (integer) 3;>

B. adds a specified integer

Syntax: INCRBY, key, increment> incrby num 2 (integer) 5> incrby num 2 (integer) 7;> incrby num 2 (integer) 9

C. decreasing value

Syntax: DECR key> decr num (integer) 9;> decr num (integer) 8

D. reduce specified integer

Syntax: DECRBY, key, decrement> decr num (integer) 6> decr num (integer) 5> decrby num 3 (integer) 2> decrby num 3 (integer) -1

7, append values to the tail

The role of append is to append value to the end of the key value. If the key does not exist, the value of the key is set to value, which is equivalent to set key value. The return value is the total length of the appended string.

Syntax: APPEND, key, value> set STR Hello OK> append str; "world"; (integer) 12;> get str; "Hello world"!"

8. Get string length

The STRLEN command returns the length of the key value and returns 0. if the key does not exist

Syntax: STRLEN key> strlen str (integer) 0;> set STR Hello OK> strlen str (integer) 5

Three, map hash type

In redis, map is also called hash. Suppose that User objects are stored in Redis in the form of JSON serialization, and that User objects have properties such as ID, username, password, age, name, and the following are stored procedures:

Save and update: User object –> JSON (string) –> redis.

What should I do if I just update the age property on the business and other properties are not updated? If you still use the above method, in the transmission, processing will cause waste of resources, the following talk about hash can be a good solution to this problem.

Redis hash introduction

Hash is called hash type, which provides the mapping of field and field values. Field values can only be string types, and
does not support hash types, collection types, and other types. As follows:

Redis series (two) - syntax and command

1, assignment

The HSET command does not distinguish between insert and temporary update operations. When the insert operation is performed, the HSET command returns 1 and returns 0 when the update operation is performed.

Set a field value at a time

Syntax: HSET, key, field, value> hset, user, username, zhangesan (integer) 1

Setting multiple field values at a time

Syntax: HMSET, key, field, value, [field, value…> hmset user age 20; username Lisi OK

When the field does not exist, assignment is similar to HSET. The difference is that the command does not perform any operations if the field exists

Syntax: HSETNX, key, field, value> hsetnx user age 30 (integer) 0

If there is no age field in user, the age value is set to 30, otherwise nothing will be done.

2, value

Gets a field value at a time

Syntax: HGET, key, field> hget, user, username, "zhangesan""

Obtain multiple field values at a time

Syntax: HMGET, key, filed[field…]> hmget, user, age, username, 1) 20, 2) lisi"

Get all field values

Syntax: HGETALL key> hgetall user 1) age, 2) 20, 3) username, 4) lisi"

3, delete fields

One or more fields can be deleted, and the returned value is the number of fields that have been deleted.

Syntax: HDEL, key, field, [field…> HDEL user age (integer) 1> HDEL user age name (integer) 0> HDEL user age username (integer) 1

4, increase the figures

Syntax: HINCRBY, key, field, increment> hincrby user age 2 adds the user's age by 2 (integer) 22> hget user age gets the user's age "22""

5, determine whether the field exists

Syntax: HEXISTS, key, field> hexists user age to see if there is a age field (integer) 1 user in the> hexists user name to see if there is a name field (integer) 0 in the user

6. Only field names or field values are obtained


  • HKEYS key
  • HVALS key> hmset, user, age, 20, name, OK,>, hkeys, user, 1), age, 2) name,>, hvals, user 1), 20, 2) Lisi lisi"

7, get the field number

Syntax: HLEN key> HLEN user (integer) 2

Four, list list type

The difference between Arraylist and LinkedList

  • ArrayList uses arrays to store data, characterized by fast queries, slow additions and deletions
  • LinkedList uses bidirectional linked lists to store data, characterized by quick additions and deletions and slow queries, but the data at both ends of the list is also fast.

Redis’s list uses linked lists to store, so the operation of the list data type of redis is to operate both ends of the list data to operate.

1, add elements to the ends of the list

Add elements to the left of the list

Syntax: LPUSH, key, value, [value…> lpush LIST1 123 (integer) 3

Add elements to the right side of the list> rpush LIST1 456 (integer) 3

2, view list

The LRANGE command is one of the most commonly used commands for list types. Getting a fragment from the list returns all elements between start and stop (including elements at both ends). The index starts at 0. The index can be negative, such as “-1”, representing the rightmost element.

Syntax: LRANGE, key, start, stop> 2; 3) 3,>, lrange, LIST1, 0-11), 1, 2) 2, 3) 3, 4) 4, 5) 5, 1, 6) 6 021 2"

3, pop-up elements from both ends of the list

The LPOP command pops an element from the left of the list and completes in two steps:
, the first step: the element on the left of the list to be removed from the list, and the second step of
: return the removed element value.


  • LPOP key
  • RPOP key> lpop LIST1; 1;> rpop LIST1; 6"

4. Get the number of elements in the list

Syntax: LLEN key> llen LIST1 (integer) 4

5, delete the value specified in the list

The LREM command removes elements from the first count in the list to value and returns the number of actually deleted elements. Depending on the count value, this command will behave differently.

  • When count&gt is 0, LREM starts deleting from the left of the list.
  • When count&lt is 0, LREM starts deleting from the right side of the list.
  • When count=0, LREM deletes all elements that are values of value.

Syntax: LREM, key, count, value> lrem LIST1 14 starts from the left and removes the element (integer) 4 with the value of 1

6, gets / sets the element value of the specified index

Gets the element value of the specified index

Syntax: LINDEX, key, index> lindex LIST1 2 "5""

Sets the element value of the specified index

Syntax: LSET, key, index, value> lset LIST1 24;> OK; lrange LIST1 0-11) 2, 2) 3, 3) 4"

7, keep only the list of specified fragments

The specified range is consistent with the LRANGE

Syntax: LTRIM, key, start, stop> lrange LIST1 0-11), 2, 2) 3, 3)> 4; ltrim LIST1 01; OK> lrange LIST1 0-11) 4"

8, insert elements into the list

The command first looks for elements of pivot in the list from left to right, and then determines whether to insert value into the front or back of the element, depending on whether the second parameter is BEFORE or AFTER.

Syntax: LINSERT, key, BEFORE|AFTER, pivot, value> lrange LIST1 0-11)> 4; linsert LIST1 after 45 (integer) 2;> lrange LIST1 0-11) 4, 2) 5"

9. Transfer elements from one list to another

Syntax: RPOPLPUSH, source, destination> rpoplpush, LIST1, List2, 5,> lrange, List2, 0-11) 5,> lrange LIST1 0-11) 4"

Five, set collection type

The difference between set (set) and list (list):

  • Collection type: unordered and non repeatable
  • List type: ordered and repeatable

1, add / delete elements

Syntax: SADD, key, member, [member…> Sadd, Set1, a, B, C (integer) 3,>, Sadd, Set1, a (integer) 0

Syntax: SREM, key, memeber, [member…> SREM, Set1, C, D, D, are not in the collection, so only C (integer) 1 is removed

2. Get all the elements in the collection

Syntax: SMEMBERS key> smemebers Set1 1) B, 1) a"

3, determine whether the elements are in the collection

Syntax: SISMEMBER, key, member> sismember, Set1, a (integer) 1,> sismember, Set1, H (integer) 0

4, arithmetic command

Differential set operation of A. set A – B

A collection of elements that belong to A and do not belong to B.

Redis series (two) - syntax and command

Syntax: SDIFF, key, [key…]> Sadd setA 123 (integer) 3> Sadd setB 234 (integer) 3> sdiff setA setB 1) 1> sdiff setB setA 1) 4"

B. set intersection operation A B

A collection of elements that belong to A and belong to B.

Redis series (two) - syntax and command

Syntax: SINTER, key, [key…]> sinter, setA, setB, 1) 2, 2) 3"

A B C., union operation set

A collection of elements belonging to A or B.

Redis series (two) - syntax and command

Syntax: SUNION, key, [key…]> Sunion, setB, setA 1) 1, 2) 2, 3) 3, 4) 4"

5. Get the number of elements in the collection

Syntax: SCARD key> smembers setA 1) 1, 2) 2, 3)> 3; scard setA (integer) 3

6. Eject an element from the collection

Note: since the collection is unordered, the SPOP command pops up randomly from the collection to select an element.

Syntax: SPOP key> spop setA; 1"

Six, sortedset ordered set type

Sortedset, also called Zset, is an ordered collection, sorted, but unique. The difference between Sortedset and set is that you add a fraction to the elements in the set, and then you sort them by this score.

1, add elements

Adds an element to the ordered set and a fraction of that element. If the element already exists, the new fraction is replaced with the new one. The return value is the number of elements added to the collection, excluding the elements already present.

Syntax: ZADD, key, score, member, [score, member…> zadd scoreboard 80; zhangesan 89; Lisi 94; Wangwu (integer) 3;> zadd scoreboard 97; Lisi (integer) 0

2. Get the score of the element

Syntax: ZSCORE, key, member> zscore scoreboard Lisi "97""

3, delete elements

Removes one or more members from an ordered set of key, and members that do not exist are ignored.
returns an error when key exists, but is not an ordered set type.

Syntax: ZREM, key, member, [member…> zrem scoreboard Lisi (integer) 1

4. Get a list of the elements in a range

A. returns all the elements from the start to the stop (both ends of the element) in the order in which the element scores vary from small to large

Syntax: ZRANGE, key, start, stop, [WITHSCORES]> zrange scoreboard 021) zhangesan, 2) Wangwu, 3) lisi"

B. returns all the elements from the start to the stop (both ends of the element) in the order of the element scores from large to small

Syntax: ZREVRANGE, key, start, stop, [WITHSCORES]> zrevrange scoredboard 021) Lisi, 2) Wangwu, 3) zhangesan"

If you need to get the element’s score, you can add the WITHSCORES parameter at the end of the command> zrange scoreboard 01; WITHSCORES 1) Zhangsan, 2) 80, 3) Wangwu, 4) 94"

5. Get the ranking of the elements

A. from small to large

Syntax: ZRANK, key, member> zrank scoreboard Lisi (integer) 0

B. from big to small

Syntax: ZREVRANK, key, member> zrevrank scoreboard Zhangsan (integer) 1

6. Obtain the elements of the specified fractional range

Syntax: ZRANGEBYSCORE, key, min, Max, [WITHSCORES][LIMIT, offset, count]> zrangebyscore scoreboard 9097, WITHSCORES 1) Wangwu, 2) 94, 3) Lisi 97,> zrangebyscore scoreboard 70100, limit 121) Wangwu, 2) lisi"

7, increase the score of an element

The return value is a changed fraction

Syntax: ZINCRBY, key, increment, member> zincrby scoreboard 4; Lisi "101""

8. Get the number of elements in the collection

Syntax: ZCARD key> zcard scoreboard (integer) 3

9, get the number of elements within the specified score range

Syntax: ZCOUNT, key, min, Max> zcount scoreboard 8090 (integer) 1

10, according to the rankings delete elements

Syntax: ZREMRANGEBYRANK, key, start, stop> zremrangebyrank scoreboard 01 (integer) 2;> zrange scoreboard 0-11) lisi"

11, delete elements according to the score range

Syntax: ZREMRANGEBYSCORE, key, min, Max> zadd scoreboard 84; Zhangsan (integer) 1;> zremrangebyscore scoreboard 80100 (integer) 1

Seven, Keys command

1, keys

Returns all key that satisfies a given pattern> mylist*, 1) mylist, 2) mylist5, 3) mylist6, 4) mylist7, 5) keys mylist8"

2, exists

Verify that an key has an
instance: from the results, there is no HongWan key in the database, but the age key exists> exists HongWan (integer) 0;> exists age (integer) 1

3, del

Delete a key> del age (integer) 1;> exists age (integer) 0

4, rename

Rename the key
example: change age to age_new.> keys * 1) age,>, rename, age, age_new, OK,>, keys * 1) age_new"

5, type

of the returned value: determine the type of value> type, addr, string,> type, myzset2, set,> type, mylist, list

6, set the key’s survival time

Redis is more used as caching in actual use, but cache data generally needs to set live time, that is, data destroyed after expiration.

EXPIRE key seconds key set the survival time (in seconds) key will automatically delete the TTL key set key PERSIST key to remove the remaining survival time, survival time PEXPIRE key milliseconds survival time setting unit in the number of seconds after: ms

Example:> set test 1 set the value of test is 1 OK> get test to obtain the test value of "1"> EXPIRE test 5 set the test survival time was 5 seconds (integer) 1> TTL test view test was born in the generation time and delete 1 (integer)> 1; TTL test (integer) -2> get test to obtain the value of test (NIL), has been deleted