限制 redis 中的列表长度 [英] Limit list length in redis

查看:45
本文介绍了限制 redis 中的列表长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 redis 列表并将新项目推送到列表.问题是我真的只需要列表中最近的 10 个项目.

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.

我使用 lpush 将项目添加到列表中,并使用 lrange 获取最近的 10 个.

I'm using lpush to add items to a list and lrange to get the most recent 10.

有没有在一定数量后丢弃物品?我最终会得到可能包含 1,000 项的列表,并且可能会导致性能问题和延迟.

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.

谢谢!

推荐答案

每次lpush后,调用ltrim将列表修剪为10个元素

After every lpush, call ltrim to trim the list to 10 elements

http://redis.io/commands/ltrim

这篇关于限制 redis 中的列表长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆