Redis Research (iv)-hash type

Source: Internet
Author: User

The key value of a hash type (hash) is also a dictionary structure that stores mappings for field and field values, but the field value can only be a string and does not support other data types.

A hash type cannot nest other data types. A hash type key can contain a maximum of 2^32-1 fields.


First, Introduction

Hash types are suitable for storing objects: Use object categories and IDs to form key names, use fields to represent objects ' properties, and field values to store property values.

If you want to store car objects in relational data:

Data is stored in the form of a two-dimensional table, which requires that all records have the same attributes and cannot be added or subtracted from a single record. If you want to increase the production date attribute for a car with an ID of 1, you need to change the data table to

The data field is redundant for two records with IDs 2 and 3. The amount of data is much harder to maintain.

However, Redis is free to increment and decrement fields for any key without affecting other keys.


Second, the order

1. Assigning and taking values

Hset key field Valuehget key Fieldhmset key F1 v1 f2 v2......hmget key F1 F2.....hgetall key

Hset does not differentiate between insert and update operations, when an insert is performed (the previous field does not exist) Hset returns 1, and when the update operation is performed (the previous field already exists) Hset returns 0. Hset also automatically establishes the key when it does not exist.


The Hgetall command gets all the fields and field values in the key.


2. Determine if a field exists

Hexists key Field
The presence returns 1, does not exist return 0, and returns 0 if the key does not exist

3. Assign a value when the field does not exist

and Hset difference if the field already exists, the HSETNX command does nothing, atomic operations, without worrying about race conditions.

Hsetnx key field value


4. Increase the number

Hincrby key field Increment

5. Delete a field

Hdel Key F1 F2 ...


Third, practice
Store an article

The first method:


The second method:


The second is more intuitive and easier to maintain, saving space for storage.


Iv. Command Supplements

Hkeys key only gets field Hvals key Get field value Hlen key Get field number




Redis Research (iv)-hash 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.