String data type of redis Study Notes

Source: Internet
Author: User
Tags redis server

    • Min Qifeng
    • Email: [email protected]
    • Blog: http://blog.csdn.net/qifengzou
    • Date: 2014.10.11
    • Reprinted, please indicate the csdn blog from "Qi Feng"


1. Overview

The string data type is the simplest and most basic data type in redis. All keys and values in redis are of the string type, other more complex data types (lists, sets, sorted-sets, and hashes) are also stored Based on the string data type. The string data type is binary secure, that is, the string can contain data in any format, such as JPG images and serialized objects.


2 operation commands

Table 1 string-type operation commands

01 AppendKey Value
  Function Description: Subtract 1 from the value corresponding to the key.
Returned Results: After the append operation, the key corresponds to the final length of the value.
Note:
1) time complexity: O (1)
2) When the key exists and is of the string type, the value is appended to the end of the original value;
3) if the key does not exist, a new key/value is created.
02 DecrKey
  Function Description: Subtract 1 from the value atomicity of the key.
Return result: return the value corresponding to the key after performing the minus 1 operation.
Note:
1) time complexity: O (1)
2) If the value corresponding to the key can be converted to a number, perform the minus 1 operation;
3) if the value corresponding to the key cannot be converted to a number, an error message is returned;
4) this operation is atomic. Assume that the value of the current key is 100. When the three clients simultaneously perform this operation, the final result will be 97.
03 IncrKey
  Function Description: Add 1 of the value atomicity of the key.
Return result: return the value corresponding to the key after performing the plus 1 operation.
Note:
1) time complexity: O (1)
2) If the value corresponding to the key can be converted to a number, add 1;
3) if the value corresponding to the key cannot be converted to a number, an error message is returned;
4) this operation is atomic. Assume that the value corresponding to the current key is 100. When the three clients simultaneously perform this operation, the final result will be 103.
04
DecrbyKey decremen
  Function Description: decrement the value atomicity of the key.
Return result: return the value corresponding to the key after performing the subtraction operation.
Note:
1) time complexity: O (1)
2) If the value corresponding to the key can be converted to a number, perform the subtraction operation;
3) if the value corresponding to the key cannot be converted to a number, an error message is returned;
4) The decrement parameter cannot be a floating point, but an integer;
5) The operation is atomic.
05 IncrbyKey Increment
  Function Description: add the value atomicity of the Key to the increment.
Return result: return the value corresponding to the key after the add operation is executed.
Note:
1) time complexity: O (1)
2) If the value corresponding to the key can be converted to a number, add the key;
3) if the value corresponding to the key cannot be converted to a number, an error message is returned;
4) The increment parameter cannot be a floating point, but an integer;
5) The operation is atomic.
06 IncrbyfloatKey Increment
  Function Description: add the value atomicity of the Key to the increment.
Return result: return the value corresponding to the key after the add operation is executed.
Note:
1) time complexity: O (1)
2) If the value corresponding to the key can be converted to a number, add the key;
3) if the value corresponding to the key cannot be converted to a number, an error message is returned;
4) The increment parameter of this command can be a floating point number, but pay attention to the computing precision of this command;
5) There is no command for decrbyfloat. If you want to subtract a floating point number, you can imitate it: incrbyfloat key-5.9;
6) The operation is atomic.
07 SetKey value [ex seconds] [PX milliseconds] [NX | XX]
  Function Description: set the value corresponding to the key
parameter description:
starting from redis 2.6.12, the SET command behavior can be modified using a series of parameters:
1) ex second: set the key expiration time to second seconds
set key value ex second. The effect is equivalent to setex key second value.
2) Px millisecond: Set the key expiration time to millisecond
set key value PX millisecond equivalent to psetex key millisecond value.
3) NX: Set the key only when the key does not exist
set key value NX is equivalent to setnx key value
4) XX: you can set a key only when the key already exists.
returned results:
1) Before 2.6.12, OK is always returned.
2) After 2.6.12, OK is returned;
3) If NX or XX is set, but the setting operation is not executed because the condition is not met, the command returns an Empty bulk reply (null bulk reply ).
Note:
1) time complexity: O (1)
2) if the key already exists, this operation overwrites the old value and ignores the type;
3) when the set command is successfully executed on a key with a TTL, the original TTL of the key is cleared.
08 GetKey
  Function Description: gets the value of a specified key.
Returned result: the value of the specified key.
Note:
1) time complexity: O (1)
2) If the key does not exist, Nil is returned;
3) if the key is not of the string type, an error message is returned.
09 GetSetKey Value
  Function Description: sets the value of the key atomically and obtains the original value of the key.
Returned result: the original value of the key.
Note:
1) time complexity: O (1)
2) If the key does not exist, a new key is created and nil is returned;
3) if the key is not of the string type, an error message is returned.
10 StrlenKey
  Function Description: gets the value length of the key.
Returned result: the length of the value of the key.
Note:
1) time complexity: O (1)
2) If the key does not exist, 0 is returned;
3) if the key is not of the string type, an error message is returned.
11 SetexKey seconds Value
  Function Description: Set the corresponding value of the key to value, and set the survival time of the key to seconds.
Returned results: OK is returned only when the operation is complete.
Note:
1) time complexity: O (1)
2) If the key already exists, this operation will directly overwrite the old value;
3) The command is similar to the following two commands:
Set key value
Expire key seconds
The difference is that setex is atomic. The associated value and the Set survival time are completed at the same time. This command is very useful when redis is used as a cache;
4) if the seconds parameter is invalid, an error is returned.
12 SetnxKey Value
  Function Description: Set the key value to value only when the key does not exist.
Returned results: 1 is returned if the setting is successful, and 0 is returned if the setting fails.
Note:
1) time complexity: O (1)
2) set the key value to value, if and only if the key does not exist;
3) if the given key already exists, setnx will not take any action;
4) setnx is short for "set if not exists" (if it does not exist, set.
13 SetbitKey Offset Value
  Function Description: sets or clears the bit on the specified offset for the string value stored in the key)
Return result: Specify the offset of the originally stored bits.
Note:
1) time complexity: O (1)
2) Bit setting or clearing depends on the value parameter, which can be 0 or 1;
3) when the key does not exist, a new string value is automatically generated;
4) The string will be stretched (grown) to ensure that it can save the value on the specified offset. When the string value is stretched, the blank position is filled with 0;
5) The offset parameter must be greater than or equal to 0 and smaller than 2 ^ 32 (bit ing is limited to 512 MB );
6) use largeOffsetOfSetbitFor the operation, memory allocation may cause the redis server to be blocked. ReferenceSetrangeCommand, in the warning section.
14 GetbitKey offset
  Function Description: obtains the bit on the specified offset for the string value stored in the key)
Returned result: the bit at the specified offset.
Note:
1) time complexity: O (1)
2) If the offset value is longer than the string value or the key does not exist, 0 is returned;
3) when the key does not exist, the getbit operation will return 0.
15 setrange key offset value
Function Description: overwrites the string value stored in the given key with the value parameter, starting from offset
the returned result is as follows: length of the modified string
Note:
1) time complexity: O (1) for small strings ). (For details about what string is "small", refer to the APPEND Command) otherwise it is O (M) and M is the length of the value parameter
2) the key does not exist, the original value is treated as a blank string.
3) The setrange command ensures that the string is long enough to set the value to the specified offset, if the length of the string originally stored in the given key is smaller than the offset (for example, the string is only five characters long, but the offset you set is 10 ), then the blank space between the original character and offset will be filled with zero bytes (zerobytes, "\ x00");
4) note that the maximum offset you can use is 2 ^ 29-1 (536870911), because the redis string size is limited to 512 MB (megabytes. If you need more space than this, you can use multiple keys;
5) when generating a long string, redis needs to allocate memory space, this operation may sometimes cause server blocking ). On the 2010 MacBook Pro, setting the offset to 536870911 (512 MB memory allocation) takes about 300 milliseconds, and setting the offset to 134217728 (128 MB memory allocation) takes about 80 milliseconds, set the offset to 33554432 (32 MB memory allocation), which takes about 30 milliseconds. Set the offset to 8388608 (8 MB memory allocation), which takes about 8 milliseconds. Note that if the initial memory allocation is successful, you do not need to re-execute the setrange operation on the same key.
16 GetrangeKey start end
  Function Description: truncates the substring of the string value corresponding to the key.
Return result: the intercepted substring.
Note:
1) time complexity: O (N) (N indicates the length of the string to be returned)
2) The string truncation range is determined by the offset of start and end (including start and end );
3) The negative offset indicates that the string is counted from the end,-1 indicates the last character,-2 indicates the second to the end, and so on;
4) getrange ensures that the range of the sub-string does not exceed the value range of the actual string to process requests that exceed the range;
5) in <= 2.0, getrange is called substr.
17 MsetKey value [key value...]
  Function Description: sets one or more key-value pairs at the same time.
Returned results: Always Returns OK (because mset cannot fail)
Note:
1) time complexity: O (N) (N indicates the number of key-value pairs)
2) if a given key already exists, mset overwrites the old value with the new value. If this is not the expected result, use the msetnx command: it will only set if all given keys do not exist;
3) mset is an atomic operation. All given keys are set at the same time. Some given keys are updated while others are not changed, impossible.
18 MsetnxKey value [key value...]
  Function Description: sets one or more key-value pairs at the same time. if and only if none of the given keys exist
Returned results: 1 is returned for success, and 0 is returned for failure.
Note:
1) time complexity: O (N) (N indicates the number of key-value pairs)
2) even if only one given key already exists, msetnx rejects the setting operation for all given keys;
3) msetnx is atomic, so it can be used to set multiple unique logic objects with different keys to indicate different fields. All fields are either set completely, or none.
19 MgetKey [Key...]
  Function Description: returns the values of all (one or more) specified keys.
Returned result: a list containing the values of all given keys
Note:
1) time complexity: O (N) (N indicates the number of key-value pairs)
2) if a given key does not exist, the key returns a special value nil. Therefore, this command never fails.

String data type of redis Study Notes

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.