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
(2) get [key]: Get the value corresponding to the key
(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
(4) strlen [key]: Get the length
(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
key2 does not exist, set successfully
(6) incr [key]: Increase the digital value stored in the key by 1, and can only operate on numbers
When the key does not exist, create a new one and set the value to 1
(7) decr [key]: Decrease the digital value stored in the key by 1
If the key does not exist, create a new one and set the value to -1
(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
(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
(3) lpop/rpop [key]: Pop up a value from the left/right, when all the values are popped up, the key is automatically deleted
(4) rpoplpush [key1] [key2]: Pop a value from the right of the key1 list and insert it to the left of the key2 list
(5) lindex [key] [index]: Obtain the specified element according to the table below the index (from left to right)
(6) linsert [key] before [value] [newvalue]: Insert [newvalue] before [value]
(7) lrem [key] [n] [value]: delete n values from the left
(8) lset [key] [index] [value]: Replace the value of the list key subscripted as index with value
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
(2) smembers [key]: View all values in the collection
(3) sismember [key] [value]: Determine whether the set key contains a given value, return 1 if yes, and return 0 if not
(4) scard [key]: returns the number of elements in the set
(5) srem [key] [value1] [value2]: delete an element in the collection
(6) spop [key]: Randomly pop a value from the collection
(7) srandmember [key] [n]: Randomly take n values from the set, and do not delete these values from the set
(8) move [source] [destination] [value]: move a value in the collection to another collection
(9) sinter [key1] [key2]: returns the intersection of two sets
(10) sunion [key1] [key2]: returns the union of two sets
(11) sdiff [key1] [key2]: Returns the difference set of two sets (in key1, not including key2)
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
(2) hget [key] [field]: get the value from the key1 collection filed
(3) hmset [key1] [field1] [value1] [field2] [value2]: set hash values in batches (the new version hset can also implement this function)
(4) hexists [key1] [field]: Check whether the given field field exists in the hash table key
(5) hkeys [key]: List all fields of the hash set
(6) hvals [key]: List all values of the hash set
(7) hincrby [key] [field] [increment]: add an increment to the value of the field field in the hash table key
(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
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
(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
(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
(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
(5) zincrby [key] [increment] [value]: add an increment to the score of the element
(6) zrem [key] [value]: Delete the element with the specified value
(7) zcount [key] [min] [max]: the number of elements in the statistical interval (closed interval)
(8) zrank [key] [value]: Returns the rank of the specified value in the collection, starting from 0