Answering my phone, I canβt format the answer correctly.
It may be a late answer, but I stick with it here just in case anyone still needs this functionality.
Short answer
ltrim mylist 0 - (n + 1) where mylist is the key and n is the length of the list.
Long answer
The way ltrim works is that it takes two indexes and returns the elements that fall between them, including indexes.
List of subscribers startIndex endIndex
Suppose we have a redis list with a key list containing 10 entries:
ltrim mylist 0 5 will clip the list to items starting at index 0 through index 5. And discard those that fall outside this range.
Fortunately, redis list operations support negative indexing, which is extremely useful in some situations. Usually when you do not know the length of the list.
-1 refers to the last element, - 2 to the last but one element, etc. And (-n) is the first element.
Out of range indicators are not harmful. If the ending index is greater than the length of the list, redis treats it as equal to the last index.
This is why ltrim mylist 0, - (n +1) clears the list. He does this because (-n) is equivalent to index 0. Adding 1 to it does not leave a single element in this range, since it will be before the first element.
The-null-Pointer- Jul 27. '17 at 12:14 2017-07-27 12:14
source share