Redis的hash相当于Java的HashMap,内部结构实现与HashMap一致,即数组+链表结构hash数据结构不过Redis的hash的值只能是字符串,rehash方式不一样,为了提高性能,Redis保留新旧两个hash结构,采用渐进式rehash策略,查询时会同事查询两个hash结构,在后续的定时任务中以及hash操作指令中,循序渐进将旧hash的内容迁移到xinhash中,直至完全取代旧hash。hash移除最后一个元素后会自动被删除,内存被回收。前面说到string适合存储用户信息,而hash结构也可以存储用户信息,不过是对每个字段单独存储,因此可以在查询时获取部分字段的信息,节省网络流量。因此就引出了这篇文章,存储结构体信息是用hash还是string?以下信息出自StackOverflow Redis strings vs Redis hashes to represent JSON: efficiency?
I want to store a JSON payload into redis. There's really 2 ways I can do this:
1. One using a simple stringkeysand values.
key:user, value:payload (the entire JSONblob which can be 100-200 KB)
SETuser:1 payload
2. Using hashes
HSET user:1 username "someone"
HSET user:1 location "NY"
HSET user:1 bio "STRING WITH OVER 100 lines"
Keepin mind that if I use a hash, the valuelength isn't predictable. They're notallshort such as the bio example above.
Which is more memory efficient? Usingstringkeysandvalues, orusing a hash?