Redis Tutorial (ii): String data type

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

Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/131.html?1455808279

First, overview:

The string type is the most basic type of data storage in Redis, which 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 accommodate 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 for 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 value after append.
DECRKey O (1) Decrements the value of the specified key by 1. If the key does not exist, its initial value is 0, and after decr its value is-1. 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 value range for this operation is a 64-bit signed integer. The value after the decrement.
INCRKey O (1) Increments the value of the specified key by 1. If the key does not exist, its initial value is 0, and after incr its value is 1. 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 value range for this operation is a 64-bit signed integer. The value after increment.
DecrbyKey Decrement O (1) The value of the specified key is reduced by decrement. If the key does not exist, its initial value is 0, and after decrby its value is-decrement. 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 value range for this operation is a 64-bit signed integer. The value after the decrease.
IncrbyKey increment O (1) Adds the value atomicity of the specified key to increment. If the key does not exist, its initial value is 0, and after incrby its value is increment. 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 value range for this operation is a 64-bit signed integer. The value after the increment.
GETKey O (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.
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 values of the key are returned. As with the Get command, the command can handle only string Value, or Redis will give you a related error message. Returns the original value of the key, or nil if the key does not exist before.
STRLENKey O (1) Returns the length of the character value for the specified key, and if value is not of type string, Redis will fail and give 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 completes two operations, one is to set the value of the key to the specified string, and set the key in the Redis server survival time (in seconds). 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, at which point the effect is equivalent to the SET command. Conversely, if the key already exists, the command will not take any action and return. 1 indicates that the setting was successful, otherwise 0.
SETRANGEKey offset value O (1) Replaces the partial string value of the specified key. Starting with offset, the length of the replacement is the string length of the third parameter of the command value, where if the value of offset is greater than the string length of the original value of the key, Redis will be padded with value (Offset-strlen (value)) The 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 a new value after fill offset 0x00. 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 is executed with an increase in the length of the original value of the specified key, it will cause Redis to reallocate enough memory to accommodate all of the replaced strings, resulting in some performance impairment. The modified string value length.
GETRANGEKey Start end O (1) If the truncated string is short, we can consider the time complexity of the command as O (1), or O (n), where n represents the length of the truncated substring. When the substring is truncated, the command will contain both start (0 for first character) and end character in a closed interval, and if the end value exceeds the character length of value, the command will simply intercept all character data after start. Sub-string
setbitKey offset value O (1) Sets the value of bit on the specified offset, which can only be 1 or 0, after which the command returns the original bit value on that offset. 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 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, and the bit value added in the middle is 0. Finally, it should be stated that the value of offset must be greater than 0. The bit original value on the specified offset.
getbitKey offset O (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 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 all values of the specified keys, if one of them does not exist, or if the value is not of type string, the key's value returns nil. Returns a list of values for the specified keys.
MSETkey value [key value ...] O (N) n indicates the number of keys specified. The command atomically completes the set operation of all key/value in the parameter, and its specific behavior can be seen as executing the SET command multiple iterations. The command does not fail and always returns OK.
msetnxkey value [key value ...] O (N) n indicates the number of keys specified. The command atomically completes the set operation of all key/value in the parameter, and its specific behavior can be seen as multiple iterations of the execution of the SETNX command. However, it is necessary to make it clear that if any of the keys in this batch are already present, the operation will all be rolled back, that is, all modifications will not take effect. 1 means all keys are set successfully, and 0 means no key is modified.

#p # pagination Title #e#

Examples of commands:

1. Set/get/append/strlen:

The code is as follows:


/> Redis-cli #执行Redis客户端工具.
Redis 127.0.0.1:6379> exists MyKey #判断该键是否存在, there is a return of 1, otherwise 0 is returned.
(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" #该键已经存在, so returns the length of the appended value.
(integer) 11
Redis 127.0.0.1:6379> Get MyKey #通过get命令获取该键 to determine the results of 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"
The Redis 127.0.0.1:6379> strlen MyKey #获取指定Key的字符长度, equivalent to the Strlen function in the C library.
(integer) 14


2. Incr/decr/incrby/decrby:

The code is as follows:


Redis 127.0.0.1:6379> Set MyKey #设置Key的值为20
Ok
Redis 127.0.0.1:6379> incr mykey #该Key的值递增1 #p# pagination title #e#
(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 #对空值执行递减操作, its original value is set to 0, the decrement 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 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) ERR value is not a 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:

The code is as follows:


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


4. Setex:
#p # pagination Title #e#

The code is as follows:


Redis 127.0.0.1:6379> setex MyKey "Hello" #设置指定Key的过期时间为10秒.
Ok
#通过ttl命令查看一下指定Key的剩余存活时间 (in seconds), 0 means 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"
The Redis 127.0.0.1:6379> ttl mykey #该ttl命令的返回值显示 and the key has expired.
(integer) 0
Redis 127.0.0.1:6379> get MyKey #获取已过期的Key将返回nil.
(nil)


5. Setnx:

The code is 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 executes successfully.
(integer) 1
Redis 127.0.0.1:6379> setnx MyKey "World" #该键已经存在, so this setting doesn't produce any effect.
(integer) 0
Redis 127.0.0.1:6379> Get MyKey #从结果可以看出, the returned value is still the value set for the first time.
"Hello"


6. Setrange/getrange: #p # page Title #e#

The code is 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 0 at the end.
(integer) 22
Redis 127.0.0.1:6379> get MyKey #查看补0后替换的结果.
"Hello Ddrldx00x00x00x00x00x00x00x00x00dd"
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 #查看替换空值后的结果.
"X00X00DD"
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 bytes after the first byte are intercepted.
"123456789"


7. Setbit/getbit:

The code is as follows:

#p # pagination Title #e#
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 #获取设置的结果, binary 0000 0001 hexadecimal value 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 #获取设置的结果, binary 0000 0011 hexadecimal value 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 returns 0.
(integer) 0


8. Mset/mget/msetnx:

The code is 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, so the command executes successfully and returns 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, nil is returned because the Key5 is not set successfully.
Redis 127.0.0.1:6379> mget Key3 key5 #p # pagination Title #e#
1) "Stephen"
2) (nil)

Redis Tutorial (ii): String data type

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.