Sinsing analysis of string types in Redis

Source: Internet
Author: User

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, more than data, such as JSON objects, and that the value of a string type in Redis can hold up to 512M of data length.

The commands we use are as follows:

(1) Append key value if the key already exists, then the append command appends the value of the parameter value to the end of the already existing value, and if the key does not exist, the command creates a new empty string and appends value to its end.

(2) DECR key 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 becomes-1. If the value of value cannot be converted to an integer value, such as Hello, the operation fails and an error message is returned. Note that the range of values for this operation is a 64-bit signed integer.

(3) INCR key increments the value atomicity of the specified key by 1. If the key does not exist, its initial value is 0, after incr its value becomes 1, if the value of Vlaue cannot be converted to an integer value, such as Hello, then the operation fails and returns an error message. Note that the range of values for this operation is a 64-bit signed integer.

(4) Decrby key decrement subtracts the value stored on the key by the value specified by decrement, and if the key does not exist, it is set to 0 before the operation, if the value associated with key has the wrong data type or contains a character that cannot be converted to an integer String, the operation will have an error. This operation is only for 64-bit signed numbers.

(5) Get key The operation gets the value stored on the key, and if key does not exist, then nil will be returned. If the data type of the value stored on the key is not a string, an error will occur. Because get only handles values of type string.

(6) Getbit key offset it returns the position of the string stored on the key at offset offset, and if the specified offset value exceeds the length of the string, subsequent strings are treated as a contiguous space filled by 0. If the specified key does not exist, it is treated as an empty string, in which case the offset value is out of range and can be seen as a contiguous space filled by 0.

(7) GetRange key start end it has a time complexity of O (n), where N is the length of the string that needs to be returned. The time complexity is determined by the length of the final return, since creating a substring from an already existing string is very inexpensive and can be thought of as a time complexity O (1) for smaller strings. It is important to note that this command is called substr before the Redis2.0 version. The command returns a specific substring of the string on the key, the range of which is specified by the start and end two offset values, and both contain the endpoints. In this command, you can specify a negative offset value, at which point the offset value starts at the end of the string. So, 1 means the last character, 2 means the penultimate character. If you specify an offset value that exceeds the intrinsic length of the string, the command will limit the result to the true length of the string.

(8) Getset key value It is atomic to set the value stored on key to value and return the original value. If the key exists but the stored value is not a string type, an error is returned.

(9) Incrby key Increment it adds the value specified by increment to the value stored on the key, and if the key does not exist, it is now set to 0 before the operation is performed. If the value stored on the key has the wrong data type or contains a string value that cannot be converted to an integral type, an error will be returned that can only be used for 64-bit signed integers.

(Ten) Mget key1 key2 .... This operation returns the value stored above the specified keys. For each data type whose value is not a string type or a nonexistent key, the return value will be nil. Therefore, this operation does not occur without failure.

(one) Mset key1 value1 key2 value2 ..... This action sets the appropriate value for the specified keys. As with the set command, Mset will replace the original value with the new value, and the Mset operation is atomic, so all the keys specified will be set to complete during one operation. The return value is always OK because the operation does not fail.

(msetnx) key1 value1 key2 value2 .... It is set to the corresponding value of the given keys and will not be executed if any one of the keys specified already exists. As a result, msetnx can be used to set its value on keys that represent different fields in a unique logical object, to ensure that the fields are either set successfully or not set. The msetnx operation is atomic, so all keys will be set during a single operation. If all keys values are set, the return value is 1. If no key is set to its value, the return value is 0.

Set key value It sets a string value to the specified key, and if the key already has a value, the value will be overridden regardless of its data type. The return value is always OK because the set operation does not fail.

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 specified as 0 or 1, and the position can be set or cleared, depending on the value. When the key does not exist, a new value for the string is created. The length of the string can be increased again to include the value at offset offsets. The value of offset in the command needs to be greater than or equal to 0, but less than 232. This will limit the size of the bitmap to less than 512MB. When the string stored on the key is expanded, the The added bit will be set to 0. It is important to note that when the last possible bit is set, the offset value equals 231, and the string stored on the key does not contain a string value or contains a string value of a smaller length, Redis needs to reallocate the intermediate memory, which may block the server for some time. Once the first allocation space allocation is complete, subsequent setbit calls to the same key will no longer have the overhead of allocating space.

(Setex) Key seconds value This operation sets the value stored on key and the key automatically times out after a given seconds seconds. This command is equivalent to the set MyKey value expire MyKey seconds. Where the Setex operation is atomic and can be replaced with the above two commands located in the Multi/exec block. This command provides an alternative to the specified sequence of operations, which is common when redis is used as a cache.

(+) 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 already stored on the key, no action is taken. Setnx can be seen as an abbreviation of the set if not exists.

(+) SetRange key offset value It is the portion of the string that is stored on the key that is overridden, starting at the specified offset position, the same length as value. If offset is greater than the length of the current string, the string is supplemented to accommodate the specified offset. If the specified key does not exist, the value stored on it is considered to be a space, so the command will ensure that the key holds a string that is sufficient to be hit so that it can set its value at offset offsets. Note, however, that the maximum offset value we can set is 229-1, because the size of the Redis string is limited to less than 512MB.

Strlen key indicates the length of the string that is stored on the key. If the value stored on the key is not a string type, an error is returned. If the key does not exist, then the return value is 0.













Sinsing analysis of string types in Redis

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.