Five common data types in Redis and their related operation notes

Five common data types in Redis and their related operation notes

1 Redis key (key)

(1)Keys * View all keys in the current library

(2) exists [key] Determine whether the key exists, return 1 exists, return 0 does not exist

(3)type [key] Check what type the key is

(4)del [key] delete the specified key data

(5) Unlink [key] Select non-blocking deletion according to value: only keys are deleted from the keyspace metadata, and the real deletion will be performed asynchronously in the subsequent operation

(6)expire [key] [time]: Set the expiration time for the given key, in seconds

(7) ttl [key] Check how many seconds are left to expire, -1 means it will never expire, -2 means it has expired

2 String String

​ The String type of Redis is the most basic data type. A string value can be up to 512M. At the same time, String is binary safe and can contain any data, such as jpg pictures or serialized objects.

operate:

(1) set [key] [value]: set kv such as set k1 name

image-20211011144731620

(2) get [key]: Get the value corresponding to the key

image-20211011144813377

(3) append [key] [value]: append value to the value corresponding to the key, and return the length at the same time, if the key does not exist, create a new one

image-20211011144942354

(4) strlen [key]: Get the length

image-20211011145103816

(5) setnx [key] [value]: Only when the key does not exist, the value of the key is set

key1 exists, so it cannot be set successfully

image-20211011145412074

key2 does not exist, set successfully

image-20211011145433099

(6) incr [key]: Increase the digital value stored in the key by 1, and can only operate on numbers

image-20211011145645507

When the key does not exist, create a new one and set the value to 1

image-20211011145719568

(7) decr [key]: Decrease the digital value stored in the key by 1

image-20211011145743593

If the key does not exist, create a new one and set the value to -1

image-20211011145820299

(8) incrby [key] [step size]: Increase the digital value stored in the key, customize the step size, if the step size is 20, the value will increase by 20

(9) decrby [key] [step size]: Decrease the digital value stored in the key, customize the step size, if the step size is 20, the value will be reduced by 20

3 listList

​ List is a data type with single key and multiple values, sorted according to insertion order. An element can be added to the head or tail of the list. The underlying layer is a doubly linked list.

operate:

(1) lpush/rpush [key] [value1] [value2]...: Insert one or more values ​​from the left/right

image-20211011195152155

(2)Irange [key] [start] [stop]: Obtain elements according to the table below the index (from left to right). If the input start and stop are 0 and -1 respectively, it means to take all values

image-20211011195419315

(3) lpop/rpop [key]: Pop up a value from the left/right, when all the values ​​are popped up, the key is automatically deleted

image-20211011195517596

(4) rpoplpush [key1] [key2]: Pop a value from the right of the key1 list and insert it to the left of the key2 list

image-20211011195733955

(5) lindex [key] [index]: Obtain the specified element according to the table below the index (from left to right)

image-20211011195804261

(6) linsert [key] before [value] [newvalue]: Insert [newvalue] before [value]

image-20211011200008482

(7) lrem [key] [n] [value]: delete n values ​​​​from the left

image-20211011200033824

(8) lset [key] [index] [value]: Replace the value of the list key subscripted as index with value

image-20211011200105459

4 Redis collection Set

​ The set in Redis is also similar to the function of the list, but duplicate elements are not allowed in the set. In fact, the string type does not need a set, and the underlying layer is a hash table. The complexity of adding, deleting, and searching is O(1)

operate:

(1) sadd [key] [value1] [value2]: Add one or more elements to the key, automatically deduplicated

image-20211011200737962

(2) smembers [key]: View all values ​​in the collection

image-20211011200753596

(3) sismember [key] [value]: Determine whether the set key contains a given value, return 1 if yes, and return 0 if not

image-20211011200835616

(4) scard [key]: returns the number of elements in the set

image-20211011200859899

(5) srem [key] [value1] [value2]: delete an element in the collection

image-20211011200914762

(6) spop [key]: Randomly pop a value from the collection

image-20211011201015679

(7) srandmember [key] [n]: Randomly take n values ​​​​from the set, and do not delete these values ​​​​from the set

image-20211011201050295

(8) move [source] [destination] [value]: move a value in the collection to another collection

image-20211011201421546

(9) sinter [key1] [key2]: returns the intersection of two sets

image-20211011201558700

(10) sunion [key1] [key2]: returns the union of two sets

image-20211011201616779

(11) sdiff [key1] [key2]: Returns the difference set of two sets (in key1, not including key2)

image-20211011201632139

5 Redis hash Hash

​Hash in Redis is a mapping table of string type field and value, which is suitable for storing objects, similar to Map<String, Object> in java. When the underlying field-value length is short and the number is small, Will use ziplist (compressed list), otherwise use hashtable (hash table)

operate:

(1) hset [key] [field] [value]: assign value to the field key in the key set

image-20211011203250489

(2) hget [key] [field]: get the value from the key1 collection filed

image-20211011203318971

(3) hmset [key1] [field1] [value1] [field2] [value2]: set hash values ​​in batches (the new version hset can also implement this function)

image-20211011203401641

(4) hexists [key1] [field]: Check whether the given field field exists in the hash table key

image-20211011203453362

(5) hkeys [key]: List all fields of the hash set

image-20211011203514471

(6) hvals [key]: List all values ​​of the hash set

image-20211011203526640

(7) hincrby [key] [field] [increment]: add an increment to the value of the field field in the hash table key

image-20211011203705135

(8) hsetnx [key] [field] [value]: Set the value of the field field in the hash table key to value, and the operation can be successful only if the field field does not exist

image-20211011203811372

image-20211011204000283

6 Redis ordered collection Zset

​ Zset is also a set without repeated elements. The difference from set is that each member of Zset is associated with a score, and the members in the set are sorted from low to high according to the score, and the score can be repeated.
Its bottom layer uses two data structures:

​ (1) The hash table is used to associate value and score to ensure the uniqueness of elements

​ (2) Jump table, the function is to sort the value, and obtain the list of elements according to the value of the score
. Operation:

(1) zadd [key] [score1] [value1] [score2] [value2]: add one or more member elements and their values ​​to the key

image-20211011204731380

(2) zrange [key] [start] [end] [WITHSCORES]: Return the elements between start and end in the following table. If you bring WITHSCORES, you can return the score together. If start and end are 0 and -1 respectively , returns all elements

image-20211011204831112

image-20211011204852149

(3) zrangebyscore [key] [min] [max] [withscores] [limit offset count]: returns all members whose score value is between min and max (closed interval), sorted by score from small to large, limit offset count is pagination

image-20211011204952952

image-20211011205014351

(4) zrevrangebyscore [key] [max] [min] [withscores] [limit offset count]: Returns all members whose score value is between min and max (closed interval), arranged in descending order of score

image-20211011205143520

(5) zincrby [key] [increment] [value]: add an increment to the score of the element

image-20211011205432322

(6) zrem [key] [value]: Delete the element with the specified value

image-20211011205458082(7) zcount [key] [min] [max]: the number of elements in the statistical interval (closed interval)

image-20211011205522605

(8) zrank [key] [value]: Returns the rank of the specified value in the collection, starting from 0

image-20211011205604287

Guess you like

Origin blog.csdn.net/weixin_42195126/article/details/120711645