小编use*_*273的帖子

当字符串超过7个字节时,字符串的Redis int表示更大,否则更小

我正在努力尽可能地减少Redis的物体尺寸,我花了整整一周的时间来试验它.

在测试不同的数据表示时,我发现字符串"hello"的int表示形成了一个较小的对象.它可能看起来不是很多,但是如果你有很多数据,它可以在使用几GB内存和几十GB内存之间产生差异.

请看以下示例(如果需要,您可以自己尝试):

> SET test:1 "hello"
> debug object test:1
> Value at:0xb6c9f380 refcount:1 encoding:raw serializedlength:6 lru:9535350 lru_seconds_idle:7
Run Code Online (Sandbox Code Playgroud)

特别是你应该看看serializedlength,在这种情况下是6(字节).

现在,看看它的以下int表示:

> SET test:2 "857715"
> debug object test:2
> Value at:0xb6c9f460 refcount:1 encoding:int serializedlength:5 lru:9535401 lru_seconds_idle:2
Run Code Online (Sandbox Code Playgroud)

如你所见,它导致一个字节更短的对象(注意还编码:int我认为这意味着以更有效的方式处理整数).

使用字符串"hello w"(稍后您将看到为什么我没有使用"hello world"),当它表示为int时,我们得到更大的节省:

> SET test:3 "hello w"
> SET test:4 "857715023" <- Int representation. Notice that I inserted a "0", if I don't, it results in a bigger object and the encoding is set to "raw" instead …
Run Code Online (Sandbox Code Playgroud)

int optimization redis

22
推荐指数
1
解决办法
1382
查看次数

Redis将多个项目添加到排序集

我正在关注这个Redis教程http://redis.io/topics/twitter-clone

在页面的最后他们说

注意:如果帖子列表开始非常大,并且我们想要访问列表中间的元素,则LRANGE效率不高,因为Redis列表由链表支持.如果系统设计用于对数百万个项目进行深度分页,则最好采用分类集.

这是我最担心的,因为我们正在设计一个系统,应该能够处理数百万甚至数十亿的帖子,我们估计一个项目将在列表中间搜索20-30%的web应用程序在跑.

排序集有一个问题..您可以添加如下所示的排序集,考虑到它是第一个(1)帖子

zadd zset 1 "We are happy to announce a new member in our team"
Run Code Online (Sandbox Code Playgroud)

但是如果我们还需要添加内容字段呢?

zadd zset 1 "We are happy to announce a new member in our team" "Please welcome James Sullivan, our new member team! He had..."
Run Code Online (Sandbox Code Playgroud)

这是你得到的错误

(error) ERR syntax error
Run Code Online (Sandbox Code Playgroud)

除了奇怪的事实,你必须访问的领域,如post[0]和post[1]而不是post['title']和post['content'](理论上,我还没有尝试过),也有这个事实,我们不能克服截至目前.

实际上post对象的结构是这样的,它应该在O(1)时间内找到,即使在百万个post对象的中间也是如此

+-----------+
+- 1 <- ID -+
+-----------+
+------------------------+
+- title "the title..." -+
+----------------------------+
+- content "the content..." -+
+-----------------------------+ …
Run Code Online (Sandbox Code Playgroud)

redis

7
推荐指数
1
解决办法
905
查看次数

标签 统计

redis ×2

int ×1

optimization ×1