看到一个问题, 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:
- One using a simple string keys and values.
- key:user, value:payload (the entire JSON blob which can be 100-200 KB)
- SET user:1 payload
- Using hashes
- HSET user:1 username "someone"
- HSET user:1 location "NY"
- HSET user:1 bio "STRING WITH OVER 100 lines"
Keep in mind that if I use a hash, the value length isn't predictable. They're not all short such as the bio example above.
Which is more memory efficient? Using string keys and values, or using a hash?
string 和 hash 直观测试
首先我们先测试用数据测试一下,测试数据结构如下:
values = { "name": "gs", "age": 1 }
使用for 生成10w个key,key的生成规则为: