Highly available Redis (ii): hash type

Source: Internet
Author: User

1. Hash type key-value structure

The hash type is also a key-value structure, and key is a string type whose value is divided into two parts: field and Value
Where the field section represents the property, and value represents the property corresponding to the

In the above figure, user:1:info for key, name,age,Date some properties of the user key, value is the value of the property.

In the hash, you can add a new attribute and a new value to the key

For example, use the following command to add a new attribute to the User:1:info key Viewcounter, the value of the property corresponds to 100

hset user:1:info viewCounter 100
2. Features
key-value结构key(field)不能相同,value可以相同
The 3.Redis hash type corresponds to the command 3.1 hget command, Hset command, and Hdel command
hget key field          获取hash key对应的field的valuehset key field value    设置hash key对应的field的valuehdel key field          删除hash key对应的field的value

Example:

Precautions:

hget命令,hset命令和hdel命令的时间复杂度为O(1)
3.2 hexists Command and Hlen command
hexists key field   判断hash key是否有fieldhlen key            获取hash key field的数量

Example:

127.0.0.1:6379> hgetall user:1:info1) "name"2) "python"3) "age"4) "23"127.0.0.1:6379> hexists user:1:info name(integer) 1127.0.0.1:6379> hlen user:1:info(integer) 2

Precautions:

hexists命令和hlen命令的时间复杂度为O(1)
3.3 hmget Command and Hmset command
hmget key field1 field2 ... fieldN                          批量获取hash key的一批field对应的值hmset key field1 value1 field2 value2 ... fieldN valueN     批量设置hash key的一批field value

Example:

Precautions:

hmget命令和hmset命令的时间复杂度为O(1)
3.4 hgetall Command, hvals command and Hkeys command
hgetall key         返回hash key对应所有的field和valuehvals key           返回hash key对应所有field的valuehkeys key           返回hash key对应所有field

Example:

127.0.0.1:6379> hgetall user:2:info1) "age"2) "30"3) "name"4) "mysql"5) "page"6) "50"127.0.0.1:6379> hvals user:2:info1) "30"2) "mysql"3) "50"127.0.0.1:6379> hkeys user:2:info1) "age"2) "name"3) "page"

Precautions:

hgetall命令,hvals命令和hkeys命令的时间复杂度为O(1)由于Redis的单线程的特点以及hgetall会返回所有的key和value,所以如果hash中存储的数据过多时,hgetall命令的执行速度会比较慢
3.5 hsetnx Command, Hincrby command and Hincrbyfloat command
hsetnx key field value                  设置hash key对应field的value(如field已经存在,则失败)hincrby key field intCounter            hash key对应的field的value自增intCounterhincrbyfloat key field floatCounter     

Precautions:

hsetnx命令,hincrby命令和hincrbyfloat命令的时间复杂度为O(1)
4. Actual combat

Record the number of visits per user's personal page of a site, or use a hash type
This ensures that the relevant data for each user is a whole, and that by using a string type, the amount of traffic per user's personal home face is a separate state from each user's other data.

Record the number of visits per user's personal page needs to be designed and used according to the actual situation

hincrby user:1:info pageview count
5. Using Redis to save each user-related data, you can use three ways 5.1 way One

Using the user's ID as key, the user's relevant data is serialized and as value

When using, according to the user ID to obtain the corresponding data of the string format, after deserialization can get user-related data to query and update operations

5.2 Way Two

Use the user's ID and the user's corresponding property name to splice to get the new string, and as a key, the value of the user's corresponding property as value

In this way, all of the user's information is separated from each other, it is convenient to query and update the user's data, and can easily add new properties for the user, without having an effect on the original attribute

5.3 Way Three

Use the user ID as key, the user's other data is saved in the hash format,

It is convenient to query, update and add user's attributes.

How to save user-related data in comparison

Highly available Redis (ii): 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.