Five common data types
General Command
Switch libraries, 16 libraries in total, starting from 0
127.0.0.1:6379> select 15 OK
View all key s in the current library
127.0.0.1:6379[15]> keys * 1) "k1"
Judge whether a key exists (no k2)
127.0.0.1:6379[15]> exists k1 (integer) 1 127.0.0.1:6379[15]> exists k2 (integer) 0
Check the type of your key
127.0.0.1:6379[15]> type k1 string
Delete the specified key data
127.0.0.1:6379[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
127.0.0.1:6379[15]> unlink k1 (integer) 1
Set the expiration time for the given key in seconds
127.0.0.1:6379[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
127.0.0.1:6379[15]> ttl k1 (integer) 3593
View the number of key s in the current database
127.0.0.1:6379> dbsize (integer) 1
Empty current library
127.0.0.1:6379> flushdb OK
Kill all libraries
127.0.0.1:6379> flushall OK
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
127.0.0.1:6379> set k1 t100 OK
Query corresponding key value
127.0.0.1:6379> get k1 "t100"
Gets the length of the value
127.0.0.1:6379> strlen k1 (integer) 4
Appends the given to the end of the original value
127.0.0.1:6379> append k1 t200 (integer) 8 127.0.0.1:6379> get k1 "t100t200"
Set the value of the key only when the key does not exist
127.0.0.1:6379> setnx k1 t300 (integer) 0 127.0.0.1:6379> get k1 "t100t200"
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
127.0.0.1:6379> set k2 200 OK 127.0.0.1:6379> incr k2 (integer) 201 127.0.0.1:6379> decr k2 (integer) 200
Custom step increase / decrease
127.0.0.1:6379> incrby k2 300 (integer) 500 127.0.0.1:6379> decrby k2 600 (integer) -100
Set one or more key value pairs at the same time
127.0.0.1:6379> mset k1 t100 k2 t200 OK
Get one or more value s at the same time
127.0.0.1:6379> 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
127.0.0.1:6379> msetnx k1 t300 k2 t400 (integer) 0 127.0.0.1:6379> mget k1 k2 1) "t100" 2) "t200"
Gets the range of values, starting at 0
127.0.0.1:6379> set k1 wuqian OK 127.0.0.1:6379> getrange k1 2 5 "qian"
Override value
127.0.0.1:6379> setrange k1 2 abc (integer) 6 127.0.0.1:6379> get k1 "wuabcn"
Set the expiration time in seconds while setting the key value
127.0.0.1:6379> setex k1 3600 wuqian OK 127.0.0.1:6379> ttl k1 (integer) 3547
The new value is set and the old value is obtained
127.0.0.1:6379> getset k1 wuqianqian "wuqian" 127.0.0.1:6379> get k1 "wuqianqian"
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)
127.0.0.1:6379[1]> lpush k1 v1 v2 v3 (integer) 3 127.0.0.1:6379[1]> rpush k2 v1 v2 v3 (integer) 3 127.0.0.1:6379[1]> lrange k1 0 -1 1) "v3" 2) "v2" 3) "v1" 127.0.0.1:6379[1]> lrange k2 0 -1 1) "v1" 2) "v2" 3) "v3"
Get list length
127.0.0.1:6379[1]> llen k1 (integer) 3
Get elements by index subscript (left to right)
127.0.0.1:6379[1]> lindex k1 1 "v2"
lpop/rpop spits out a value from the left / right. The value is in the key, and the light key dies.
127.0.0.1:6379[1]> lpop k1 "v3" 127.0.0.1:6379[1]> rpop k1 "v1" 127.0.0.1:6379[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.
127.0.0.1:6379[1]> rpoplpush k2 k1 "v3" 127.0.0.1:6379[1]> lrange k1 0 -1 1) "v3" 2) "v2" 127.0.0.1:6379[1]> lrange k2 0 -1 1) "v1" 2) "v2"
Before: inserts v0 before the specified value v1
After: insert after
127.0.0.1:6379[1]> linsert k2 before v1 v0
(integer) 3
127.0.0.1:6379[1]> lrange k2 0 -1
- "v0"
- "v1"
- "v2"
Delete 2 v0s from the left (left to right)
127.0.0.1:6379[1]> lrem k2 2 v0 (integer) 1 127.0.0.1:6379[1]> lrange k2 0 -1 1) "v1" 2) "v2"
Modify the data of the specified subscript
127.0.0.1:6379[1]> lset k2 0 v100 OK 127.0.0.1:6379[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
127.0.0.1:6379[1]> sadd k1 v1 v2 v3 v4 v5 (integer) 5
Fetch all values of the set
127.0.0.1:6379[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
127.0.0.1:6379[1]> sismember k1 v1 (integer) 1 127.0.0.1:6379[1]> sismember k1 v11 (integer) 0
Returns the number of elements in the collection.
127.0.0.1:6379[1]> scard k1 (integer) 5
Delete one / more elements in the collection
127.0.0.1:6379[1]> srem k1 v1 v2 (integer) 2
Spit out a value randomly from the set and delete it
127.0.0.1:6379[1]> spop k1 "v3" 127.0.0.1:6379[1]> smembers k1 1) "v5" 2) "v4"
Randomly take n values from the set. Will not be deleted from the collection
127.0.0.1:6379[1]> srandmember k1 2 1) "v5" 2) "v4" 127.0.0.1:6379[1]> smembers k1 1) "v5" 2) "v4"
Moves a value in a set from one set to another
127.0.0.1:6379[1]> sadd k2 v6 v7 (integer) 2 127.0.0.1:6379[1]> smove k1 k2 v4 (integer) 1 127.0.0.1:6379[1]> smembers k1 1) "v5" 127.0.0.1:6379[1]> smembers k2 1) "v7" 2) "v4" 3) "v6"
sinter intersection
sunion Union
sdiff difference set (in key1, excluding in key2)
127.0.0.1:6379[1]> sadd k1 v1 v2 v3 (integer) 3 127.0.0.1:6379[1]> sadd k2 v2 v3 v4 (integer) 3 127.0.0.1:6379[1]> sinter k1 k2 1) "v3" 2) "v2" 127.0.0.1:6379[1]> sunion k1 k2 1) "v4" 2) "v2" 3) "v3" 4) "v1" 127.0.0.1:6379[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
hset
Assign a value to the key in the collection
127.0.0.1:6379[1]> hset user:1001 id 1 name wuqian age 38 (integer) 3
Fetch value from collection
127.0.0.1:6379[1]> hget user:1001 name "wuqian"
Check whether the given field exists in the hash table key
127.0.0.1:6379[1]> hexists user:1001 prov_desc (integer) 0 127.0.0.1:6379[1]> hexists user:1001 id (integer) 1
Lists all field s of the hash set
127.0.0.1:6379[1]> hkeys user:1001 1) "id" 2) "name" 3) "age"
List all value s of the hash set
127.0.0.1:6379[1]> hvals user:1001 1) "1" 2) "wuqian" 3) "38"
Increment the value of the field in the hash table key
127.0.0.1:6379[1]> hincrby user:1001 age 10 (integer) 48
hsetnx
Set the value of the field in the hash table key to value if and only if the field does not exist
127.0.0.1:6379[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
127.0.0.1:6379> 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
127.0.0.1:6379> zrange topn 0 -1 1) "java" 2) "python" 3) "c++" 127.0.0.1:6379> 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)
127.0.0.1:6379> zrangebyscore topn 300 500 1) "python" 2) "c++" 127.0.0.1:6379> zrangebyscore topn 300 500 withscores 1) "python" 2) "300" 3) "c++" 4) "400"
Ditto, change from large to small
127.0.0.1:6379> zrevrangebyscore topn 500 300 1) "c++" 2) "python" 127.0.0.1:6379> zrevrangebyscore topn 500 300 withscores 1) "c++" 2) "400" 3) "python" 4) "300"
Increment the score of the element
127.0.0.1:6379> zincrby topn 50 java "250"
Delete the element with the specified value under the collection
127.0.0.1:6379> zrem topn c++ (integer) 1 127.0.0.1:6379> zrange topn 0 -1 1) "java" 2) "python"
Count the number of elements in the set and score interval
127.0.0.1:6379> zcount topn 1 300 (integer) 2
Returns the ranking of the value in the collection, starting from 0
127.0.0.1:6379> zrank topn python (integer) 1