Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit list length in redis

Tags:

database

redis

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.

I'm using lpush to add items to a list and lrange to get the most recent 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.

Thank you!

like image 610
dzm Avatar asked Aug 21 '12 17:08

dzm


People also ask

How do I create a list in Redis?

Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush command or the tail (the “right” end) with rpush . You can also use lpush or rpush to create a new list: lpush key value.

How do I clear a list in Redis?

Delete the key, and that will clear all items. Not having the list at all is similar to not having any items in it. Redis will not throw any exceptions when you try to access a non-existent key.


1 Answers

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

See http://redis.io/commands/ltrim

like image 131
Sripathi Krishnan Avatar answered Oct 03 '22 07:10

Sripathi Krishnan