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

127.0.0.1:6379> set test 123 OK

2, value

Syntax: GET key

127.0.0.1:6379> get test; 123"

3, take value and assign

Syntax: GETSET, key, value

127.0.0.1:6379> GetSet S2 222; 111; 127.0.0.1:6379> get S2; 222"

4 、 setting / getting multiple key values

Syntax:

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

5, delete

Syntax: DEL key

127.0.0.1:6379> 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

127.0.0.1:6379> incr num (integer) 1; incr; num (integer) 2; 127.0.0.1:6379> incr num (integer) 3; 127.0.0.1:6379>

B. adds a specified integer

Syntax: INCRBY, key, increment

127.0.0.1:6379> incrby num 2 (integer) 5 127.0.0.1:6379> incrby num 2 (integer) 7; 127.0.0.1:6379> incrby num 2 (integer) 9

C. decreasing value

Syntax: DECR key

127.0.0.1:6379> decr num (integer) 9; 127.0.0.1:6379> decr num (integer) 8

D. reduce specified integer

Syntax: DECRBY, key, decrement

127.0.0.1:6379> decr num (integer) 6 127.0.0.1:6379> decr num (integer) 5 127.0.0.1:6379> decrby num 3 (integer) 2 127.0.0.1:6379> 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

127.0.0.1:6379> set STR Hello OK 127.0.0.1:6379> append str; "world"; (integer) 12; 127.0.0.1:6379> 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

127.0.0.1:6379> strlen str (integer) 0; 127.0.0.1:6379> set STR Hello OK 127.0.0.1:6379> 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

127.0.0.1:6379> hset, user, username, zhangesan (integer) 1

Setting multiple field values at a time

Syntax: HMSET, key, field, value, [field, value…

127.0.0.1:6379> 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

127.0.0.1:6379> 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

127.0.0.1:6379> hget, user, username, "zhangesan""

Obtain multiple field values at a time

Syntax: HMGET, key, filed[field…]

127.0.0.1:6379> hmget, user, age, username, 1) 20, 2) lisi"

Get all field values

Syntax: HGETALL key

127.0.0.1:6379> 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…

127.0.0.1:6379> HDEL user age (integer) 1 127.0.0.1:6379> HDEL user age name (integer) 0 127.0.0.1:6379> HDEL user age username (integer) 1

4, increase the figures

Syntax: HINCRBY, key, field, increment

127.0.0.1:6379> hincrby user age 2 adds the user's age by 2 (integer) 22 127.0.0.1:6379> hget user age gets the user's age "22""

5, determine whether the field exists

Syntax: HEXISTS, key, field

127.0.0.1:6379> hexists user age to see if there is a age field (integer) 1 user in the 127.0.0.1:6379> 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

Syntax:

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

7, get the field number

Syntax: HLEN key

127.0.0.1:6379> 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…

127.0.0.1:6379> lpush LIST1 123 (integer) 3

Add elements to the right side of the list

127.0.0.1:6379> 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

127.0.0.1:6379> 2; 3) 3, 127.0.0.1:6379>, 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.

Syntax:

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

4. Get the number of elements in the list

Syntax: LLEN key

127.0.0.1:6379> 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

127.0.0.1:6379> 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

127.0.0.1:6379> lindex LIST1 2 "5""

Sets the element value of the specified index

Syntax: LSET, key, index, value

127.0.0.1:6379> lset LIST1 24; 127.0.0.1:6379> 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

127.0.0.1:6379> lrange LIST1 0-11), 2, 2) 3, 3) 127.0.0.1:6379> 4; ltrim LIST1 01; OK 127.0.0.1:6379> 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

127.0.0.1:6379> lrange LIST1 0-11) 127.0.0.1:6379> 4; linsert LIST1 after 45 (integer) 2; 127.0.0.1:6379> lrange LIST1 0-11) 4, 2) 5"

9. Transfer elements from one list to another

Syntax: RPOPLPUSH, source, destination

127.0.0.1:6379> rpoplpush, LIST1, List2, 5, 127.0.0.1:6379> lrange, List2, 0-11) 5, 127.0.0.1:6379> 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…

127.0.0.1:6379> Sadd, Set1, a, B, C (integer) 3, 127.0.0.1:6379>, Sadd, Set1, a (integer) 0

Syntax: SREM, key, memeber, [member…

127.0.0.1:6379> 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

127.0.0.1:6379> smemebers Set1 1) B, 1) a"

3, determine whether the elements are in the collection

Syntax: SISMEMBER, key, member

127.0.0.1:6379> sismember, Set1, a (integer) 1, 127.0.0.1:6379> 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…]

127.0.0.1:6379> Sadd setA 123 (integer) 3 127.0.0.1:6379> Sadd setB 234 (integer) 3 127.0.0.1:6379> sdiff setA setB 1) 1 127.0.0.1:6379> 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…]

127.0.0.1:6379> 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…]

127.0.0.1:6379> Sunion, setB, setA 1) 1, 2) 2, 3) 3, 4) 4"

5. Get the number of elements in the collection

Syntax: SCARD key

127.0.0.1:6379> smembers setA 1) 1, 2) 2, 3) 127.0.0.1:6379> 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

127.0.0.1:6379> 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…

127.0.0.1:6379> zadd scoreboard 80; zhangesan 89; Lisi 94; Wangwu (integer) 3; 127.0.0.1:6379> zadd scoreboard 97; Lisi (integer) 0

2. Get the score of the element

Syntax: ZSCORE, key, member

127.0.0.1:6379> 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…

127.0.0.1:6379> 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]

127.0.0.1:6379> 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]

127.0.0.1:6379> 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

127.0.0.1:6379> 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

127.0.0.1:6379> zrank scoreboard Lisi (integer) 0

B. from big to small

Syntax: ZREVRANK, key, member

127.0.0.1:6379> zrevrank scoreboard Zhangsan (integer) 1

6. Obtain the elements of the specified fractional range

Syntax: ZRANGEBYSCORE, key, min, Max, [WITHSCORES][LIMIT, offset, count]

127.0.0.1:6379> zrangebyscore scoreboard 9097, WITHSCORES 1) Wangwu, 2) 94, 3) Lisi 97, 127.0.0.1:6379> 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

127.0.0.1:6379> zincrby scoreboard 4; Lisi "101""

8. Get the number of elements in the collection

Syntax: ZCARD key

127.0.0.1:6379> zcard scoreboard (integer) 3

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

Syntax: ZCOUNT, key, min, Max

127.0.0.1:6379> zcount scoreboard 8090 (integer) 1

10, according to the rankings delete elements

Syntax: ZREMRANGEBYRANK, key, start, stop

127.0.0.1:6379> zremrangebyrank scoreboard 01 (integer) 2; 127.0.0.1:6379> zrange scoreboard 0-11) lisi"

11, delete elements according to the score range

Syntax: ZREMRANGEBYSCORE, key, min, Max

127.0.0.1:6379> zadd scoreboard 84; Zhangsan (integer) 1; 127.0.0.1:6379> zremrangebyscore scoreboard 80100 (integer) 1

Seven, Keys command

1, keys

Returns all key that satisfies a given pattern

127.0.0.1:6379> 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

127.0.0.1:6379> exists HongWan (integer) 0; 127.0.0.1:6379> exists age (integer) 1

3, del

Delete a key

127.0.0.1:6379> del age (integer) 1; 127.0.0.1:6379> exists age (integer) 0

4, rename

Rename the key
example: change age to age_new.

127.0.0.1:6379> keys * 1) age, 127.0.0.1:6379>, rename, age, age_new, OK, 127.0.0.1:6379>, keys * 1) age_new"

5, type

Type
of the returned value: determine the type of value

127.0.0.1:6379> type, addr, string, 127.0.0.1:6379> type, myzset2, set, 127.0.0.1:6379> 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:

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