We'd like to RPUSH/LPUSH a key with an empty list.
This is for consistency reasons: when the key is read with LRANGE than whether the list is empty or not, the rest of the code behaves the same.
Why the fact that if a key has an empty list it is deleted is a problem?
Because we are using Redis as a cache and would like to differentiate the 2 situations:
1. A specific key with corresponding values was not cached yet. In which case we want to calculate the values (takes a long time) and cache them. The outcome of the calculation might be an empty list.
2. A key with an empty list was already cached. In which case we would like not to perform the calculations and return an empty list.
The following options don't work:
1. rpush key --> no list value results with "wrong number of arguments".
2. rpush key [] --> adds a '[]' item
The (ugly) solution we are currently using is storing a one-item-list with an "EMPTY-ITEM" item, and checking that when we read the list.
Any ideas?
Thank you
Empty lists do not exist in Redis - a list must have one or more items. Empty lists (e.g. as a result of popping a non-empty one) are automatically removed.
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.
Redis LPUSH command is used to insert all the specified values at the head of the list stored at key. It is created as an empty list before performing the push operations if tje key does not exist.
Empty lists do not exist in Redis - a list must have one or more items. Empty lists (e.g. as a result of popping a non-empty one) are automatically removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With