LMOVE and More List Commands
A Wordcraft worker takes "send match summary email" jobs from a list. If it pops a job and then crashes before finishing, that job is gone for good: it left the list and lived only in the dead worker's memory. Moving the job to a second list in the same step as taking it fixes that, and a couple of other commands keep lists from growing without limit.
The rule
LMOVE takes from one end and puts on another, all in one step.
LMOVE source destination LEFT|RIGHT LEFT|RIGHT pops an item from the chosen end of source, pushes it onto the chosen end of destination, and returns the item. The first direction is where to take from, the second is where to put. If source is empty the reply is (nil) and nothing moves.
127.0.0.1:6379> RPUSH jobs:pending mail:1 mail:2 mail:3
(integer) 3
127.0.0.1:6379> LMOVE jobs:pending jobs:working LEFT RIGHT
"mail:1"
127.0.0.1:6379> LRANGE jobs:working 0 -1
1) "mail:1"
127.0.0.1:6379> LREM jobs:working 1 mail:1
(integer) 1Because the move is a single command, the job is always in exactly one of the two lists, even if the worker crashes. When the work is done, LREM deletes it from jobs:working; anything left there after a crash can be found and retried. BLMOVE is the blocking version that waits for a job to appear. (Older code uses RPOPLPUSH, which is the same as LMOVE ... RIGHT LEFT and is now deprecated.)
Using the same list as source and destination rotates it: LMOVE players players LEFT RIGHT moves the head to the tail, handy for taking turns.
Other everyday list commands:
LTRIM key start stopkeeps only that range and deletes the rest, replyingOK. After eachLPUSHto a feed,LTRIM feed:asha 0 99caps it at the newest 100 entries.LREM key count itemremoves up tocountcopies ofitemfrom the head side (0 means all of them) and replies with how many it removed.LSET key index itemoverwrites one position;LINSERT key BEFORE|AFTER pivot iteminserts next to an existing item.
The shortcut
Read LMOVE as a sentence: "take from the (first word) of source, put on the (second word) of destination."
Start
playersholds asha, ravi, meera. Asha has just taken her turn.
Drills
Answer fast — trust the rule.
a is x, y, z and b is empty. After LMOVE a b RIGHT LEFT, what does b hold?
jobs:pending is empty. What does LMOVE jobs:pending jobs:working LEFT RIGHT reply?
feed:asha has 250 items. After LTRIM feed:asha 0 99, what does LLEN feed:asha reply?
Why is LMOVE safer than LPOP followed by RPUSH for handing a job to a worker?
Remember
- LMOVE takes from one end and puts on another, all in one step.
- Read it as "take from the first word, put on the second".
- LTRIM caps a list; LREM clears a finished job from the working list.
