redis之Hash哈希类型以及存储原理

1.概述

1.实际上主要是对一个对象的多重属性(如人的姓名,性别,年龄)的存储;

在这里插入图片描述

同样是存储字符串,Hash 与String 的主要区别?
1、把所有相关的值聚集到一个key 中,节省内存空间
2、只使用一个key,减少key 冲突
3、当需要批量获取值的时候,只需要使用一个命令,减少内存/IO/CPU 的消耗

1.1Redis数据类型 中文官网 (不推荐,更新不及时)

http://www.redis.cn/topics/data-types-intro

1.2.Redis数据类型 英文官网 (推荐)

https://redis.io/topics/data-types-intro

2.Hash哈希类型的相关命令

2.1.命令参考地址:http://redisdoc.com/hash/hexists.html

2.2.设置key的单个field属性值:hset gaoxinfu en_name frank

127.0.0.1:6379> hset gaoxinfu en_name frank
(integer) 1

已经存在的key,我们可以通过上面的命令继续设置属性值,比如我再设置一下年龄

127.0.0.1:6379> hset gaoxinfu age 18
(integer) 1

2.3.获取key的单个field属性值:hget gaoxinfu en_name

127.0.0.1:6379> hget gaoxinfu en_name
"frank"

2.4.设置key的多个field属性值:hmset zhaobolun en_name brain age 8 sex m

设置zhaobolun这个人的英文名,年龄和性别等信息

127.0.0.1:6379> hmset zhaobolun en_name brain age 8 sex m
OK

2.5.获取key的多个field属性值:hget gaoxinfu en_name

127.0.0.1:6379> hmget zhaobolun en_name age sex
1) "brain"
2) "8"
3) "m"

2.6.获取key的多个field属性字段:hkeys zhaobolun

127.0.0.1:6379> hkeys zhaobolun
1) "en_name"
2) "age"
3) "sex"

2.7.获取key的多个field属性字段对应的value:hkeys zhaobolun

127.0.0.1:6379> HVALS zhaobolun
1) "brain"
2) "8"
3) "m"

2.8.获取key的多个field属性字段以及对应的value:HGETALL zhaobolun

127.0.0.1:6379> HGETALL zhaobolun
1) "en_name"
2) "brain"
3) "age"
4) "8"
5) "sex"
6) "m"
127.0.0.1:6379> 

2.9.获取key的field属性个数:HLEN zhaobolun

127.0.0.1:6379> HLEN zhaobolun
(integer) 3
127.0.0.1:6379> 

2.10.获取key的field,并加上N值:HINCRBY zhaobolun age 2

127.0.0.1:6379> HKEYS zhaobolun
1) "en_name"
2) "age"
3) "sex"
127.0.0.1:6379> HGET zhaobolun  age
"8"
127.0.0.1:6379> HINCRBY zhaobolun age 2
(integer) 10
127.0.0.1:6379> 

2.11.获取key的field,并减掉N值:HINCRBY zhaobolun age -2

127.0.0.1:6379> HINCRBY zhaobolun age -2
(integer) 8

2.12.删除key对应的单个属性:HDEL zhaobolun grade

127.0.0.1:6379> hset zhaobolun grade 3
(integer) 1
127.0.0.1:6379> hgetall zhaobolun
1) "en_name"
2) "brain"
3) "age"
4) "8"
5) "sex"
6) "m"
7) "grade"
8) "3"
127.0.0.1:6379> HDEL zhaobolun grade 
(integer) 1
127.0.0.1:6379> 

2.13.删除key对应的多个属性:HDEL zhaobolun sex age

127.0.0.1:6379> HDEL zhaobolun sex age
(integer) 2
127.0.0.1:6379> 

2.14.删除key:DEL zhaobolun

127.0.0.1:6379> DEL zhaobolun
(integer) 1
127.0.0.1:6379> 

2.15.查看数据类型:type gaoxinfu

127.0.0.1:6379> hset gaoxinfu en_nmae frank sex m age 18
(integer) 2
127.0.0.1:6379> type gaoxinfu
hash
127.0.0.1:6379> 

3.Hash哈希类型存储操作原理(略)

3.1.概述

1.首先我们redis的存储结构就是Hash,本身也是一个KV 的结构,类似于Java中的HashMap,我们叫外层的哈希(Redis KV的实现),只用到了 hashtable
2.当存储 hash 数据类型时, 我们把它叫做内层的哈希。
  内层的哈希底层可以使用两种数据结构实现:
  一种:ziplist 即为OBJ_ENCODING_ZIPLIST(压缩列表),
  一种:hashtable 即为OBJ_ENCODING_HT(哈希表),

3.2.ziplist类型数据结构

3.2.1.ziplist概述(源码 ziplist.c)

/* The ziplist is a specially encoded dually linked list that is designed
 * to be very memory efficient. It stores both strings and integer values,
 * where integers are encoded as actual integers instead of a series of
 * characters. It allows push and pop operations on either side of the list
 * in O(1) time. However, because every operation requires a reallocation of
 * the memory used by the ziplist, the actual complexity is related to the
 * amount of memory used by the ziplist.
 */
 ziplist 是一个经过特殊编码的双向链表,它不存储指向上一个链表节点和指向下一个链表节点的指针,而是存储上一个节点长度和当前节点长度,
 通过牺牲部分读写性能,来换取高效的内存空间利用率,是一种时间换空间的思想。只用在字段个数少,字段值小的场景里面。

在这里插入图片描述

3.2.2.ziplist 数据结构整体布局
 *
 * The general layout of the ziplist is as follows:
 *
 * <zlbytes> <zltail> <zllen> <entry> <entry> ... <entry> <zlend>
 *
 * NOTE: all fields are stored in little endian, if not specified otherwise.
 *
 * <uint32_t zlbytes> is an unsigned integer to hold the number of bytes that
 * the ziplist occupies, including the four bytes of the zlbytes field itself.
 * This value needs to be stored to be able to resize the entire structure
 * without the need to traverse it first.
 *
 * <uint32_t zltail> is the offset to the last entry in the list. This allows
 * a pop operation on the far side of the list without the need for full
 * traversal.
 *
 * <uint16_t zllen> is the number of entries. When there are more than
 * 2^16-2 entries, this value is set to 2^16-1 and we need to traverse the
 * entire list to know how many items it holds.
 *
 * <uint8_t zlend> is a special entry representing the end of the ziplist.
 * Is encoded as a single byte equal to 255. No other normal entry starts
 * with a byte set to the value of 255.

3.3.hashtable类型数据结构

4.Hash哈希类型应用场景

4.1.String字符串类型可以做的,Hash类型都可以做

4.2.存储对象类型数据,便于管理

1.比如以前,我们存储某个对象,可能是一个表,但是现在一个key,然后不同的feild即可存储
2.Hash类型比String类型能够节省更多的key空间(以前一个对象,可能好多key去存储,现在只需要一个),也更加便于集中管理

4.3.购物车

在这里插入图片描述

5.Hash哈希类型不适合的应用场景

1、Field 不能单独设置过期时间
2、没有bit 操作
3、需要考虑数据量分布的问题(value 值非常大的时候,无法分布到多个节点)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山富哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值