Redis Tutorial (ii): String data type

Source: Internet
Author: User
Tags truncated redis server redis tutorial
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 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 reduction
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. Added value after
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. 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 the setting is 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.

Examples of commands:

1. Set/get/append/strlen:

  /> 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) One-to-one    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"    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:
Redis 127.0.0.1:6379> set MyKey     #设置Key的值为20    OK    redis 127.0.0.1:6379> incr mykey         #该Key的值递增1    (integer) redis    127.0.0.1:6379> decr mykey        #该Key的值递减1    (integer)    redis 127.0.0.1:6379 > del mykey          #删除已有键.    (integer) 1    redis 127.0.0.1:6379> decr mykey        #对空值执行递减操作, whose original value is set to 0, and 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        #对空值执行递增操作, its 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    OK    Redis 12 7.0.0.1:6379> Decrby MyKey 5     (integer) 5    redis 127.0.0.1:6379> incrby mykey    (integer) 15

3. Getset:

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

4. Setex:

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

5. Setnx:

    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 executed successfully.    (integer) 1    redis 127.0.0.1:6379> setnx mykey "World"       #该键已经存在, so this setting does not produce any effect.    (integer) 0    redis 127.0.0.1:6379> get MyKey                      #从结果可以看出, the value returned is still the value set for the first time.    "Hello"

6. Setrange/getrange:

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) Redis 127.0.0.1:6    379> get MyKey #查看替换后的值.    "Hello Ddrld" Redis 127.0.0.1:6379> setrange mykey dd #offset已经超过该Key原有值的长度了, the command will be 0 at the end.    (integer) Redis 127.0.0.1:6379> get MyKey #查看补0后替换的结果.     "Hello ddrld\x00\x00\x00\x00\x00\x00\x00\x00\x00dd" 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 #查看替换空值后的结果.    "\X00\X00DD" 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.    Redis 127.0.0.1:6379> getrange MyKey 1 #20已经超过Value的总长度, so all the bytes after the first byte are intercepted. "123456789 " 

7. Setbit/getbit:

  Redis 127.0.0.1:6379> del mykey    (integer) 1    redis 127.0.0.1:6379> setbit mykey 7 1       # Set the seventh bit value starting from 0 to 1, return the original bit value 0    (integer) 0    redis 127.0.0.1:6379> get MyKey                #获取设置的结果, binary 0000 The hexadecimal value of 0001 is 0x01    "\x01"    redis 127.0.0.1:6379> setbit mykey 6 1       #设置从0开始计算的第六位BIT值为1, returning the original bit value of 0    ( Integer) 0    redis 127.0.0.1:6379> get MyKey                #获取设置的结果, binary 0000 0011 hexadecimal value 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 return 0.    (integer) 0

8. Mset/mget/msetnx:

    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 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, returns nil because the Key5 is not set successfully.    redis 127.0.0.1:6379> mget key3 key5                       1) "Stephen"    2) (nil)

The above is the Redis tutorial (ii): The content of the string data type, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • 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.