Redis base data type (String)

Source: Internet
Author: User
Tags redis redis server
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.

Original:

A String value can is at Max's megabytes in 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 the first 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:
/> 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 code" #通过set命令为键设置新值 and overwrites the original value.
Ok
Redis 127.0.0.1:6379> Get MyKey
"This is a test code"
Redis 127.0.0.1:6379> strlen MyKey #获取指定Key的字符长度, equivalent to the Strlen function in C library.
(integer) 19

2. Incr/decr/incrby/decrby:
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 orout 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:
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 Mycounter0
"1"
Redis 127.0.0.1:6379> get MyCounter #查看设置后的结果.
"0"

4. Setex:
Redis 127.0.0.1:6379> setex MyKey "Hello" #设置指定Key的过期时间为10

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.