REDIS data structures and internal encodings-hash (hash)

Source: Internet
Author: User
Tags data structures hash redis
I. Concept

In Redis, there is also a hash type for the structure of key-value pairs like other languages, and the key value of a redis hash type is itself a key-value pair structure.

Such as:

Key

value

User:1

Key

value

Code

01

Name

Shl

User:2

Key

value

Code

02

Name

Zyc

two. Command 2.1 set the value

Command: Hset key field value

Example: Hset user:1 name SHL

Return: 0 2.2 Batch set value

Command: Hmset key field1 value1 field2 value2 ...

For example:

Hmset user:1 name SHL Age 32

Returns OK 2.3 Get value

Command: Hget key field

Example: Hset user:1 name SHL

Hget user:1 Name

Back to SHL

2.4 Obtaining values in bulk

Command: Hmget key field1 Field2

Example: Hmset user:1 name SHL Age 32

Hmget user:1 name Age

Back to: SHL

32

2.5 Delete Field

Command: Hdel key field

Example: Hset user:2 name Zyc

Hdel user:2 Name

Returns: 1 indicates the number of Delete Success 2.6 calculated field

Command: Hlen key

Example: Hmset user:3 name SHL age Tianjin

Hlen User:3

Return: 3 2.7 Determine if field exists

Command: Hexists key field, containing return 1, does not contain return 0.

For example Hset user:2 name Zyc

Hexistsuser:2 Name

Return: 1 2.8 gets all field of the specified key

Command: Hkeys key gets all field of the specified key

Example: Hmset user:4 name SHL Age 32

Hkeys User:4

Return: Name

Age 2.9 Gets all the value of the specified key

Command: Hvals key

Example: Hmset user:4 name SHL Age 32

Hvals User:4

Back to: SHL

2.10 Get all field-value for the specified key

Command: Hgetall key

Example: Hmset user:4 name SHL Age 32

Hgetall User:4

Return: Name

Shl

Age

32

2.11 Specifies the count of the value of field.

Command:

(1) Hincrby key field

Example: Hset user:1 Age 32

Hincrby User:1 age 10

Returns: 42

(2) Hincrbyfloat key field

Example: Hset user:1 Age 32

Hincrbyfloat user:1 age-8.1

Returns: 23.9

2.12 computes the string length of the contents of the specified field's value

Command: Hstrlen key field

Example: Hset user:1 name SHL

Hstrlen user:1 Name

Returns: 3 three. Time complexity of the hash type command

Four. Internal code

There are two kinds of internal encodings for hash types:

(1) ziplist (compression list)

When the number of elements of the hash type is less than the Hash-max-ziplist-entries configuration (default 512), and all values are less than the Hash-maxziplist-value configuration (default is 64 bytes), Redis uses ziplist as the internal implementation of the hash. Ziplist can use a more compact structure to achieve continuous storage of multiple elements, so it is better to save memory.

(2) Hashtable (hash table)

When the hash type fails to meet the ziplist requirements, Redis uses Hashtable as the internal implementation of the hash, because the read and write efficiency of ziplist is reduced

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.