I. redis string-type operations

Source: Internet
Author: User
Tags key string

* Use in redis client:


============== Method of the string type


Description: string is the simplest type of redis. A key corresponds to a value, and the string type is binary secure.
Redis string can contain any data, such as JPG images or serialized objects.


1: A key contains only one value. If the same value is set, = overwrites

Set Name: leyangjun
Get value: Get name

2: setnx: (determines whether the set value exists. If it does not exist, it is set)
Set the value corresponding to the key to a value of the string type. If the key already exists, 0 is returned. NX indicates not exist.

3: setex:
Set the value corresponding to the key to a value of the string type and specify the validity period of the key value.
Example: setex name 10 leyangjun --> set the name to leyangjun and save it for 10 seconds.
Get name --> 10 seconds or the like.
4: setrange: (replace the value. If there are two values, replace them with two. For example, the length and the previous digits will not be deleted)
String used to set the value of the specified key
Example: setrange name 6 gmail.com -->) replace the value of name with gmail.com

5: mset: Batch setting
Multiple key values are set at one time. If "OK" is returned successfully, all values are set. If "failure" is returned, 0 indicates no value is set.
Example: mset key1 leyangjun1 key2 leyangjun2
6: msetnx:
Set the values of multiple keys at a time. If "OK" is returned successfully, all values are set. If "0" is returned for failure, no values are set, but existing keys are not overwritten.
Example: msetnx key1 leyangjun3 key2 leyangjun2 --> if any of the keys exists, the setting will not succeed.
7: get:
Returns the string value corresponding to the key. If the key does not exist, Nil is returned.

8: GetSet:
Sets the key value and returns the old value of the key.
Example: GetSet key6 30 --> get the old value and set it to a new value
9: getrange
Obtains the value of a key string.
Example: getrange email 0 4 --> get the email value from 0-4 (leyan) [email protected],
10: mget: Batch Retrieval
Obtain the values of multiple keys at a time. If the corresponding key does not exist, the corresponding NIL is returned.
Example: mget key1 key2 key3...
11: incr auto-increment 1
Add the key value and return a new value --> If the key does not exist, the key is set and the original value is 0.
Example: incr key6 --> increment by 1
12: incrby
Similar to incr, if a specified value is added, the key is set when the key does not exist and the original value is regarded as 0.
Example: incrby key7 5 --> auto-increment 5 plus
Incrby key7-5 --> negative auto-increment minus
13: decr auto-Reduction
Subtraction of key values
Example: decr key6 --> key6-1 minus 1
14: decrby
Similar to decr, specify impairment
Example: decrby key6 3 --> key6-3
Decrby key6-3 --> adding 3 to the negative
15: append
Append value to the string of the specified key to return the length of the new string.
Example: append name. Net --> name = leyangjun after append leyangjun.net
16: strlen
Set the value length of the key.
Example: strlen name

================================================================ End = ========================================================== ==================
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.