Introduction to redis data types
You probably already know that Redis is not a simple key-value store. It is actually a data structure server that supports different types of values. That is, you don’t have to think of the string as just the value that the key points to. The following data types can be used as value types:
- Strings: Binary safe Strings, meaning redis Strings can contain any data. Like JPG images or serialized objects. A key can store up to 512MB. Binary security means that the information security of binary data can be guaranteed during data transmission, that is, it will not be tampered with or deciphered. If an attack occurs, it can be detected in time.
- Lists: A collection of string elements sorted in insertion order. They are basically linked lists. You can add an element to the head of the list (left) or add an element to the tail (right).
- Sets: A collection of unduplicated and unordered string elements.
- Sorted sets: A collection of non-repeating and ordered string elements. Similar to Sets, but each string element is associated with a floating number value called score. The elements are always sorted by score, so the difference is that it’s a set of elements that can be retrieved. Sorted sets have unique elements, but score is repeatable.
- Hashes: A map consisting of fields and associated values, both of which are strings. Is a collection of key-value pairs, especially suitable for storing objects.
Redis keys are binary safe, which means you can use any binary sequence as a key, from a simple string of the form “foo” to the contents of a JPEG file. An empty string is also a valid key.
Here are a few rules about keys:Copy the code
- The key should not be too long. A 1024 byte key, for example, is not a good idea, not only because of the memory consumption, but also because of the computational cost of finding such keys in the data.
- The key should not be too short. For example, if you want to use “u:1000: PWD” instead of “user:1000:password”, this is fine, but the latter is easier to read and the resulting space consumption is small compared to key objects and value objects themselves. Of course, no one is stopping you from having to save a little space with shorter keys.
- Use a common naming convention. For example, “object-type:username:field” is a good idea.
- Key is case sensitive. For example, “user:password” and “user:password” are two different keys.
Redis data type common operation command
Note: in Redis, 0 means no and 1 means yes in the result returned by executing the command.
-
Strings common command:
- setKey value: Sets the value of a specified key. Example:
set key1 value1
- setexKey seconds value: Sets the value of the specified key and the expiration time of the key, in seconds (set with expire) example:
setex name 5 zhagnsan
- setnxKey value: Sets the key value if the key does not exist. (Set if not exists), a distributed lock problem, example:
setnx key2 helloworld
- appendKey value: appends a specified key value. For example:
append key1 hello
- getKey: obtains the specified key value. For example:
get key1
- getrangeKey start end: obtains the value of the specified range in the key. If end is -1, it represents the last string from the start position to the value. For example:
getrange key1 0 3
- setrangeKey offset value: replaces the offset specified in the key. For example:
setrange key1 5 kitty
- strlenKey: Returns the length of the string value stored by key. Example:
strlen key1
- delKey: deletes the specified key. For example:
del key1
- incrKey: Increments the value by 1 if the value specified by key is of type INTEGER. Example:
incr key1
- decrKey: If the value specified by key is of type INTEGER, the value is reduced by 1.
decr key1
- incrbyKey number: If the value specified by key is an integer, the value is incremented by an integer. For example:
incrby key1 5
- decrbyKey number: If the value specified by key is an integer, the value is subtracted from the value. For example:
incrby key1 5
- msetkey value [key value …] : Sets multiple keys and values in batches. For example:
mset a1 b1 a2 b2
- mgetkey value [key value …] Obtain the values of multiple keys in a batch. For example:
mget a1 a2
- getsetKey value: get and then set. Example:
getset name lisi
- setKey value: Sets the value of a specified key. Example:
-
Lists common commands:
- lpushkey element [element …] : Sets multiple elements in the header (left) of the key specified in the list. Example:
lpush numbers 1 2 3 4 5
- rpushkey element [element …] : Sets multiple elements at the end (right) of the key specified in the list. Example:
rpush numbers1 a b c d e
- lrangeKey start stop: obtains the elements in the range specified by key. When stop is -1, it starts from start to the last element in the list. For example:
lrange numbers 1 3
- lpopKey: Displays the specified key header element, the first element on the left. Example:
lpop numbers
- rpopKey: pop-ups specify key: tail element, i.e. the first element on the right. Example:
rpop numbers
- lindexKey index: The element with the specified key based on the index subscript. Example:
lindex numbers 1
- llenKey: Gets the number of key elements. Example:
llen numbers
- Lrem key count element:
- lpushkey element [element …] : Sets multiple elements in the header (left) of the key specified in the list. Example:
-
Common commands for Sets:
- saddkey member [member …] : Adds multiple non-repeating elements to the specified key. Example:
sadd set1 1 2 3 4
- smembersKey: Displays the specified key element. For example:
smembers set1
- sismemberKey member: checks whether the specified key has the specified value. For example:
sismember set1 2
- scardKey: Gets the number of key elements specified in the collection. Example:
scard set1
- sremkey member [member …] : Deletes the element in the collection with the specified key. Example:
srem set1 1 2
- srandmemberKey [count] : randomly obtains the count of the specified key in the collection. The default value of count is 1.
srandmember set1 2
- spopKey [count] : indicates the value of a specified key. Example:
spop set1 2
- smoveSource destination member: Moves the member element of a source to destination. Example:
smove set1 set2 3
- sdiffkey [key …] : Returns the set of differences between the element with the specified key and other keys, as shown in the following example:
sdiff set1 set2
- sinterkey [key …] : Returns the intersection of the element with the specified key and multiple other keys, as shown in this example:
sinter set1 set2
- sunionkey [key …] : Returns the union of the element with the specified key and multiple other keys, as shown in the following example:
sunion set1 set2
- saddkey member [member …] : Adds multiple non-repeating elements to the specified key. Example:
-
Sort Sets
- zadd key score member [score member …] : Adds multiple unique elements to the specified key. Score is used to sort elements. Example:
zadd zset1 10 b 20 a 30 e 40 c 50 d
- zrangeKey start stop [WITHSCORES] : queries the key elements in the range from start to stop (in sequence). If stop is -1, it indicates the start to the last element. The optional parameter WITHSCORES indicates that score is displayed.
zrange zset1 0 -1 withscores
- zrevrangeKey start stop [WITHSCORES] : queries the key elements in the range from start to stop (in reverse order). If stop is -1, it indicates the start to the last element. The optional parameter WITHSCORES indicates that score is displayed.
zrevrange zset1 0 -1 withscores
- zrangebyscoreMax [WITHSCORES] [LIMIT offset count] : Query key elements (order) of score values from min to Max, WITHSCORES indicates score display, LIMIT offset count is used for paging display, example:
zrangebyscore zset1 21 50 withscores
- zrevrangebyscoreMax [WITHSCORES] [LIMIT offset count] : Query the key element (in reverse order) of score values from min to Max, WITHSCORES indicates score display, LIMIT offset count is used for paging display, example:
zrangebyscore zset1 21 50 withscores
- zremkey member [member …] Removes one or more elements of the specified key from the set. Example:
zrem zset1 e a
- zcardKey: Returns the number of key elements specified in the set. Example:
zcard zset1
- zcountKey min Max: Returns the number of specified key elements in the score range of min and Max. Example:
zcount zset1 31 50
- zrankKey member: Returns the subscript (sorted by score) of the member elements of the set with the specified key. Example:
zrank zset1 d
- zrevrankKey member: Returns the subscript (in reverse order by score) of the member element of the set with the specified key. Example:
zrevrank zset1 d
- zadd key score member [score member …] : Adds multiple unique elements to the specified key. Score is used to sort elements. Example:
-
Hashes common commands:
- hsetkey field value [field value …] : Sets one or more k-V key-value pairs for the specified key. Example:
hset user name zhangsan age 30 hight 170
- hsetnxKey field value: Sets a K-V key-value pair for the specified key if the specified field does not already exist. Example:
hsetnx user weight 110
- hgetKey field: Gets the value of the specified key field. Example:
hget user name
- hmsetkey field value [field value …] : Equivalent to hset, sets one or more k-V key-value pairs for the specified key. Example:
hmset user name zhangsan age 30 hight 170
- hmgetkey field [field …] : gets the value specified by one or more fields of the specified key. Example:
hmget user name age hight
- hgetallKey: Returns all k-V key pairs of the specified key. For example:
hgetall user
- hdelkey field [field …] : Deletes one or more k-V key pairs of the specified key. For example:
hdel user name age
- hlenKey: Returns the number of k-V key-value pairs of the specified key. For example:
hlen user
- hexistsKey field: Checks whether the field with the specified key exists.
hexists user hight
- hkeysKey: Returns all fields specified by key. Example:
hkeys user
- hvalsKey: Returns the values of all fields specified by key. For example:
hvals user
- hincrbyKey field increment: Increments the field value of a specified key if the specified field value is an INTEGER. Example:
hincrby user age 5
- Hincrbyfloat Key field increment: Increments the field value of a specified key.
Hincrbyfloat user money 11.11
- hsetkey field value [field value …] : Sets one or more k-V key-value pairs for the specified key. Example:
-
Other common commands:
- Select index, index is the index subscript of redis database, the subscript starts from 0, default 16 libraries
- keysPattern: View all keys of the current library. Pattern supports regular expressions. Example:
keys *
- existskey [key …] : Checks whether the specified key exists in the current library. Example:
exists set1
- moveKey dbindex: moves the key specified by the current library to the database specified by dbindex. Example:
move user 1
- expireKey seconds, sets the expiration time for the specified key, in seconds. Example:
expire set1 5
- ttlKey: displays the remaining expiration time of the specified key. The unit is second. -1 indicates that the key never expires, -2 indicates that the key has expired, and other values indicate the remaining expiration time.
ttl set2
- typeKey: Displays the type of the specified key. For example:
type set2
- Dbsize: Displays the number of current database keys
- Flushdb: flushes the current database
- Flushall: flushes all databases