2.Redis data type String type and hash type

Source: Internet
Author: User
Tags redis

9, Redis data type

1) String type and operation

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

Set: Set key corresponding to value of string type

For example: We add a Name=lizh key value pair

127.0.0.1:6379>set name Lizh

Ok

SETNX: Set key corresponding to value of string type, if key already exists, does not update data and returns 0, if key does not exist, insert data and return 1. NX is not the meaning of exist.

127.0.0.1:6379> setnx name Llll

(integer) 0

Setex: Sets the key corresponding to the value of string type and specifies the validity period for this key value pair

For example, add a haircolor=red key value pair and specify a validity period of 10 seconds.

Setex Haircolor Red

Ok

Get Haircolor

"Red"

10 seconds to get the data

Get Haircolor

(nil)

Setrange: Sets the substring of the value value of the specified key, similar to a replacement string

For example: Replace the Xiaoli 126 mailbox with a Gmail mailbox

Mset: Set the value of multiple keys at a time, successful return OK means that all values are set, and failure to return 0 means that no values are set.

MSETNX: Set multiple key values at a time, successful return OK means all values are set, failure returns 0 means no values are set, but does not overwrite existing key

Get: Gets the string value corresponding to key if key does not exist returns nil

Getset: Sets the value of the key and returns the old value of the key

GetRange: Gets the substring of the value of the key

Mget: Gets the value of multiple keys at a time, and returns nil if the corresponding key does not exist

INCR: The value of the key to do Gaga operation, and return a new value

Incrby: Similar to INCR, plus the specified value, key does not exist when the key will be set, and that the original value of 0

DECR: The value of the key to do subtraction operations

Decrby: Similar to DECR, minus the specified value.

Append: Appends value to the string of the specified key, returning the length of the new string value

Strlen: Takes the length of the value of the specified key

2) Hash type

Hash type and operation

The Redis hash is a String type field and value mapping table, and its add delete operation is 0 (1) (average), and the hash is especially good for storing objects. Gencun each character of an object to a single string type. The presence of an object in a hash type consumes less memory and makes it easier to access the entire object.

Hset: Sets the hash field to the specified value, and if the key does not exist, it is created first.

Where user:001 can be seen as a record of a table

HSETNX: Sets the hash field to the specified value, and if the key does not exist, it is created first. Returns 0 if present.

Hmset: Set multiple field of hash at the same time

Hincrby: The specified hash field plus the given value

Hexists: Test whether the specified field exists

Hlen: Returns the field number of the specified hash

Hdel: Delete field for specified hash

Hkeys: Returns all field of hash

Hvals: Returns all value of hash

Hgetall: Gets all the field and value in a hash

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.