redis data type

Posted by mwalsh on Mon, 10 Jan 2022 09:50:44 +0100

Five common data types

General Command

Switch libraries, 16 libraries in total, starting from 0> select 15

View all key s in the current library[15]> keys *
1) "k1"

Judge whether a key exists (no k2)[15]> exists k1
(integer) 1[15]> exists k2
(integer) 0

Check the type of your key[15]> type k1

Delete the specified key data[15]> del k1
(integer) 1

Select non blocking deletion according to value
/Only the keys are deleted from the keyspace metadata. The real deletion will occur in subsequent asynchronous operations[15]> unlink k1
(integer) 1

Set the expiration time for the given key in seconds[15]> expire k1 3600
(integer) 1

Check how many seconds are left to expire, - 1 means it will never expire, - 2 means it has expired[15]> ttl k1
(integer) 3593

View the number of key s in the current database> dbsize
(integer) 1

Empty current library> flushdb

Kill all libraries> flushall

Redis string (String)

Memcached as like as two peas, one key corresponds to one value.
The string type is binary safe. This means that the Redis string can contain any data. For example, jpg images or serialized objects.
String type is the most basic data type of Redis. The string value in a Redis can be 512M at most

set add key value pair> set k1 t100

Query corresponding key value> get k1

Gets the length of the value> strlen k1
(integer) 4

Appends the given to the end of the original value> append k1 t200
(integer) 8> get k1

Set the value of the key only when the key does not exist> setnx k1 t300
(integer) 0> get k1

Digital values increase by 1 and decrease by 1
Can only operate on numeric values,
If it is blank, the new increment of incr is 1 and the new increment of decr is - 1> set k2 200
OK> incr k2
(integer) 201> decr k2
(integer) 200

Custom step increase / decrease> incrby k2 300
(integer) 500> decrby k2 600
(integer) -100

Set one or more key value pairs at the same time> mset k1 t100 k2 t200

Get one or more value s at the same time> mget k1 k2
1) "t100"
2) "t200"

Set one or more key value pairs at the same time. The key must not exist
Atomicity, if one fails, all fail> msetnx k1 t300 k2 t400
(integer) 0> mget k1 k2
1) "t100"
2) "t200"

Gets the range of values, starting at 0> set k1 wuqian
OK> getrange k1 2 5

Override value> setrange k1 2 abc
(integer) 6> get k1

Set the expiration time in seconds while setting the key value> setex k1 3600 wuqian
OK> ttl k1
(integer) 3547

The new value is set and the old value is obtained> getset k1 wuqianqian
"wuqian"> get k1

The data structure of String is a dynamic String,
The internal space actually allocated for the current string is generally higher than the actual string len, which is generally more than 1.5 times
When the string length is less than 1M, the expansion is to double the existing space
If it exceeds 1M, only 1M more space will be expanded during capacity expansion
Note that the maximum length of the string is 512M

Redis list

lpush/rpush inserts one or more values from the left / right
lrange gets elements by index subscript (left to right)
0 is the first on the left and - 1 is the first on the right, (0 - 1 means to get all)[1]> lpush k1 v1 v2 v3
(integer) 3[1]> rpush k2 v1 v2 v3
(integer) 3[1]> lrange k1 0 -1
1) "v3"
2) "v2"
3) "v1"[1]> lrange k2 0 -1
1) "v1"
2) "v2"
3) "v3"

Get list length[1]> llen k1
(integer) 3

Get elements by index subscript (left to right)[1]> lindex k1 1

lpop/rpop spits out a value from the left / right. The value is in the key, and the light key dies.[1]> lpop k1
"v3"[1]> rpop k1
"v1"[1]> lrange k1 0 -1
1) "v2"

Rpop lpush spits out a value from the right side of the list and inserts it to the left side of the list.[1]> rpoplpush k2 k1
"v3"[1]> lrange k1 0 -1
1) "v3"
2) "v2"[1]> lrange k2 0 -1
1) "v1"
2) "v2"

Before: inserts v0 before the specified value v1
After: insert after[1]> linsert k2 before v1 v0
(integer) 3[1]> lrange k2 0 -1

  1. "v0"
  2. "v1"
  3. "v2"

Delete 2 v0s from the left (left to right)[1]> lrem k2 2 v0
(integer) 1[1]> lrange k2 0 -1
1) "v1"
2) "v2"

Modify the data of the specified subscript[1]> lset k2 0 v100
OK[1]> lrange k2 0 -1
1) "v100"
2) "v2"

The data structure of List is quickList.

First, when there are few list elements, a continuous memory storage will be used. This structure is ziplost, that is, compressed list.
It stores all the elements next to each other and allocates a continuous piece of memory.

When there is a large amount of data, it will be changed to quicklist.
Because the additional pointer space required by ordinary linked lists is too large, it will waste space. For example, only int type data is stored in this list, and two additional pointers prev and next are required in the structure.

Redis combines the linked list and zipplist to form a quicklist. That is to string multiple ziplist s using bidirectional pointers. This not only meets the fast insertion and deletion performance, but also does not appear too much spatial redundancy.

Redis set

Redis set's external functions are similar to those of list, which is a list function. The special feature is that set can automatically eliminate duplication

Add one or more member elements to the set key, and the existing member elements will be ignored[1]> sadd k1 v1 v2 v3 v4 v5
(integer) 5

Fetch all values of the set[1]> smembers k1
1) "v1"
2) "v4"
3) "v3"
4) "v2"
5) "v5"

Judge whether the set contains the value, with 1 and no 0[1]> sismember k1 v1
(integer) 1[1]> sismember k1 v11
(integer) 0

Returns the number of elements in the collection.[1]> scard k1
(integer) 5

Delete one / more elements in the collection[1]> srem k1 v1 v2
(integer) 2

Spit out a value randomly from the set and delete it[1]> spop k1
"v3"[1]> smembers k1
1) "v5"
2) "v4"

Randomly take n values from the set. Will not be deleted from the collection[1]> srandmember k1 2
1) "v5"
2) "v4"[1]> smembers k1
1) "v5"
2) "v4"

Moves a value in a set from one set to another[1]> sadd k2 v6 v7
(integer) 2[1]> smove k1 k2 v4
(integer) 1[1]> smembers k1
1) "v5"[1]> smembers k2
1) "v7"
2) "v4"
3) "v6"

sinter intersection
sunion Union
sdiff difference set (in key1, excluding in key2)[1]> sadd k1 v1 v2 v3
(integer) 3[1]> sadd k2 v2 v3 v4
(integer) 3[1]> sinter k1 k2
1) "v3"
2) "v2"[1]> sunion k1 k2
1) "v4"
2) "v2"
3) "v3"
4) "v1"[1]> sdiff k1 k2
1) "v1"

Redis hash (Hash)

Redis hash is a collection of key value pairs.
Redis hash is a mapping table of field and value of string type. Hash is especially suitable for storing objects.

The user ID is the searched key and the stored value. The user object contains name, age, birthday and other information. If it is stored in an ordinary key/value structure

Assign a value to the key in the collection[1]> hset user:1001 id 1 name wuqian age 38
(integer) 3

Fetch value from collection[1]> hget user:1001 name

Check whether the given field exists in the hash table key[1]> hexists user:1001 prov_desc
(integer) 0[1]> hexists user:1001 id
(integer) 1

Lists all field s of the hash set[1]> hkeys user:1001
1) "id"
2) "name"
3) "age"

List all value s of the hash set[1]> hvals user:1001
1) "1"
2) "wuqian"
3) "38"

Increment the value of the field in the hash table key[1]> hincrby user:1001 age 10
(integer) 48

Set the value of the field in the hash table key to value if and only if the field does not exist[1]> hsetnx user:1001 prov_desc bj
(integer) 1

Redis ordered set Zset(sorted set)

Redis ordered set zset is very similar to ordinary set. It is a string set without duplicate elements.
The difference is that each member of the ordered set is associated with a score, which is used to sort the members of the set from the lowest score to the highest score. The members of the collection are unique, but the scores can be repeated.
Because the elements are ordered, you can also quickly get a range of elements according to score or position.
Accessing the intermediate elements of an ordered set is also very fast, so you can use an ordered set as a smart list without duplicate members.

establish> zadd topn 200 java 300 python 400 c++
(integer) 3

Returns the elements in the ordered set key with subscripts between
With with scores, you can return scores and values to the result set> zrange topn 0 -1
1) "java"
2) "python"
3) "c++"> zrange topn 0 -1 withscores
1) "java"
2) "200"
3) "python"
4) "300"
5) "c++"
6) "400"

Returns all members in the ordered set key whose score value is between min and max (including those equal to min or max).
Ordered set members are arranged in the order of increasing score value (from small to large)> zrangebyscore topn 300 500
1) "python"
2) "c++"> zrangebyscore topn 300 500 withscores
1) "python"
2) "300"
3) "c++"
4) "400"

Ditto, change from large to small> zrevrangebyscore topn 500 300
1) "c++"
2) "python"> zrevrangebyscore topn 500 300 withscores
1) "c++"
2) "400"
3) "python"
4) "300"

Increment the score of the element> zincrby topn 50 java

Delete the element with the specified value under the collection> zrem topn c++
(integer) 1> zrange topn 0 -1
1) "java"
2) "python"

Count the number of elements in the set and score interval> zcount topn 1 300
(integer) 2

Returns the ranking of the value in the collection, starting from 0> zrank topn python
(integer) 1

Topics: Redis