Analysis on the string type in Redis, Xinxing redis

Source: Internet
Author: User

Analysis on the string type in Redis, Xinxing redis

The string type is the most basic data storage type in Redis. It is binary safe in Redis, which means that this type can accept data in any format, for information such as data or json objects, the maximum length of string-type values in Redis is 512 MB.

Common commands are as follows:

(1) append key value if the key already exists, the append Command appends the value of the parameter value to the end of the existing value. If the key does not exist, this command creates a new Null String and appends the value to the end of it.

(2) The decr key degrades the value atomicity of the specified key. 1. If the key does not exist, the initial value is 0, and the value is-1 after decr ,. If the value cannot be converted to an integer value, such as hello, this operation fails and an error message is returned. Note that the value range of this operation is 64-bit signed integer.

(3) The incr key increments the value atomicity of the specified key. if the key does not exist, the initial value is 0 and the value becomes 1 after incr. If the vlaue value cannot be converted to an integer value, for example, hello, this operation fails and an error message is returned. Note that the value range of this operation is 64-bit signed integer.

(4) decrby key decrement: The value stored on the key minus the value specified by decrement. If the key does not exist, it is set to 0 before the operation, if the value associated with the key has an incorrect data type or contains a string that cannot be converted to an integer, this operation will result in an error. This operation only applies to the 64-bit signed number.

(5) get key: this operation obtains the value stored on the key. If the key does not exist, nil is returned. If the data type of the value stored on the key is not string, an error occurs. Because get only processes values of the string type.

(6) getbit key offset: return the position of the string stored on the key at the offset. If the specified offset value exceeds the length of the string, subsequent strings are regarded as a continuous space filled with 0. If the specified key does not exist, it is considered as an empty string. In this case, the offset value is out of the range and can be seen as a continuous space filled with 0.

(7) the time complexity of getrange key start end is O (N), where N is the length of the string to be returned. The time complexity is determined by the length of the final returned string. Because it is very low to create a substring from an existing string, it can be considered that the time complexity for a smaller string is O (1 ). Note that this command is called substr before Redis2.0. This command returns the special sub-string of the string on the key. The sub-string range is specified by the start and end offset values, and both contain endpoints. You can specify a negative offset value in this command. The offset value starts from the end of the string. Therefore,-1 indicates the last character, and-2 indicates the second to the last. If the specified offset value exceeds the inherent length of the string, the command limits the result range to the actual length of the string.

(8) getset key value: It is atomic. It sets the value stored on the key to value and returns the original value. If the key exists but the stored value is not of the string type, an error is returned.

(9) incrby key increment: It adds the specified value of increment to the value stored on the key. If the key does not exist, it is set to 0 before the operation is executed, then perform the operation. If the value stored on this key has an incorrect data type or contains a string value that cannot be converted to an integer type, an error is returned, this operation can only be performed on 64-bit signed integers.

(10) mget key1 key2... this operation returns the value stored on the specified keys. If the data type of each value is not a string type or a key that does not exist, the returned value will be nil. Therefore, this operation will not fail.

(11) mset key1 value1 key2 value2... this operation sets the corresponding value for the specified keys. Like the set command, mset replaces the original value with the new value. The mset operation is atomic. Therefore, all specified keys are set in one operation. The return value is always OK because the operation will not fail.

(12) msetnx key1 value1 key2 value2 .... it sets the given keys as the corresponding value. If any of the specified keys already exists, this operation will not be executed. Therefore, msetnx can be used to set the value of the keys that represent different fields in a unique logical object. Once again, it can ensure that these fields are either set successfully or not. Msetnx operations are atomic, so all keys will be set during an operation. If all keys values are set, the returned value is 1. If no key is set, the returned value is 0.

(13) set key value: it sets a string value for the specified key. If the key already has a value, the value will be overwritten regardless of its data type. The returned value is always OK, because the set operation will not fail.

(14) setbit key offset value it sets or clears the position of the string stored on the key at the specified offset. The value in the command can be set to 0 or 1. The location can be set or cleared according to the value. If the key does not exist, a new string value is created. The length of a string can be increased once to include the value at the offset. The offset value in the command must be greater than or equal to 0, but smaller than 232. This limits the bitmap size to MB. When the string stored on the key is expanded, the added bit is set to 0. note that when the last possible bit is set, the offset value is equal to 231, redis needs to re-allocate the intermediate memory when the string stored on the key does not contain the string value or contains a smaller string value, which may block the server for a period of time. Once space allocation is completed for the first time, subsequent setbit calls to the same key will no longer have space allocation overhead.

(15) setex key seconds value this operation sets the value stored on the key to value, and the key automatically times out after the given seconds. This command is equivalent to set mykey value expire mykey seconds. The setex operation is atomic and can be replaced by the above two commands in the multi/exec block. This command provides an alternative to the specified operation sequence. This operation is common when Redis is used as a cache.

(16) setnx key value if the specified key does not exist, set it to a string. In this case, the command is equivalent to set. When a value is stored on the key, no operation is performed. Setnx can be seen as the abbreviation of set if not exists.

(17) setrange key offset value: it is the part where the string stored on the key is overwritten starting from the specified offset position. The length is the same as the length of the value. If offset is greater than the length of the current string, the string is supplemented with 0 to adapt to the specified offset. If the specified key does not exist, the value stored on it is considered as a space, therefore, this command will ensure that the key will hold a sufficient string to set its value at the offset. Note that the maximum offset value we can set is 229-1, because the Redis string size is limited to MB.

(18) strlen key indicates the length of the string stored on the key. If the value stored on the key is not of the string type, an error is returned. If the key does not exist, the return value is 0.













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.