Summary of common commands for hashing types in Redis _redis

Source: Internet
Author: User
Tags hash redis set time

Redis Hash Type

Redis is used to store data in the form of a key-value pair using a dictionary structure, the key value of a hash type (hash) is also a dictionary structure that stores the mapping of field and field values, but the field value can only be a string and no other data type is supported, that is, the hash type cannot nest other data types. A hash type key can contain up to 2^32-1 fields.

In addition to the hash type, data type nesting is not supported for other data types of Redis. For example, each element of a collection type can only be a string, not a collection or a hash list, and so on.

A hash type is suitable for storing objects: Using object classes and IDs to form a build name, using fields to represent the properties of an object, and field values to store property values. For example, to store a car object with ID 2, you can use the three fields named color, name, and price to store the car's colors, names, and prices, respectively.

1. Basic command

For example, now you want to store an article with ID 1, with title, author, time, content

The key is Post:1, and the fields are title, author, time, content, and the values are "the" the "the", "" "Me", "2014-03-04", "" "," "," "," ",", stored as follows

Redis 127.0.0.1:6379> hmset post:1 title "The" "The" "The" the "" Author "JoJo" Time 2016/08/25 content "This is my
OK

The Hmset command is used here, and the basic assignment commands for the hash are as follows:

hset key field value#例如hset post:2 title "second Post"

hget key field#例如hget post:2 title, get the title value of the post with ID 2

hmset key field value [field value ...]  #这个同上, Bulk deposit value

hmget key field [field ...] #批量取值, get the list

Cases:

Redis 127.0.0.1:6379> Hmget post:1 Time author 1 "2016/08/25"
2) "JoJo"

hgetall key#取得key所对应的所有键值列表, here's an example.

Redis 127.0.0.1:6379> hgetall post:1 1) "title" 2 "" The "" The "" The "" 3 ""
Author "
4)" JoJo "
5)" Time "
6" 2016/08/25 "
7" content "
8" "This Is my"

2, to determine whether there is

Hexists key Field

Returns 0 if there is a return of 1 (if the key does not exist and returns 0).

3, when the field does not exist to assign values

Hsetnx key field value

The hset difference is that if the field exists, the command will not do anything, but here's the difference is that the commands provided by Redis are atomic and do not produce data inconsistencies.

Cases:

Redis 127.0.0.1:6379> hexists post:1 time
(integer) 1  //judgment is Redis 127.0.0.1:6379> with Time field
Post:1 time 2016/08/26
(integer) 0  //does not exist, set time, the existence of the words returned 0, the value unchanged, the original value
Redis 127.0.0.1:6379> hget post:1 Time
' 2016/08/25 '
redis 127.0.0.1:6379> hsetnx post:1 Age
(integer) 1   //No age field exists, returns 1, and sets the Age field
Redis 127.0.0.1:6379> hget post:1 age
"23"

4, increase the number

Hincrby key Field number

This is incry similar to the command.

Cases:

Redis 127.0.0.1:6379> Hincrby post:1 age 2
(integer) 25

5, delete the field

Hdel key field [field ...]

Deletes a field, one or more, and the return value is the number of deleted fields.

6. Other Orders

hkeys key#获取字段名

hvals key#获取字段名

Examples are as follows:

Redis 127.0.0.1:6379> Hkeys post:1
1) "title"
2 "author" 3 "Time
"
4 "content"
5 "" Age "
Redis 127.0.0.1:6379> hvals post:1 1) "The" the "" The ""
2 "" JoJo "3" 2016/08/25 "4" "This is my
firs T post "
5" "25"

Finally, there is a command to get the number of fields:

Hlen Key

Returns the number of fields

Redis 127.0.0.1:6379> hlen post:1
(integer) 5

Summarize

The above is redis in the hash type commonly used in all the contents of the command, I hope to be able to learn or work to bring certain help, if you have questions you can message exchange.

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.