gpt4 book ai didi

memory - 对象的 Redis 编码和大小影响

转载 作者:IT王子 更新时间:2023-10-29 06:06:10 25 4
gpt4 key购买 nike

当我将一些对象写入 redis 时,我可以获得不同的内存使用统计信息。我想了解这是如何发生的。

简单的例子:

127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> MEMORY usage a
(integer) 49
127.0.0.1:6379> set a "1"
OK
127.0.0.1:6379> MEMORY usage a
(integer) 49
127.0.0.1:6379> set a \x01 <<<< Message packed of number 1
OK
127.0.0.1:6379> MEMORY usage a
(integer) 55
127.0.0.1:6379> set a '\x01' <<<< Message pack of number 1 but added ''
OK
127.0.0.1:6379> MEMORY usage a
(integer) 55
127.0.0.1:6379> set a "\x01" <<<<< Message pack of number 1 but added ""
OK
127.0.0.1:6379> MEMORY usage a
(integer) 52

和每次编码(作为 embstr):

127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f9858217ba0 refcount:2147483647 encoding:int serializedlength:2 lru:11691925 lru_seconds_idle:222
127.0.0.1:6379> set a "1"
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f9858217ba0 refcount:2147483647 encoding:int serializedlength:2 lru:11692152 lru_seconds_idle:1
127.0.0.1:6379> set a \x01
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f98563a1398 refcount:1 encoding:embstr serializedlength:5 lru:11692162 lru_seconds_idle:0
127.0.0.1:6379> set a '\x01'
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f98563a13e0 refcount:1 encoding:embstr serializedlength:5 lru:11692168 lru_seconds_idle:1
127.0.0.1:6379> set a "\x01"
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f98563a1788 refcount:1 encoding:embstr serializedlength:2 lru:11692177 lru_seconds_idle:1

有点复杂的值(带数字的字符串)

127.0.0.1:6379> set a "abc123"
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f985821b260 refcount:1 encoding:embstr serializedlength:7 lru:11692243 lru_seconds_idle:3
127.0.0.1:6379> MEMORY usage a
(integer) 57
127.0.0.1:6379> set a \xa6abc123
OK
127.0.0.1:6379> MEMORY usage a
(integer) 61
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f985821b320 refcount:1 encoding:embstr serializedlength:11 lru:11692273 lru_seconds_idle:9
127.0.0.1:6379> set a "\xa6abc123"
OK
127.0.0.1:6379> MEMORY usage a
(integer) 58
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f985821b260 refcount:1 encoding:embstr serializedlength:8 lru:11692291 lru_seconds_idle:11
127.0.0.1:6379>

和一个大的 JSON 字符串(然后消息打包),编码是“原始”

127.0.0.1:6379> set a '[{"id":1,"first_name":"Kyrstin","last_name":"Ifill","email":"kifill0@livejournal.com","count":93,"ip_address":"182.218.153.253"}]'
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f98582b96f0 refcount:1 encoding:raw serializedlength:128 lru:11692490 lru_seconds_idle:2
127.0.0.1:6379> MEMORY usage a
(integer) 182
127.0.0.1:6379> set a '\x91\x86\xa5count]\xaafirst_name\xa7Kyrstin\xa9last_name\xa5Ifill\xa5email\xb7kifill0@livejournal.com\xaaip_address\xaf182.218.153.253\xa2id\x01'
OK
127.0.0.1:6379> MEMORY usage a
(integer) 197
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f98582b9700 refcount:1 encoding:raw serializedlength:143 lru:11692517 lru_seconds_idle:6
127.0.0.1:6379> set a "\x91\x86\xa5count]\xaafirst_name\xa7Kyrstin\xa9last_name\xa5Ifill\xa5email\xb7kifill0@livejournal.com\xaaip_address\xaf182.218.153.253\xa2id\x01"
OK
127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f98582b96f0 refcount:1 encoding:raw serializedlength:107 lru:11692535 lru_seconds_idle:2
127.0.0.1:6379> MEMORY usage a
(integer) 158

最佳答案

此答案基于 64 位机器上的 Redis 4.0

为了提高内存效率,Redis 采用三种方法对值字符串进行编码:

  1. Int:如果值字符串可以转换为整数,例如-2^63 ~ 2^63,Redis将该值保存为整数。这是最有效的编码。
  2. Embeded String:如果值字符串的大小小于等于44字节,Redis会将字符串保存在同一个字符串中Redis 对象本身的 block 。这比 Raw String 编码的内存效率更高。此外,它对缓存更友好。检查this因为这个原因。
  3. Raw String:否则,Redis 使用原始编码。

在你的情况下:

  1. set a 1 and set a "1": 该值为可以转换为1的字符串。所以 Redis 使用 Int 编码,即 encoding:int
  2. set a\x01 and set a '\x01': 值为4个字符的字符串(注意:因为字符串被单引号括起来,不会被转义)。它不能转换为整数,并且其大小小于 44。所以 Redis 使用 Embeded String 编码,即 encoding:embstr
  3. 设置一个“\x01”:由于字符串被双引号括起来,所以该值被转义为二进制字符串,其长度,即1字节,是小于44,不能转为整数。所以 Redis 使用 Embeded String 编码,即 encoding:embstr
  4. 设置一个大的json字符串:值为大于44的字符串。所以 Redis 使用 Raw String 编码,即 encoding:raw

关于memory - 对象的 Redis 编码和大小影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49408862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com