[Redis data structure] string type

Source: Internet
Author: User

This article describes how to use redis string commands. The string type is binary secure and can store any data type, images, objects, and so on. Syntax operations related to the string type are listed below:

1. How to add a string

Command: Set Add a String object Syntax: Set [Key] [value] example: Set Name wangzp Description: added a String object named name with the value of 'wangzp '; if a name object already exists, the original object will be overwritten.

2. How to obtain a string

Command: Get get the Value Syntax of a string object: Get [Key] example: Get Name Description: Get the value of the name object, the return value is 'wangzp'; if this object does not exist, then return NULL (nil)

3. Differences between setnx and set

The two have the same syntax. The former performs the set operation only when the key does not exist. The latter overwrites the key whether or not it already exists. So in actual use, you should determine whether to use set or setnx based on the application of the key. If the key is protected, we recommend that you use setnx, to overwrite the previous values with errors.

4. Add strings in batches

Command: mset add multiple string objects at a time Syntax: mset [key1 value1] [key2 value2]... [keyn valuen] example: mset name wangzp age 23 address Shanghai Description: one-time add name, age, address three string objects.

5. Obtain strings in batches

Command: mget get multiple string object values at a time Syntax: mget [key1] [key2]... [keyn] example: mget name age address Description: obtains values of the three string objects name, age, and address at a time.

6. Differences between mset and msetnx

The syntax is the same as set and setnx. However, when msetnx is used, if one of the string object keys exists, the setting fails. For example: name is an existing msetnx name wangzp age 23. If you run the preceding command, 0 is returned, indicating that the execution failed.

7. Reset string

Command: GetSet resets the string object (if the key exists, the old value is returned and the new value is set;) Syntax: GetSet key value Example 1: getSet name wangzp // assume that the value of name exists before wangzpold. Elaborate: Return wangzpold and set name to wangzp Example 2: GetSet name newwangzp // assume that name does not exist before Description: return nil, and set name to newwangzp. Summary: According to examples 1 and 2, you will find that GetSet is actually equivalent to the get and set operations.

8. Delete string

Command: del Delete String object Syntax: Del key example: del Name Description: delete name object

9. String-related auxiliary commands

In addition to the above basic addition, deletion, modification, and query operations, there are also some auxiliary commands: 1. String Value truncation, add operation command syntax: setrange [Key] [Starting index position] [replaced string] // returns the length of the new string. Example 1: setrange name 2 OK // assume that the previous name = wangzp is described as follows: 6 (waokzp) is returned; replace the value from the specified starting position. Command syntax: getrange [Key] [Starting index location] [ending index location] Example 2: getrange name 0 3 // assume that the previous name = wangzp states: Return Wang, the start position and end position are closed intervals, including the boundary position command syntax: append [Key] [value] Example 3: append name-23 // assume that the previous name = wangzp is described as follows: returns 9 (returns the length of the new string). The syntax command adds a string to the end of the string object. 2. String Value auto-increment and auto-increment operation command syntax: incr [Key] // auto-increment + 1 Example 1: incr age // assume age = 23 elaborate: return 24, that is, the value of age + 1. The value of the key object must be of the numerical type. Command syntax: incrby [Key] [step size] // specify step size to execute auto-increment Example 2: incrby age 5 // age = 23 elaborate: Return 28, that is, age + 5, if the step size is negative, it indicates the auto-subtraction operation command syntax: decr [Key] // auto-subtraction-1 Example 3: decr age // age = 23 Description: Return 22, that is, age-1 command syntax: decrby [Key] [step size] example 4: decrby age 5 // age = 23 Description: Return 18, that is, age-5, if the step size is negative at this time, it is equivalent to the auto-increment operation. 3. Get the string object length. command syntax: strlen [Key]

This article from the "Java program" blog, please be sure to keep this source http://793404905.blog.51cto.com/6179428/1547689

[Redis data structure] string type

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.