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

同样是存储字符串,Hash 与String 的主要区别?
1、把所有相关的值聚集到一个key 中,节省内存空间
2、只使用一个key,减少key 冲突
3、当需要批量获取值的时候,只需要使用一个命令,减少内存/IO/CPU 的消耗
Redis数据类型 中文官网 (不推荐,更新不及时)
http://www.redis.cn/topics/data-types-intro
Redis数据类型 英文官网 (推荐)
https://redis.io/topics/data-types-intro
Hash哈希类型的相关命令
1、命令参考地址:
http://redisdoc.com/hash/hexists.html
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
3、获取key的单个field属性值:hget gaoxinfu en_name
127.0.0.1:6379> hget gaoxinfu en_name
"frank"
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
5、获取key的多个field属性值:hget gaoxinfu en_name
127.0.0.1:6379> hmget zhaobolun en_name age sex
1) "brain"
2) "8"
3) "m"
6、获取key的多个field属性字段:hkeys zhaobolun
127.0.0.1:6379> hkeys zhaobolun
1) "en_name"
2) "age"
3) "sex"
7、获取key的多个field属性字段对应的value:hkeys zhaobolun
127.0.0.1:6379> HVALS zhaobolun
1) "brain"
2) "8"
3) "m"
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>
9、获取key的field属性个数:HLEN zhaobolun
127.0.0.1:6379> HLEN zhaobolun
(integer) 3
127.0.0.1:6379>
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>
11、获取key的field,并减掉N值:HINCRBY zhaobolun age -2
127.0.0.1:6379> HINCRBY zhaobolun age -2
(integer) 8
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>
13、删除key对应的多个属性:HDEL zhaobolun sex age
127.0.0.1:6379> HDEL zhaobolun sex age
(integer) 2
127.0.0.1:6379>
14、删除key:DEL zhaobolun
127.0.0.1:6379> DEL zhaobolun
(integer) 1
127.0.0.1:6379>
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>
Hash哈希类型存储操作原理(略)
1、概述
1、首先我们redis的存储结构就是Hash,本身也是一个KV 的结构,类似于Java中的HashMap,我们叫外层的哈希(Redis KV的实现),只用到了 hashtable
2、当存储 hash 数据类型时, 我们把它叫做内层的哈希。
内层的哈希底层可以使用两种数据结构实现:
一种:ziplist 即为OBJ_ENCODING_ZIPLIST(压缩列表)
一种:hashtable 即为OBJ_ENCODING_HT(哈希表)
2、ziplist类型数据结构
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 是一个经过特殊编码的双向链表,它不存储指向上一个链表节点和指向下一个链表节点的指针,而是存储上一个节点长度和当前节点长度,
通过牺牲部分读写性能,来换取高效的内存空间利用率,是一种时间换空间的思想。只用在字段个数少,字段值小的场景里面。

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.
hashtable类型数据结构
哈希表
下述代码对应 dict.h 中的 dictht
/*
* 哈希表结构
* 每个字典都使用两个哈希表,从而实现渐进式 rehash 。
*/
typedef struct dictht {
// 哈希表数组
dictEntry **table;
// 哈希表的大小
unsigned long size;
// 哈希表的大小掩码(总是 size-1)
unsigned long sizemask;
// 哈希表当前的已有结点数量
unsigned long used;
} dictht;
哈希表结点
下述代码对应 dict.h 中的 dictEntry
typedef struct dictEntry {
// 键
void *key;
// 值
union {
void *val;
uint64_t u64;
int64_t s64;
double d;
} v;
// 指向下一个哈希表结点的指针
struct dictEntry *next;
} dictEntry;

Hash哈希类型应用场景
1、String字符串类型可以做的,Hash类型都可以做
2、存储对象类型数据,便于管理
1、比如以前,我们存储某个对象,可能是一个表,但是现在一个key,然后不同的feild即可存储
2、Hash类型比String类型能够节省更多的key空间(以前一个对象,可能好多key去存储,现在只需要一个),也更加便于集中管理
3、购物车

Hash哈希类型不适合的应用场景
1、Field 不能单独设置过期时间
2、没有bit 操作
3、需要考虑数据量分布的问题(value 值非常大的时候,无法分布到多个节点)