database 限制 redis 中的列表长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12060004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit list length in redis
提问by dzm
I'm using redis lists and pushing to new items to a list. The problem is I really only need the most recent 10 items in a list.
我正在使用 redis 列表并将新项目推送到列表。问题是我真的只需要列表中最近的 10 个项目。
I'm using lpushto add items to a list and lrangeto get the most recent 10.
我正在使用lpush将项目添加到列表并lrange获取最近的 10 个。
Is there anyway to drop items after a certain number? I'll end up with lists that may have 1,000's of items and can cause performance issues with latency.
有没有在一定数量后丢弃物品?我最终会得到可能有 1,000 个项目的列表,并且可能会导致延迟的性能问题。
Thank you!
谢谢!
回答by Sripathi Krishnan
After every lpush, call ltrimto trim the list to 10 elements
在 every 之后lpush,调用ltrim将列表修剪为 10 个元素
回答by DhruvPathak
You can use LTRIMintermittentlyafter any LPUSH, no need to call LTRIM after every LPUSH as that would add to overall latency in your app( though redis is really fast, but you can save lots of LPUSH operations )
您可以在任何 LPUSH 之后间歇性地使用LTRIM,无需在每个 LPUSH 之后调用 LTRIM,因为这会增加应用程序的整体延迟(尽管 redis 非常快,但您可以节省大量 LPUSH 操作)
Here is a pseudo code to achieve an LTRIM on approximately every 5th LPUSH:
这是一个伪代码,用于在大约每 5 个 LPUSH 上实现 LTRIM:
LPUSH mylist 1
random_int = some random number between 1-5
if random_int == 1: # trim my list with 1/5 chance
LTRIM mylist 0 10
Though your list may grow to be a few elements more than 10 elements at times, but it will surely get truncated at regular intervals. This approach is good for most practical purposes and saves a lot of LTRIM operations, keeping your pushes fast.
尽管您的列表有时可能会增长到超过 10 个元素的几个元素,但它肯定会定期被截断。这种方法适用于大多数实际目的,并且可以节省大量 LTRIM 操作,从而保持快速推送。
回答by ovunccetin
The following code,
下面的代码,
- pushes the item to the list,
- keep the size fixed to 10,
- and returns the most recent 10 elements
- 将项目推送到列表中,
- 保持大小固定为 10,
- 并返回最近的 10 个元素
in a transaction.
在一次交易中。
MULTI
LPUSH list "item1"
LTRIM list 0 9
LRANGE list 0 9
EXEC
回答by wei tu
Just an alternative. According to official docof LPUSH, it returns the length of the list after the push operations. You can set a threshold length like k(in your case k > 10) and call LTRIMwhen returned length is bigger than k. Sample pseudo code as follows:
只是一个替代方案。根据官方文档的LPUSH,它返回的推操作后,列表的长度。您可以设置阈值长度k(在您的情况下为 k > 10)并LTRIM在返回长度大于 时调用k。示例伪代码如下:
len = LPUSH mylist xxx
if len > k:
LTRIM mylist 0 9
LRANGE mylist 0 9
It's more controllable than random method. Greater ktriggers less LTRIMbut with more memory cost. You can adjust kaccording to how often you want to call LTRIMsince calling extra command is more expensive.
它比随机方法更可控。更大的k触发更少,LTRIM但内存成本更高。您可以k根据您想要调用的频率进行调整,LTRIM因为调用额外命令的成本更高。
回答by beytarovski
Nobody gives exact solution for storing only most 10 recent items.
没有人给出仅存储最近 10 个项目的确切解决方案。
Let's create a sample list with 15 items (here just numbers):
让我们创建一个包含 15 个项目的示例列表(这里只是数字):
RPUSH list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Now indicate offset from the end of the list:
现在指出从列表末尾的偏移量:
LTRIM list -10 -1
Show list
显示列表
LRANGE list 0 -1
1) "6"
2) "7"
3) "8"
4) "9"
5) "10"
6) "11"
7) "12"
8) "13"
9) "14"
10) "15"
Now you can add new items and run trim:
现在您可以添加新项目并运行修剪:
RPUSH list 16
LTRIM list -10 -1
1) "7"
2) "8"
3) "9"
4) "10"
5) "11"
6) "12"
7) "13"
8) "14"
9) "15"
10) "16"

