5 data structures and operation commands of redis


The program is used to store data, the database is used to store data, the data processed by the program should be stored in the database, and the data with different characteristics should be stored in different data structures in redis.

1: String: single key: single value

Operation command:
1): set string type data to redis: set key value (if the key already exists, the previous key will be overwritten)
set zsname zhangsan
2) get key
get zsname
3) append string: append key value : If the key does not exist, create a new key and set the value to value
set phone 11111
4) Get the string length: strlen key
5) Add one to the string value: incr key (if the key does not exist, first set a key , Initialized to 0, and then add one operation) The value represented by the key must be a numeric value, otherwise an error will be reported.
Insert picture description here
6) Subtract one from the string value: decr key
Insert picture description here
7) Add one offset to the string value: incrby key offset
incrby zsage 10
Insert picture description here
8) Subtract one offset from the string value: decrby key offset
Insert picture description here
9) Get characters The substring in the string getrange key startIndex endIndex
getrange zsname 2 6
Each subscript in the string can also be a negative number, from right to left, starting from -1

Insert picture description here
Insert picture description here
10) The value overlay subscript is the string starting with startIndex setrange key startIndex value (overwrite several characters as long as several characters can be covered)
Insert picture description here
11) Set the string data while setting its maximum life cycle: setex key seconds value
Insert picture description here
12) setnx key value: Set the value of string type data to the redis database. When the key does not exist, the setting is successful, otherwise the setting is abandoned.
13) Set string type data to redis in batches: mset key 1 value 1 key 2 value 2
14) Batch from redis Get string type data in: mget key 1 key 2 key 3...
15) msetnx batch set string to redis, when the key does not exist, the setting is successful, as long as one already exists, all set
msetnx key 1 key 2...

2:list list: single key: multi-order value: this order refers to the order in which it is placed in what order it is, and the order can be repeated

1) Insert one or more values ​​into the list header (left side) at once: lpush key value[value value…]
lpush list01 1 2 3
Insert picture description here

2) Get the elements of the specified subscript interval in the specified list: lrange key startIndex endIndex
3) Insert one or more values ​​at the end of the list at once: rpush key value[value,value...]
rpush list02 abc
rpush list02 de
Insert picture description here
4 ) Remove and return the header element from the specified list: lpop key
lpop list02
Insert picture description here
5) Get the element with the specified subscript in the list: lindex key index
Insert picture description here
6) Get the length of the specified list: llen key
llen list02
Insert picture description here
7) Remove from the specified list And return to the end of the table element: rpop key
Insert picture description here
8) Remove some data from the list according to the value of count: lrem key count value
a. If count>0, delete count data equal to calue from the left
. b. If count<0 from Delete count data equal to calue on the right side of the list
. c. If count=0, remove all data equal to value from the list.
Insert picture description here
9) Intercept the elements of the specified subscript interval in the specified list to form a new list and assign it to key:ltrim key startIndex endIndex
Insert picture description here
10) Set the element of the specified subscript in the specified list to the specified value
lset list04 1 10
11) Insert the value into the specified list at the position before/after the pivot element: linsert key before/after pivot valueInsert picture description here

3: set collection: single key: multiple unordered values, cannot be repeated, directly operated by business data

 1)将一个或者多个元素(如果元素已经存在则会忽略)添加到集合中,返回成功加入的元素个数:sadd ey value [value value....]
 sadd set01 a b a  c
 2)获取指定集合中所有的元素:smember key

Insert picture description here
3) Determine whether the specified element exists in the set:
return 1 if sismember key member exists, return 0 if it does not exist

Insert picture description here
4) Get the length of the specified set: scar key
Insert picture description here
5) Remove one or more elements in the specified set: srem key member [member, member...]
Non-existent elements will be ignored, and the number of successfully removed will be returned.
6) Random Get one or more elements in the specified set: srandmember key [count]
count>0: multiple elements obtained randomly cannot be repeated
count<0: multiple elements obtained randomly may be repeated
Insert picture description here
7) From the specified set Randomly remove one or more elements
: spop key[count]
Insert picture description here
8) Move the specified element in the specified set to another element: move source dest member
Insert picture description here
9) Get the elements in one set but not in other sets: sdiff key key...
10) Get a new set composed of
all elements in the specified set : sinter 11) Get a large set composed of all elements in all the specified set: sunion key key...

4: pojo (entity class) single key: object (attribute: value)

1) Set one or more field-value pairs to the hash table: hset key field value,
2) Get the value of the specified field in the hash table: hget key field
Insert picture description here
3) Set multiple field-value pairs to In the hash table: hmset key1 value1…
4) Get the value of the field in the specified hash table in batch: hmget key field[field2…]
5) Get all the fields and values ​​in the specified hash table: gettall key
Insert picture description here
6) From the specified Delete one or more fields from the hash table: hdel key field1 field2...
7) Get the number of all fields in the specified hash table: hlen key
Insert picture description here
8) Determine whether there is a field: hexists key field in the specified hash table
9 Get all the field lists in the specified hash table: hkeys key
10) Get all the value lists in the specified hash table: hvals key
11) Add the field values ​​in the specified hash table: hincrby key field int
Insert picture description here
12) Pair Specify the field value in the specified hash table to perform floating point addition operation: hincrbyfloat key field float
13) Set a field-value pair to the hash table. If the key-value already exists, discard the setting, hsetnx key fiels value
Insert picture description here

5: zset: single key: multiple order value: his order is not what you put in it or what there is a certain rule, it cannot be repeated.

Each element of zset is associated with a score (the score can be repeated), and redis sorts the members in the set from small to large through the score.
1) Add one or more members and their score values ​​to the ordered set: zadd key score memeber[score member...]
If the element already exists, the score will be overwritten
2) Get the element in the specified index interval in the specified ordered set: zrange key startIndex endIndex [withscores]
Insert picture description here
3) Get the element in the specified score interval in the specified ordered set: zrangebyscore key min max
Insert picture description here
4) Delete one or more elements in the ordered set: zrem key member[member...]
Insert picture description here
5) Get the number of all elements in the specified ordered set: zcard key
6) Get the ranking of the specified element in the specified set :zrank key member
7) Get the number of elements in the specified set whose score is in the specified interval: zcount key min max
8) Get the score of the specified element in the specified set: zscore key member
9) Get the rank of the specified element in the specified set: zrevrank key member (from large to small row)
10)

6: The key operation instruction starts from 0:

1: Check the key:keys pattern
* in the database : match 0 or more characters
? : Match a character
[] : Match a character in [], there is a same character in it to match
keys : View all
keys in the
database keys k : View all
Insert picture description here
keys in the database beginning with k 2: Determine that the key is in the database If exists key, return 1 if it exists, return 0 if it does not exist
Insert picture description here
exists key1 key2... Determine whether multiple keys exist
Insert picture description here
3: Move the specified key to the specified database instance: move key index,
Insert picture description here
4: View the remaining time of the specified key :Ttl key
if the survival time is not set, return -1
if the key does not exist, return -2
Insert picture description here
5: set the maximum key generation time: expire key seconds
Insert picture description here
6: view the data type of the specified key: type
Insert picture description here
7: rename key: rename key newkey
Insert picture description here

8: Delete key: Del key... delete the specified key, the return value is the number of keys deleted in the century
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42678668/article/details/107934552
Recommended