Redis string and hash type operations

Source: Internet
Author: User

Data types for Redis

String type and operation

String is the simplest type, and a key corresponding to a value,sting type is binary safe. A Redis string can contain any data, such as a JPG image or a serialized object.


Example:

Set name Gxj

Get Name

Output: GXJ


SETNX: Sets the value of the key corresponding to string type value, and if key already exists, returns 0,NX is the meaning of not exist.


Example:

Set name ZJZ

Enter: Returns 0, if not present, sets the value


Setex: Sets the value of the key corresponding to string, and specifies the validity period for this key value, in S

Example: Setex name ZJZ, this setting overrides the same key set previously, but expires after 10 seconds, returning nil



SETRANGE: Sets the substring of the value of the specified key


Set email [email protected]

SetRange Eamil 4 qq.com

Output: [email protected]



Mset sets the value of multiple keys at once, and succeeds returns OK, indicating that all values are set, and failure returns 0 to indicate that no value is set

Example:

Mset key1 K1 key2 K2 Key3 K3

Mget Key1 Key2 Key3

return: K1

K2

K3


MSETNX: Setting the value of multiple keys at once, success returns OK, indicating that all values are set, failure returns 0, that no value is set, but does not overwrite the previously set key

such as the original existence Key3

Now set

Msetnx Key8 8 Key9 9 Key3 3

Returns 0, the Key8 also does not exist, and the Key3 value is still the previous value


GetRange gets the substring of the value of key

GetRange Email 0 5 Get email 0 to 5 characters



INCR 1 Increase in delivery

Set num 1

incr num Output 2



Incrby Num 5 each plus 5, if negative, each minus


Decr num minus 1 per time

Decrby Num 5 minus 5 at a time, plus 5 for each negative


Append email ' test ' append test to the value of email


Strlen email Specifies the string length of the value of the key





Hash data type

Redis Hash is a string-type field and value mapping table, and its addition and deletion operations, both 0 and 1,hash, are particularly suitable for storing objects, compared to Gencun each word of an object into a single string type, storing an object in a hash, Consumes less memory space and makes it easier to store the entire object.


Hset user001 name ZJZ

Hset user001 Age 1

Hget user001 name Output: ZJZ


HSETNX: Set hash filed to the specified value, if key does not exist, create, otherwise return 0


Hmset,hmget Batch setup, getting


Hincrby user001 age 21 times increased by 2 years, negative decreases


Hexists user001 age See if age exists


Hlen user001 Returns the number of keys set by the hash object


Hdel user001 Age Delete key


Hkeys user001 returns all fields in the hash table


Hvals user001 returns all the value in the hash table


Hgetall user001 returns all keys and values in the hash table


Redis string and hash type operations

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.