In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "String data type example analysis in Redis", the content is detailed, the steps are clear, and the details are handled properly. I hope that this "String data type instance analysis in Redis" article can help you solve your doubts.
Overview:
String type is the most basic data storage type in Redis, and it is binary safe in Redis, which means that this type can accept data in any format, such as JPEG image data or Json object description information. The maximum length of data that a string type Value can hold in Redis is 512m.
List of related commands: the command prototype time complexity command describes the return value APPENDO (1) if the Key already exists, the APPEND command appends the data of the parameter Value to the end of the existing Value. If the Key does not exist, the APPEND command will create a new Key/Value. The length of the appended Value. DECRO (1)
Decrements the Value atomicity of the specified Key by 1. If the Key does not exist, its initial value is 0 and its value is-1 after the decr. If the value of Value cannot be converted to an integer value, such as Hello, the operation will fail
And return the corresponding error message. Note: the range of values for this operation is 64-bit signed integers.
The value after decreasing. INCRO (1)
Increments the Value atomicity of the specified Key by 1. If the Key does not exist, its initial value is 0 and its value is 1 after the incr. If the value of Value cannot be converted to an integer value, such as Hello, the operation will fail
And return the corresponding error message. Note: the range of values for this operation is 64-bit signed integers.
The incremented value. DECRBYO (1)
The Value atomicity of the specified Key is reduced by decrement. If the Key does not exist, its initial value is 0 and its value is-decrement after decrby. If the value of Value cannot be converted to an integer value
For example, Hello, the operation fails and returns the appropriate error message. Note: the range of values for this operation is 64-bit signed integers.
The reduced value. INCRBYO (1)
The Value atomicity of the specified Key will be increased by increment. If the Key does not exist, its initial value is 0 and its value is increment after incrby. If the value of Value cannot be converted to an integer value, such as Hello
The operation fails and returns the appropriate error message. Note: the range of values for this operation is 64-bit signed integers.
The increased value. GETO (1) gets the Value of the specified Key. If the Value associated with the Key is not of type string, Redis returns an error message because the GET command can only be used to get string Value. The Value associated with the Key, if the Key does not exist, returns nil. SETO (1) sets the Key to hold the specified string Value, overwriting its original value if the Key already exists. Always return "OK". GETSETO (1) atomicity sets the Key to the specified Value and returns the original value of the Key. Like the GET command, this command can only deal with string Value, otherwise Redis will give you the relevant error message. Returns the original value of the Key, or nil if the Key does not previously exist. STRLENO (1) returns the character length of the specified Key. If Value is not of type string, Redis will fail and give the relevant error message. Returns the Value character length of the specified Key, or 0 if the Key does not exist. SETEXO (1) atomicity completes two operations, one is to set the value of the Key to the specified string, while setting the survival time (seconds) of the Key in the Redis server. This command is mainly used when Redis is used as a Cache server. SETNXO (1) if the specified Key does not exist, the Key is set to hold the specified string Value, which is equivalent to the SET command. Conversely, if the Key already exists, the command does nothing and returns. 1 indicates that the setting is successful, otherwise 0. SETRANGEO (1)
Replaces part of the string value of the specified Key. Starting with offset, the length of the replacement is the string length of the third argument of the command, value, where if the value of offset is greater than the string length of the original value of the Key, Value
Redis will fill in (offset-strlen (value)) the number of 0x00 after the Value, and then append the new value. If the key does not exist, the command assumes the length of its original value to be 0, followed by an offset
Append the new value after 0x00. Since the maximum length of the string Value is 512m, the maximum value of offset is 536870911. Finally, it is important to note that if the command causes the original value of the specified Key to be specified when executed
An increase in length will cause Redis to reallocate enough memory to accommodate all the strings after replacement, resulting in a certain performance penalty.
The modified string Value length. GETRANGEO (1)
If the length of the intercepted string is very short, we can regard the time complexity of the command as O (1), otherwise it is O (N), where N represents the length of the intercepted substring. When this command intercepts a substring, it takes the side of the closed interval
Formula contains both the character of start (0 for the first character) and the character of end. If the end value exceeds the character length of Value, the command will only intercept all character data since the beginning of start.
Substring SETBITO (1)
Sets the value of BIT on the specified Offset, which can only be 1 or 0. After setting, the command returns the original bit value on the Offset. If the specified Key does not exist, the command creates a new value and specifies the
The bit value in the parameter is set on the Offset of the If Offset is greater than the character length of Value, Redis lengthens the value of value and sets the bit value in the parameter on the specified Offset, with a bit value of 0 added in the middle. Last but not least
Indicates that the offset value must be greater than 0.
The original value of BIT on the specified Offset. GETBITO (1) returns the value of BIT on the specified Offset, 0 or 1. If Offset exceeds the length of string value, the command returns 0, so it always returns 0 for empty strings. The bit value on the specified Offset. MGETO (N) N represents the number of Key obtained. Returns all the Values of the specified Keys, and if one of the Key does not exist, or if its value is not of type string, the Value of that Key returns nil. Returns a list of Values for the specified Keys. MSETO (N) N represents the number of specified Key. This command atomically completes all the key/value setting operations in the parameters, and its specific behavior can be regarded as executing the SET command multiple iterations. The command does not fail and always returns OK. MSETNXO (N)
N represents the number of specified Key. This command atomically completes all the key/value setting operations in the parameters, and its specific behavior can be regarded as executing the SETNX command multiple iterations. However, what needs to be made clear here is that
If any Key already exists in this batch of Keys, the operation will all be rolled back, that is, none of the changes will take effect.
String (string)
Append key value adds a value to the original value. If the key does not already exist, a new key and value are not appended.
Substr key start len intercepts key, starting with start, intercepts the length of len
Strlen key gets the length of the key
Incr key self-increasing 1
Decr key minus 1
Incrby key num increases key, increases num
Decrby key num reduces key, reduces num
Getrange key start end intercepts characters key [start,end] header also includes tail
The data of setrange key offset value replacing offset location is changed to value (offset is the index of key)
Setex key seconds value sets the expiration time of key
Setnx key value does not exist setting key, value if key exists, adding value again will fail
Mset key1 value key2 value sets multiple key and value at one time
Mget key1 key2 acquires value of multiple key at one time
Msetnx key1 value key2 value sets multiple key and value at once. If one of the key exists, all of them fail to be created (atomicity).
If getset key value does not exist to get nil, then set the value, and if there is a value before getting, the value after setting (update operation)
# 127.0.0.1 keys 6379 > set key1 vOK127.0.0.1:6379 > get key1 "v" 127.0.0.1 keys * 1) "key1" 127.0.0.1 keys 6379 > exists key1 (integer) 1127.0.1 keys 6379 > append key1 v1 (integer) 3127. 0.0.1 vv1 > substr key1 0 3 "vv1" 127.0.0.1 substr key1 6379 > strlen key1 (integer) 3127.0.0.1 strlen key1 > append key1 "hello1" (integer) 9127.0.0.1 > substr key1 12 "v1" 127.0.0.1 > substr key1 1 "v" 127.0.1VR 6379 > append key2 "lisi" (integer) 4127.0.0.1 > get key2 " Lisi "# # 127.0.0.1 lisi 6379 > set views 0OK127.0.0.1:6379 > get views" 0 "127.0.0.1 get views 6379 > incr views (integer) 1127.0.0.1 get views 6379 > incr views (integer) 2127.0.0.1 get views 6379 > get views" 2 "127.0. 0.1 decr views 6379 > decr views (integer) 1127.0.0.1 decr views (integer) 0127.0.0.1 decr views 6379 > incrby views 10 (integer) 10127.0.0.1 incrby views 6379 > decrby views 5 (integer) 5 colors 127.0.0.1 decr views 6379 > set key1 "hello World "OK127.0.0.1:6379 > GETRANGE key1 0 3" hell "127.0.0.1 GETRANGE key1 6379 > GETRANGE key1 0-1" hello World "127.0.0.1 world 6379 > SETRANGE key2 32 (integer) 7127.0.0.1 integer 6379 > get key2" abc2efg "# 127.0.0.1 integer 6379 > setex key3 30 helloOK127.0.0.1:6379 > get key3" hello "127.0.0.1 get key2 (integer) 24127.0.0.1: 6379 > SETNX mykey redis (integer) 1127.0.0.1 keys * 1) "mykey" 2) "key2" 3) "key1" 127.0.0.1 keys 6379 > SETNX mykey "MongoDB" (integer) 0127.0.1 redis > get mykey "redis" # 127.0.1 > mset K1 v1 k2 v2OK127.0. 0.1key 6379 > mget k1 k21) "v1" 2) v2 "127.0.0.1 integer object # the key here is an ingenious design user: {id}: {filed} 127.0.0.1 user > msetnx user:1:name" zhangsan "user:1:age 2 (integer) 1127.0.1 key > mget user:1:name user:1:age1)" zhangsan "2)" 2 "127.0.0.1 set article:101:views 0OK127.0.0.1:6379 > incr article:101:views (integer) 1127.0.0.1 get article:101:views" 1 "# # 127.0.0.1: 6379 > getset db redis (nil) 127.0.0.1 getset db 6379 > get db "redis" 127.0.0.1 VR 6379 > getset db 10 "redis"
String similar scenario: value can be our number as well as our string!
Counter
Count the number of multiple units uid:1923:follow 0
Number of fans
Object cache storage
After reading this, the article "instance Analysis of String data types in Redis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.