Redis Tutorial (ii): String data type _redis

Source: Internet
Author: User
Tags strlen redis server redis tutorial

First, overview:

A string type is the most basic type of data storage in Redis, and it is binary safe in Redis, which means that the type can accept data in any format, such as JPEG image data or JSON object description information. The value of the string type in Redis can hold up to 512M of data length.

Ii. List of related commands:

Command prototypes
Complexity of Time Command description return value
APPENDKeyValue
O (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 creates a new key/value. The length of the appended value.
DECRKey O (1) Decrements the value atomicity of the specified key by 1. If the key does not exist, its initial value is 0, and the value is-1 after DECR. If value cannot be converted to an integer value, such as Hello, the operation will fail and return the appropriate error message. Note: This operation is scoped to a 64-bit signed integer. The decremented value.
INCRKey O (1) Increments the value atomicity of the specified key by 1. If the key does not exist, its initial value is 0, and the value is 1 after incr. If value cannot be converted to an integer value, such as Hello, the operation will fail and return the appropriate error message. Note: This operation is scoped to a 64-bit signed integer. The incremented value.
DecrbyKey Decrement O (1) Decrement the reduction of the value atomicity of the specified key. If the key does not exist, its initial value is 0, and its value is-decrement after Decrby. If value cannot be converted to an integer value, such as Hello, the operation will fail and return the appropriate error message. Note: This operation is scoped to a 64-bit signed integer. The reduced value.
IncrbyKey increment O (1) Adds the value atomicity of the specified key to the increment. If the key does not exist, its initial value is 0, and its value is increment after Incrby. If value cannot be converted to an integer value, such as Hello, the operation will fail and return the appropriate error message. Note: This operation is scoped to a 64-bit signed integer. The value of the increment.
Get key O (1) Gets the value of the specified key. If the Value associated with this key is not a string type, Redis returns an error message because the GET command can only be used to obtain string Value. Value associated with the key, and returns nil if the key does not exist.
SETKey value O (1)
Sets the key to hold the specified string value, overwriting its original value if the key already exists. Always return "OK".
getsetKey value O (1) The atomic setting of the key is the specified value, and the original value of the key is returned. As with the Get command, the command can only handle string Value, otherwise Redis will give the associated error message. Returns the original value of the key, and returns nil if the key does not exist before.
STRLENKey O (1) Returns the length of the character value of the specified key, and if value is not a string type, Redis executes the failure and gives the associated error message. Returns the value character length of the specified key, or 0 if the key does not exist.
SetexKey seconds value O (1) Atomicity accomplishes two things, one is to set the value of the key to the specified string, and to set the number of seconds that the key survives in the Redis server. This command is primarily used when Redis is used as a cache server.
setnxKey value O (1) If the specified key does not exist, the key is set to hold the specified string value, and the effect 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 or 0.
SETRANGEKey offset value O (1) Replaces a partial string value for the specified key. Beginning with offset, the length of the replacement is the string length of the third parameter value of the command, where the value of offset is greater than the string length of the value of the key, Redis will be padded after value (Offset-strlen (value)) Number of 0x00, and then append the new value. If the key does not exist, the command assumes the length of its original value as 0, and then appends the offset 0x00 to append the new value. Since the maximum length of string value is 512M, the maximum value for offset is 536870911. Finally, it is important to note that if the command causes the value of the specified key to increase in length when executed, this will cause the Redis to reallocate enough memory to accommodate all the replaced strings, thereby causing some performance impairment. The modified string value length.
getrangeKey Start end O (1) If the length of the intercepted string is very short, we can see the time complexity of the command as O (1), or O (n), where N denotes the length of the intercepted substring. When the substring is intercepted, the command will contain both the start (0 for thefirst character) and the end of the string in a closed interval, and if the end value exceeds the character length of value, the command will simply intercept all character data from the beginning of start. Substring
setbitKey offset value O (1) Sets the value of the bit on the specified offset, which can only be 1 or 0, and returns the original bit value on the offset upon setting. If the specified key does not exist, the command creates a new value and sets the bit value in the parameter on the specified offset. If the offset is greater than the character length of value, Redis will lengthen the value and set the bit value in the parameter on the specified offset, with the bit value added in the middle being 0. The last thing to note is that the offset value must be greater than 0. The bit original value on the specified offset.
getbitKey offset O (1) Returns the value of the bit on the specified offset, 0 or 1. If offset exceeds the length of string value, the command returns 0, so always returns 0 for an empty string. The bit value on the specified offset.
mgetkey [key ...] O (N) n indicates the number of keys to get. Returns values for all specified keys, if one of the key does not exist, or if its value is not a string type, the value of the key returns to nil. Returns a list of values for the specified keys.
msetkey value [key value ...] O (N) n indicates the number of key specified. All key/value in the completion parameters of the command atomicity are set up, and their specific behavior can be seen as multiple iterations to execute the SET command. This command does not fail and always returns OK.
msetnxkey value [key value ...] O (N) n indicates the number of key specified. All key/value in the completion parameters of the command atomicity are set up, and their specific behavior can be seen as multiple iterations to execute the setnx command. What needs to be made clear here, however, is that if any key in the keys already exists, the operation will be rolled back, that is, all modifications will not take effect. 1 indicates that all keys are set successfully, and 0 indicates that no key has been modified.

Third, the command example:

1. Set/get/append/strlen:

Copy Code code as follows:

/> Redis-cli #执行Redis客户端工具.
Redis 127.0.0.1:6379> exists MyKey #判断该键是否存在, there is a return of 1, otherwise 0.
(integer) 0
Redis 127.0.0.1:6379> append MyKey "Hello" #该键并不存在, so the append command returns the length of the current value.
(integer) 5
Redis 127.0.0.1:6379> Append MyKey "World" #该键已经存在, thus returning the length of the appended value.
(integer) 11
Redis 127.0.0.1:6379> Get MyKey #通过get命令获取该键 to determine the results of the append.
"Hello World"
Redis 127.0.0.1:6379> Set MyKey "This is a test" #通过set命令为键设置新值 and overwrites the original value.
Ok
Redis 127.0.0.1:6379> Get MyKey
"This is a test"
Redis 127.0.0.1:6379> strlen MyKey #获取指定Key的字符长度, equivalent to the Strlen function in C library.
(integer) 14

2. Incr/decr/incrby/decrby:
Copy Code code as follows:

Redis 127.0.0.1:6379> Set MyKey #设置Key的值为20
Ok
Redis 127.0.0.1:6379> incr MyKey #该Key的值递增1
(integer) 21
Redis 127.0.0.1:6379> DECR MyKey #该Key的值递减1
(integer) 20
Redis 127.0.0.1:6379> del MyKey #删除已有键.
(integer) 1
Redis 127.0.0.1:6379> decr MyKey #对空值执行递减操作, the original value is set to 0, and the descending value is-1
(integer)-1
Redis 127.0.0.1:6379> del MyKey
(integer) 1
Redis 127.0.0.1:6379> incr MyKey #对空值执行递增操作, the original value is set to 0, the increment of the value is 1
(integer) 1
Redis 127.0.0.1:6379> set MyKey Hello #将该键的Value设置为不能转换为整型的普通字符串.
Ok
Redis 127.0.0.1:6379> incr MyKey #在该键上再次执行递增操作时, Redis will report an error message.
(Error) The ERR value is "not" an integer or out of range
Redis 127.0.0.1:6379> Set MyKey 10
Ok
Redis 127.0.0.1:6379> Decrby MyKey 5
(integer) 5
Redis 127.0.0.1:6379> Incrby MyKey 10
(integer) 15

3. Getset:
Copy Code code as follows:

Redis 127.0.0.1:6379> incr mycounter #将计数器的值原子性的递增1
(integer) 1
#在获取计数器原有值的同时, and set it to the new value, both of which are done at the same time as atomic.
Redis 127.0.0.1:6379> getset mycounter 0
"1"
Redis 127.0.0.1:6379> get MyCounter #查看设置后的结果.
"0"

4. Setex:
Copy Code code as follows:

Redis 127.0.0.1:6379> setex MyKey "Hello" #设置指定Key的过期时间为10秒.
Ok
#通过ttl命令查看一下指定Key的剩余存活时间 (seconds), 0 indicates that it has expired,-1 means never expires.
Redis 127.0.0.1:6379> TTL MyKey
(integer) 4
Redis 127.0.0.1:6379> get MyKey #在该键的存活期内我们仍然可以获取到它的Value.
"Hello"
Redis 127.0.0.1:6379> ttl MyKey #该ttl命令的返回值显示, the key has expired.
(integer) 0
Redis 127.0.0.1:6379> get MyKey #获取已过期的Key将返回nil.
(nil)

5. Setnx:
Copy Code code as follows:

The Redis 127.0.0.1:6379> del MyKey #删除该键 for the following test validation.
(integer) 1
Redis 127.0.0.1:6379> setnx MyKey "Hello" #该键并不存在, so the command was executed successfully.
(integer) 1
Redis 127.0.0.1:6379> setnx MyKey "World" #该键已经存在, so this setting does not produce any effect.
(integer) 0
Redis 127.0.0.1:6379> Get MyKey #从结果可以看出, the value returned is still the first value set.
"Hello"

6. Setrange/getrange:
Copy Code code as follows:

Redis 127.0.0.1:6379> set MyKey "Hello World" #设定初始值.
Ok
Redis 127.0.0.1:6379> SetRange MyKey 6 dd #从第六个字节开始替换2个字节 (DD only 2 bytes)
(integer) 11
Redis 127.0.0.1:6379> get MyKey #查看替换后的值.
"Hello Ddrld"
Redis 127.0.0.1:6379> SetRange MyKey dd #offset已经超过该Key原有值的长度了, the command will be up to 0 at the end.
(integer) 22
Redis 127.0.0.1:6379> get MyKey #查看补0后替换的结果.
"Hello Ddrld\x00\x00\x00\x00\x00\x00\x00\x00\x00dd"
Redis 127.0.0.1:6379> del MyKey #删除该Key.
(integer) 1
Redis 127.0.0.1:6379> setrange MyKey 2 dd #替换空值.
(integer) 4
Redis 127.0.0.1:6379> get MyKey #查看替换空值后的结果.
"\X00\X00DD"
Redis 127.0.0.1:6379> Set MyKey "0123456789" #设置新值.
Ok
Redis 127.0.0.1:6379> getrange MyKey 1 2 #截取该键的Value, starting with the first byte and ending with the second byte.
"12"
Redis 127.0.0.1:6379> getrange MyKey 1 #20已经超过Value的总长度, so all the bytes after the first byte will be intercepted.
"123456789"

7. Setbit/getbit:
Copy Code code as follows:

Redis 127.0.0.1:6379> del MyKey
(integer) 1
Redis 127.0.0.1:6379> setbit MyKey 7 1 #设置从0开始计算的第七位BIT值为1, return the original bit value 0
(integer) 0
Redis 127.0.0.1:6379> Get MyKey #获取设置的结果, the hexadecimal value of binary 0000 0001 is 0x01
"\X01"
Redis 127.0.0.1:6379> setbit MyKey 6 1 #设置从0开始计算的第六位BIT值为1, return the original bit value 0
(integer) 0
Redis 127.0.0.1:6379> Get MyKey #获取设置的结果, the hexadecimal value of binary 0000 0011 is 0x03
"\x03"
Redis 127.0.0.1:6379> getbit MyKey 6 #返回了指定Offset的BIT值.
(integer) 1
Redis 127.0.0.1:6379> getbit MyKey #Offset已经超出了value的长度, so it returns 0.
(integer) 0

8. Mset/mget/msetnx:
Copy Code code as follows:

Redis 127.0.0.1:6379> mset key1 "Hello" Key2 "World" #批量设置了key1和key2两个键.
Ok
Redis 127.0.0.1:6379> mget key1 key2 #批量获取了key1和key2两个键的值.
1) "Hello"
2) "World"
#批量设置了key3和key4两个键, because they did not exist before, the command executed successfully and returned 1.
Redis 127.0.0.1:6379> msetnx Key3 "Stephen" Key4 "Liu"
(integer) 1
Redis 127.0.0.1:6379> mget Key3 Key4
1) "Stephen"
2) "Liu"
#批量设置了key3和key5两个键, but the Key3 already exists, so the command fails and returns 0.
Redis 127.0.0.1:6379> msetnx Key3 "Hello" Key5 "World"
(integer) 0
#批量获取key3和key5, because Key5 is not set to succeed, it returns nil.
Redis 127.0.0.1:6379> mget Key3 key5
1) "Stephen"
2) (nil)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.