Redis Command: LMOVE

Summary

Command NameLMOVE
UsagePOP from source, PUSH to destination list
Group list
ACL Category@write
@list
@slow
Time ComplexityO(1)
FlagWRITE
DENYOOM
Arity5

Signature

LMOVE <source_list> <destination_list> <from_which_end_left_or_right> <to_which_end_left_or_right>

Usage

Pop an item from one list and push it to another list. We have full control of the source and destination, and on which end of both lists we want to perform the operation.

Redis LMOVE command execution
Redis LMOVE command execution

Notes

  • As the command RPOPLPUSH is deprecated (from version 6.2.0), this LMOVE command can be used as a substitute for that. The following LMOVE usage is exactly the same as the RPOPLPUSH-
    LMOVE sourcelist destlist right left
  • This command can be used to create and maintain a queuing system.

Arguments

ParameterDescriptionNameType
<source_list>The list from where we want to pop the itemsourcekey
<destination_list>The list to which we want to push the itemdestinationkey
<from_which_end_left_or_right>From which end of the <source_list> we want to pop the itemwherefromoneof[pure-token]
<to_which_end_left_or_right>To which end of the <destination_list> we want to push the itemwheretooneof[pure-token]

Notes

  • The same list can be used as source and destination.

Return Value

Return valueCase for the return valueType
List item which was moved(popped and pushed)If the operation was successfulstring
(nil)If source does not exist(nil)
errorIf the data type of source and/or destination is not of type listerror

Notes

  • If the source list is empty after popping the item, then the list is deleted.
  • If the source list does not exist then the command returns (nil) and nothing else happens to the destination list.
  • If the destination list does not exist, then it is created.
  • If the source and/or destination is not of the type list then the following error message is returned-
    (error) WRONGTYPE Operation against a key holding the wrong kind of value

Examples

Here are a few examples of the LMOVE command usage-

# Redis LMOVE command examples

# Push items to list
127.0.0.1:6379> rpush bigboxlist one two three four five six seven "last last item"
(integer) 8

# Check list items
127.0.0.1:6379> lrange bigboxlist 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
6) "six"
7) "seven"
8) "last last item"

# Check if "newlist" exists or not
# It does not exist yet
127.0.0.1:6379> exists newlist
(integer) 0

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "one"
127.0.0.1:6379> lmove bigboxlist newlist left right
"one"

# Check newlist
127.0.0.1:6379> lrange newlist 0 -1
1) "one"

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "two"
127.0.0.1:6379> lmove bigboxlist newlist left right
"two"

# Here is the status of newlist after second move
127.0.0.1:6379> lrange newlist 0 -1
1) "one"
2) "two"

# Pop item from the left(HEAD) of bigboxlist
# Push item to the left(HEAD) newlist
# The moved item is "three"
127.0.0.1:6379> lmove bigboxlist newlist left left
"three"

# Status of newlist after the LMOVE operation
127.0.0.1:6379> lrange newlist 0 -1
1) "three"
2) "one"
3) "two"

# Perform LMOVE multiple times
127.0.0.1:6379> lmove bigboxlist newlist left right
"four"
127.0.0.1:6379> lmove bigboxlist newlist left right
"five"
127.0.0.1:6379> lmove bigboxlist newlist left right
"six"
127.0.0.1:6379> lmove bigboxlist newlist left right
"seven"

# Check status of mylist
127.0.0.1:6379> lrange newlist 0 -1
1) "three"
2) "one"
3) "two"
4) "four"
5) "five"
6) "six"
7) "seven"

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "last last item", this is the last item of bigboxlist
127.0.0.1:6379> lmove bigboxlist newlist left right
"last last item"

# Check newlist
# It has all the items now from bigboxlist
127.0.0.1:6379> lrange newlist 0 -1
1) "three"
2) "one"
3) "two"
4) "four"
5) "five"
6) "six"
7) "seven"
8) "last last item"

# Check items of bigboxlist
# This is empty now all the items are popped out of it
127.0.0.1:6379> lrange bigboxlist 0 -1
(empty array)

# Check if bigboxlist key exists anymore
# It does not exist. As it was deleted when the last item was popped out of it.
127.0.0.1:6379> exists bigboxlist
(integer) 0

# Set a string value
127.0.0.1:6379> set firstkey "some value here"
OK

# Try to use a string type key in the LMOVE 
# It returns an error
127.0.0.1:6379> lmove newlist firstkey left right
(error) WRONGTYPE Operation against a key holding the wrong kind of value

127.0.0.1:6379> lmove firstkey newlist left right
(error) WRONGTYPE Operation against a key holding the wrong kind of value

# Use a non existing list/key as source
# Nothing is added to the destination list, as there is nothing in the source
# (nil) is retuned as a result
127.0.0.1:6379> lmove nonexistingsource newlist left right
(nil)

# Check the nonexistingsource
127.0.0.1:6379> lrange nonexistingsource 0 -1
(empty array)

# Check even if the key exist
# It does not exist
127.0.0.1:6379> exists nonexistingsource
(integer) 0

# Check if newlist was affected in any way by the previous LMOVE operation
# It was not affected, as the sources did not exists
127.0.0.1:6379> lrange newlist 0 -1
1) "three"
2) "one"
3) "two"
4) "four"
5) "five"
6) "six"
7) "seven"
8) "last last item"

# Use the same list as source and destination
127.0.0.1:6379> lmove newlist newlist left right
"three"

# Let's check the list
# "three" was moved from left/head and added to right/tail
127.0.0.1:6379> lrange newlist 0 -1
1) "one"
2) "two"
3) "four"
4) "five"
5) "six"
6) "seven"
7) "last last item"
8) "three"

# Use the same list as source and desitnation
# Pop and push at the same end
127.0.0.1:6379> lmove newlist newlist left left
"one"

# Last operation results in the same list, as the item was popped and pushed at the same end
127.0.0.1:6379> lrange newlist 0 -1
1) "one"
2) "two"
3) "four"
4) "five"
5) "six"
6) "seven"
7) "last last item"
8) "three"

Notes

  • User has the full flexibility to pop and push from any end(HEAD or TAIL) of the lists.

Code Implementations

Here are the usage examples of the Redis LRANGE command in different programming languages.

// Redis LMOVE command example in Golang

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

var rdb *redis.Client
var ctx context.Context

func init() {
	rdb = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "default",
		Password: "",
		DB:       0,
	})

	ctx = context.Background()
}

func main() {

	// Push items to list
	// Command: rpush bigboxlist one two three four five six seven "last last item"
	// Result: (integer) 8
	pushResult, err := rdb.RPush(ctx, "bigboxlist", "one", "two", "three", "four", "five", "six", "seven", "last last item").Result()

	if err != nil {
		fmt.Println("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Error: " + err.Error())
	}

	fmt.Printf("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: %v\n", pushResult)

	// Check list items
	// Command: lrange bigboxlist 0 -1
	// Result:
	//      1) "one"
	//      2) "two"
	//      3) "three"
	//      4) "four"
	//      5) "five"
	//      6) "six"
	//      7) "seven"
	//      8) "last last item"
	lrangeResult, err := rdb.LRange(ctx, "bigboxlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange bigboxlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Check if "newlist" exists or not
	// It does not exist yet
	// Command: exists newlist
	// Result: (integer) 0
	checkExistance, err := rdb.Exists(ctx, "newlist").Result()

	if err != nil {
		fmt.Println("Command: exists newlist | Error: " + err.Error())
	}

	fmt.Printf("Command: exists newlist | Result:  %v\n", checkExistance)

	// Pop item from the left(HEAD) of bigboxlist
	// Push item to the right(TAIL) newlist
	// The moved item is "one"
	// Command: lmove bigboxlist newlist left right
	// Result: "one"
	lmoveResult, err := rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Check newlist
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "one"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Result:")
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Pop item from the left(HEAD) of bigboxlist
	// Push item to the right(TAIL) newlist
	// The moved item is "two"
	// Command: lmove bigboxlist newlist left right
	// Result: "two"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Here is the status of newlist after second move
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "one"
	//      2) "two"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Pop item from the left(HEAD) of bigboxlist
	// Push item to the left(HEAD) newlist
	// The moved item is "three"
	// Command: lmove bigboxlist newlist left left
	// Result: "three"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "LEFT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left left | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left left | Result: " + lmoveResult)

	// Status of newlist after the LMOVE operation
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "three"
	//      2) "one"
	//      3) "two"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Result:")
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Perform LMOVE multiple times
	// Command: lmove bigboxlist newlist left right
	// Result: "four"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Command: lmove bigboxlist newlist left right
	// Result: "five"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Command: lmove bigboxlist newlist left right
	// Result: "six"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Command: lmove bigboxlist newlist left right
	// Result: "seven"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Check status of mylist
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "three"
	//      2) "one"
	//      3) "two"
	//      4) "four"
	//      5) "five"
	//      6) "six"
	//      7) "seven"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Pop item from the left(HEAD) of bigboxlist
	// Push item to the right(TAIL) newlist
	// The moved item is "last last item", this is the last item of bigboxlist
	// Command: lmove bigboxlist newlist left right
	// Result: "last last item"
	lmoveResult, err = rdb.LMove(ctx, "bigboxlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove bigboxlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult)

	// Check newlist
	// It has all the items now from bigboxlist
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "three"
	//      2) "one"
	//      3) "two"
	//      4) "four"
	//      5) "five"
	//      6) "six"
	//      7) "seven"
	//      8) "last last item"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Check items of bigboxlist
	// This is empty now all the items are popped out of it
	// Command: lrange bigboxlist 0 -1
	// Result: (empty array)
	lrangeResult, err = rdb.LRange(ctx, "bigboxlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange bigboxlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Check if bigboxlist key exists anymore
	// It does not exist. As it was deleted when the last item was popped out of it.
	// Command: exists bigboxlist
	// Result: (integer) 0
	checkExistance, err = rdb.Exists(ctx, "bigboxlist").Result()

	if err != nil {
		fmt.Println("Command: exists bigboxlist | Error: " + err.Error())
	}

	fmt.Printf("Command: exists bigboxlist | Result: %v\n", checkExistance)

	// Set a string value
	// Command: set firstkey "some value here"
	// Result: OK
	setResult, err := rdb.Set(ctx, "firstkey", "some value here", 0).Result()

	if err != nil {
		fmt.Println("Command: set firstkey \"some value here\" | Error: " + err.Error())
	}

	fmt.Println("Command: set firstkey \"some value here\" | Result: " + setResult)

	// Try to use a string type key in the LMOVE
	// It returns an error
	// Command: lmove newlist firstkey left right
	// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	lmoveResult, err = rdb.LMove(ctx, "newlist", "firstkey", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove newlist firstkey left right | Error: " + err.Error())

	}

	fmt.Println("Command: lmove newlist firstkey left right | Result: " + lmoveResult)

	// Command: lmove firstkey newlist left right
	// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	lmoveResult, err = rdb.LMove(ctx, "firstkey", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove firstkey newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove firstkey newlist left right | Result: " + lmoveResult)

	// Use a non existing list/key as source
	// Nothing is added to the destination list, as there is nothing in the source
	// (nil) is retuned as a result
	// Command: lmove nonexistingsource newlist left right
	// Result: (nil)
	lmoveResult, err = rdb.LMove(ctx, "nonexistingsource", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove nonexistingsource newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove nonexistingsource newlist left right | Result: " + lmoveResult)

	// Check the nonexistingsource
	// Command: lrange nonexistingsource 0 -1
	// Result: (empty array)
	lrangeResult, err = rdb.LRange(ctx, "nonexistingsource", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange nonexistingsource 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange nonexistingsource 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Check even if the key exist
	// It does not exist
	// Command: exists nonexistingsource
	// Result: (integer) 0
	checkExistance, err = rdb.Exists(ctx, "nonexistingsource").Result()

	if err != nil {
		fmt.Println("Command: exists nonexistingsource | Error: " + err.Error())
	}

	fmt.Printf("Command: exists nonexistingsource | Result: %v\n", checkExistance)

	// Check if newlist was affected in any way by the previous LMOVE operation
	// It was not affected, as the sources did not exists
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "three"
	//      2) "one"
	//      3) "two"
	//      4) "four"
	//      5) "five"
	//      6) "six"
	//      7) "seven"
	//      8) "last last item"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Use the same list as source and destination
	// Command: lmove newlist newlist left right
	// Result: "three"
	lmoveResult, err = rdb.LMove(ctx, "newlist", "newlist", "LEFT", "RIGHT").Result()

	if err != nil {
		fmt.Println("Command: lmove newlist newlist left right | Error: " + err.Error())
	}

	fmt.Println("Command: lmove newlist newlist left right | Result: " + lmoveResult)

	// Let's check the list
	// "three" was moved from left/head and added to right/tail
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "one"
	//      2) "two"
	//      3) "four"
	//      4) "five"
	//      5) "six"
	//      6) "seven"
	//      7) "last last item"
	//      8) "three"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

	// Use the same list as source and desitnation
	// Pop and push at the same end
	// Command: lmove newlist newlist left left
	// Result: "one"
	lmoveResult, err = rdb.LMove(ctx, "newlist", "newlist", "LEFT", "LEFT").Result()

	if err != nil {
		fmt.Println("Command: lmove newlist newlist left left | Error: " + err.Error())
	}

	fmt.Println("Command: lmove newlist newlist left left | Result: " + lmoveResult)

	// Last operation results in the same list, as the item was popped and pushed at the same end
	// Command: lrange newlist 0 -1
	// Result:
	//      1) "one"
	//      2) "two"
	//      3) "four"
	//      4) "five"
	//      5) "six"
	//      6) "seven"
	//      7) "last last item"
	//      8) "three"
	lrangeResult, err = rdb.LRange(ctx, "newlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange newlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange newlist 0 -1 | Result:")

	for _, item := range lrangeResult {
		fmt.Println(item)
	}

}

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
six
seven
last last item

Command: exists newlist | Result:  0

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result:
one

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result:
one
two

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result:
three
one
two

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven
Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven
last last item

Command: lrange bigboxlist 0 -1 | Result:
Command: exists bigboxlist | Result: 0

Command: set firstkey "some value here" | Result: OK

Command: lmove newlist firstkey left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove newlist firstkey left right | Result:
Command: lmove firstkey newlist left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Result:

Command: lmove nonexistingsource newlist left right | Error: redis: nil
Command: lmove nonexistingsource newlist left right | Result:
Command: lrange nonexistingsource 0 -1 | Result:
Command: exists nonexistingsource | Result: 0

Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven
last last item

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result:
one
two
four
five
six
seven
last last item
three

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result:
one
two
four
five
six
seven
last last item
three

Notes

  • Use “LMove” method from redis-go module.
  • Signature of the method is-
    LMove(ctx context.Context, source, destination, srcpos, destpos string) *StringCmd
// Redis LMOVE command example in JavaScript(NodeJS)

import { createClient } from 'redis';

// Create redis client
const redisClient = createClient({
    url: 'redis://default:@localhost:6379'
});

await redisClient.on('error', err => console.log('Error while connecting to Redis', err));

// Connect Redis client
await redisClient.connect();


/**
 * Push items to list
 *
 * Command: rpush bigboxlist one two three four five six seven "last last item"
 * Result: (integer) 8
 */
let commandResult = await redisClient.rPush("bigboxlist", ["one", "two", "three", "four", "five", "six", "seven", "last last item"]);

console.log("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: " + commandResult);

/**
 * Check list items
 *
 * Command: lrange bigboxlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "three"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 *      8) "last last item"
 */
commandResult = await redisClient.lRange("bigboxlist", 0, -1);

console.log("Command: lrange bigboxlist 0 -1 | Result:", commandResult);

/**
 * Check if "newlist" exists or not
 * It does not exist yet
 *
 * Command: exists newlist
 * Result: (integer) 0
 */
commandResult = await redisClient.exists("newlist");

console.log("Command: exists newlist | Result: " + commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the right(TAIL) newlist
 * The moved item is "one"
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "one"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Check newlist
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the right(TAIL) newlist
 * The moved item is "two"
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "two"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Here is the status of newlist after second move
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the left(HEAD) newlist
 * The moved item is "three"
 *
 * Command: lmove bigboxlist newlist left left
 * Result: "three"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "LEFT");

console.log("Command: lmove bigboxlist newlist left left | Result: " + commandResult);

/**
 * Status of newlist after the LMOVE operation
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Perform LMOVE multiple times
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "four"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Command: lmove bigboxlist newlist left right
 * Result: "five"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Command: lmove bigboxlist newlist left right
 * Result: "six"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Command: lmove bigboxlist newlist left right
 * Result: "seven"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Check status of mylist
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the right(TAIL) newlist
 * The moved item is "last last item", this is the last item of bigboxlist
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "last last item"
 */
commandResult = await redisClient.lMove("bigboxlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove bigboxlist newlist left right | Result: " + commandResult);

/**
 * Check newlist
 * It has all the items now from bigboxlist
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 *      8) "last last item"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Check items of bigboxlist
 * This is empty now all the items are popped out of it
 *
 * Command: lrange bigboxlist 0 -1
 * Result: (empty array)
 */
commandResult = await redisClient.lRange("bigboxlist", 0, -1);

console.log("Command: lrange bigboxlist 0 -1 | Result:", commandResult);

/**
 * Check if bigboxlist key exists anymore
 * It does not exist. As it was deleted when the last item was popped out of it.
 *
 * Command: exists bigboxlist
 * Result: (integer) 0
 */
commandResult = await redisClient.exists("bigboxlist");

console.log("Command: exists bigboxlist | Result: " + commandResult);

/**
 * Set a string value
 *
 * Command: set firstkey "some value here"
 * Result: OK
 */
commandResult = await redisClient.set("firstkey", "some value here");

console.log("Command: set firstkey \"some value here\" | Result: " + commandResult);

/**
 * Try to use a string type key in the LMOVE 
 * It returns an error
 *
 * Command: lmove newlist firstkey left right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    commandResult = await redisClient.lMove("newlist", "firstkey", "LEFT", "RIGHT");

    console.log("Command: lmove newlist firstkey left right | Result: " + commandResult);
} catch (e) {
    console.log("Command: lmove newlist firstkey left right | Error: " + e);
}

/**
 * Command: lmove firstkey newlist left right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    commandResult = await redisClient.lMove("firstkey", "newlist", "LEFT", "RIGHT");

    console.log("Command: lmove firstkey newlist left right | Result: " + commandResult);
} catch (e) {
    console.log("Command: lmove firstkey newlist left right | Error: " + e);
}

/**
 * Use a non existing list/key as source
 * Nothing is added to the destination list, as there is nothing in the source
 * (nil) is retuned as a result
 *
 * Command: lmove nonexistingsource newlist left right
 * Result: (nil)
 */
commandResult = await redisClient.lMove("nonexistingsource", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove nonexistingsource newlist left right | Result: " + commandResult);

/**
 * Check the nonexistingsource
 *
 * Command: lrange nonexistingsource 0 -1
 * Result: (empty array)
 */
commandResult = await redisClient.lRange("nonexistingsource", 0, -1);

console.log("Command: lrange nonexistingsource 0 -1 | Result:", commandResult);

/**
 * Check even if the key exist
 * It does not exist
 *
 * Command: exists nonexistingsource
 * Result: (integer) 0
 */
commandResult = await redisClient.exists("nonexistingsource");

console.log("Command: exists nonexistingsource | Result: " + commandResult);

/**
 * Check if newlist was affected in any way by the previous LMOVE operation
 * It was not affected, as the sources did not exists
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 *      8) "last last item"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Use the same list as source and destination
 *
 * Command: lmove newlist newlist left right
 * Result: "three"
 */
commandResult = await redisClient.lMove("newlist", "newlist", "LEFT", "RIGHT");

console.log("Command: lmove newlist newlist left right | Result: " + commandResult);

/**
 * Let's check the list
 * "three" was moved from left/head and added to right/tail
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "four"
 *      4) "five"
 *      5) "six"
 *      6) "seven"
 *      7) "last last item"
 *      8) "three"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);

/**
 * Use the same list as source and desitnation
 * Pop and push at the same end
 *
 * Command: lmove newlist newlist left left
 * Result: "one"
 */
commandResult = await redisClient.lMove("newlist", "newlist", "LEFT", "LEFT");

console.log("Command: lmove newlist newlist left left | Result: " + commandResult);

/**
 * Last operation results in the same list, as the item was popped and pushed at the same end
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "four"
 *      4) "five"
 *      5) "six"
 *      6) "seven"
 *      7) "last last item"
 *      8) "three"
 */
commandResult = await redisClient.lRange("newlist", 0, -1);

console.log("Command: lrange newlist 0 -1 | Result:", commandResult);


process.exit(0);

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result: [
  'one',
  'two',
  'three',
  'four',
  'five',
  'six',
  'seven',
  'last last item'
]

Command: exists newlist | Result: 0

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result: [ 'one' ]

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result: [ 'one', 'two' ]

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result: [ 'three', 'one', 'two' ]

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven
Command: lrange newlist 0 -1 | Result: [
  'three', 'one',
  'two',   'four',
  'five',  'six',
  'seven'
]

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result: [
  'three',
  'one',
  'two',
  'four',
  'five',
  'six',
  'seven',
  'last last item'
]

Command: lrange bigboxlist 0 -1 | Result: []
Command: exists bigboxlist | Result: 0

Command: set firstkey "some value here" | Result: OK

Command: lmove newlist firstkey left right | Error: Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Error: Error: WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmove nonexistingsource newlist left right | Result: null
Command: lrange nonexistingsource 0 -1 | Result: []
Command: exists nonexistingsource | Result: 0

Command: lrange newlist 0 -1 | Result: [
  'three',
  'one',
  'two',
  'four',
  'five',
  'six',
  'seven',
  'last last item'
]

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result: [
  'one',
  'two',
  'four',
  'five',
  'six',
  'seven',
  'last last item',
  'three'
]

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result: [
  'one',
  'two',
  'four',
  'five',
  'six',
  'seven',
  'last last item',
  'three'
]

Notes

  • Use the function “lMove” from the package node-redis.
// Redis LMOVE command example in Java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListDirection;

import java.util.List;

public class Lmove {
    public static void main(String[] args) {
        // Create connection pool
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Push items to list
             *
             * Command: rpush bigboxlist one two three four five six seven "last last item"
             * Result: (integer) 8
             */
            long pushResult = jedis.rpush("bigboxlist", "one", "two", "three", "four", "five", "six", "seven", "last last item");

            System.out.println("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: " + pushResult);

            /**
             * Check list items
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "three"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             *      8) "last last item"
             */
            List<String> lrangeResult = jedis.lrange("bigboxlist", 0, -1);

            System.out.println("Command: lrange bigboxlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Check if "newlist" exists or not
             * It does not exist yet
             *
             * Command: exists newlist
             * Result: (integer) 0
             */
            boolean checkExistance = jedis.exists("newlist");

            System.out.println("Command: exists newlist | Result: " + checkExistance);

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the right(TAIL) newlist
             * The moved item is "one"
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "one"
             */
            String lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Check newlist
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the right(TAIL) newlist
             * The moved item is "two"
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "two"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Here is the status of newlist after second move
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the left(HEAD) newlist
             * The moved item is "three"
             *
             * Command: lmove bigboxlist newlist left left
             * Result: "three"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.LEFT);

            System.out.println("Command: lmove bigboxlist newlist left left | Result: " + lmoveResult);

            /**
             * Status of newlist after the LMOVE operation
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Perform LMOVE multiple times
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "four"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Command: lmove bigboxlist newlist left right
             * Result: "five"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Command: lmove bigboxlist newlist left right
             * Result: "six"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Command: lmove bigboxlist newlist left right
             * Result: "seven"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Check status of mylist
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the right(TAIL) newlist
             * The moved item is "last last item", this is the last item of bigboxlist
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "last last item"
             */
            lmoveResult = jedis.lmove("bigboxlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Check newlist
             * It has all the items now from bigboxlist
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             *      8) "last last item"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Check items of bigboxlist
             * This is empty now all the items are popped out of it
             *
             * Command: lrange bigboxlist 0 -1
             * Result: (empty array)
             */
            lrangeResult = jedis.lrange("bigboxlist", 0, -1);

            System.out.println("Command: lrange bigboxlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Check if bigboxlist key exists anymore
             * It does not exist. As it was deleted when the last item was popped out of it.
             *
             * Command: exists bigboxlist
             * Result: (integer) 0
             */
            checkExistance = jedis.exists("bigboxlist");

            System.out.println("Command: exists bigboxlist | Result: " + checkExistance);

            /**
             * Set a string value
             *
             * Command: set firstkey "some value here"
             * Result: OK
             */
            String setResult = jedis.set("firstkey", "some value here");

            System.out.println("Command: set firstkey \"some value here\" | Result: " + setResult);

            /**
             * Try to use a string type key in the LMOVE 
             * It returns an error
             *
             * Command: lmove newlist firstkey left right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                lmoveResult = jedis.lmove("newlist", "firstkey", ListDirection.LEFT, ListDirection.RIGHT);

                System.out.println("Command: lmove newlist firstkey left right | Result: " + lmoveResult);
            } catch (Exception e) {
                System.out.println("Command: lmove newlist firstkey left right | Error: " + e.getMessage());
            }

            /**
             * Command: lmove firstkey newlist left right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                lmoveResult = jedis.lmove("firstkey", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

                System.out.println("Command: lmove firstkey newlist left right | Result: " + lmoveResult);
            } catch (Exception e) {
                System.out.println("Command: lmove firstkey newlist left right | Error: " + e.getMessage());
            }

            /**
             * Use a non existing list/key as source
             * Nothing is added to the destination list, as there is nothing in the source
             * (nil) is retuned as a result
             *
             * Command: lmove nonexistingsource newlist left right
             * Result: (nil)
             */
            lmoveResult = jedis.lmove("nonexistingsource", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove nonexistingsource newlist left right | Result: " + lmoveResult);

            /**
             * Check the nonexistingsource
             *
             * Command: lrange nonexistingsource 0 -1
             * Result: (empty array)
             */
            lrangeResult = jedis.lrange("nonexistingsource", 0, -1);

            System.out.println("Command: lrange nonexistingsource 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Check even if the key exist
             * It does not exist
             *
             * Command: exists nonexistingsource
             * Result: (integer) 0
             */
            checkExistance = jedis.exists("nonexistingsource");

            System.out.println("Command: exists nonexistingsource | Result: " + checkExistance);

            /**
             * Check if newlist was affected in any way by the previous LMOVE operation
             * It was not affected, as the sources did not exists
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             *      8) "last last item"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Use the same list as source and destination
             *
             * Command: lmove newlist newlist left right
             * Result: "three"
             */
            lmoveResult = jedis.lmove("newlist", "newlist", ListDirection.LEFT, ListDirection.RIGHT);

            System.out.println("Command: lmove newlist newlist left right | Result: " + lmoveResult);

            /**
             * Let's check the list
             * "three" was moved from left/head and added to right/tail
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "four"
             *      4) "five"
             *      5) "six"
             *      6) "seven"
             *      7) "last last item"
             *      8) "three"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

            /**
             * Use the same list as source and desitnation
             * Pop and push at the same end
             *
             * Command: lmove newlist newlist left left
             * Result: "one"
             */
            lmoveResult = jedis.lmove("newlist", "newlist", ListDirection.LEFT, ListDirection.LEFT);

            System.out.println("Command: lmove newlist newlist left left | Result: " + lmoveResult);

            /**
             * Last operation results in the same list, as the item was popped and pushed at the same end
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "four"
             *      4) "five"
             *      5) "six"
             *      6) "seven"
             *      7) "last last item"
             *      8) "three"
             */
            lrangeResult = jedis.lrange("newlist", 0, -1);

            System.out.println("Command: lrange newlist 0 -1 | Result:");

            for (String item: lrangeResult) {
                System.out.println(item);
            }

        }

        jedisPool.close();
    }
}

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
six
seven
last last item

Command: exists newlist | Result: false

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result:
one

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result:
one
two

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result:
three
one
two

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven

Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven
last last item

Command: lrange bigboxlist 0 -1 | Result:

Command: exists bigboxlist | Result: false

Command: set firstkey "some value here" | Result: OK

Command: lmove newlist firstkey left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmove nonexistingsource newlist left right | Result: null
Command: lrange nonexistingsource 0 -1 | Result:
Command: exists nonexistingsource | Result: false

Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven
last last item

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result:
one
two
four
five
six
seven
last last item
three

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result:
one
two
four
five
six
seven
last last item
three

Notes

  • Use method “lmove” from Jedis package.
  • Signature of the method is-
    public String lmove(final String srcKey, final String dstKey, final ListDirection from, final ListDirection to)
// Redis LMOVE command examples in C#

using StackExchange.Redis;

namespace Lmove
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase rdb = redis.GetDatabase();

            /**
            * Push items to list
            *
            * Command: rpush bigboxlist one two three four five six seven "last last item"
            * Result: (integer) 8
            */
            long pushResult = rdb.ListRightPush("bigboxlist", new RedisValue[] { "one", "two", "three", "four", "five", "six", "seven", "last last item" });

            Console.WriteLine("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: " + pushResult);


            /**
             * Check list items
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "three"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             *      8) "last last item"
             */
            RedisValue[] lrangeResult = rdb.ListRange("bigboxlist", 0, -1);

            Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Check if "newlist" exists or not
             * It does not exist yet
             *
             * Command: exists newlist
             * Result: (integer) 0
             */
            bool checkExistance = rdb.KeyExists("newlist");

            Console.WriteLine("Command: exists newlist | Result: " + checkExistance);


            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the right(TAIL) newlist
             * The moved item is "one"
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "one"
             */
            RedisValue lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Check newlist
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the right(TAIL) newlist
             * The moved item is "two"
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "two"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Here is the status of newlist after second move
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the left(HEAD) newlist
             * The moved item is "three"
             *
             * Command: lmove bigboxlist newlist left left
             * Result: "three"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Left);

            Console.WriteLine("Command: lmove bigboxlist newlist left left | Result: " + lmoveResult);

            /**
             * Status of newlist after the LMOVE operation
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Perform LMOVE multiple times
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "four"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Command: lmove bigboxlist newlist left right
             * Result: "five"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Command: lmove bigboxlist newlist left right
             * Result: "six"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Command: lmove bigboxlist newlist left right
             * Result: "seven"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Check status of mylist
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Pop item from the left(HEAD) of bigboxlist
             * Push item to the right(TAIL) newlist
             * The moved item is "last last item", this is the last item of bigboxlist
             *
             * Command: lmove bigboxlist newlist left right
             * Result: "last last item"
             */
            lmoveResult = rdb.ListMove("bigboxlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove bigboxlist newlist left right | Result: " + lmoveResult);

            /**
             * Check newlist
             * It has all the items now from bigboxlist
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             *      8) "last last item"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Check items of bigboxlist
             * This is empty now all the items are popped out of it
             *
             * Command: lrange bigboxlist 0 -1
             * Result: (empty array)
             */
            lrangeResult = rdb.ListRange("bigboxlist", 0, -1);

            Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Check if bigboxlist key exists anymore
             * It does not exist. As it was deleted when the last item was popped out of it.
             *
             * Command: exists bigboxlist
             * Result: (integer) 0
             */
            checkExistance = rdb.KeyExists("bigboxlist");

            Console.WriteLine("Command: exists bigboxlist | Result: " + checkExistance);

            /**
             * Set a string value
             *
             * Command: set firstkey "some value here"
             * Result: OK
             */
            bool setResult = rdb.StringSet("firstkey", "some value here");
            Console.WriteLine("Command: set firstkey \"some value here\" | Result: " + setResult);

            /**
             * Try to use a string type key in the LMOVE 
             * It returns an error
             *
             * Command: lmove newlist firstkey left right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                lmoveResult = rdb.ListMove("newlist", "firstkey", ListSide.Left, ListSide.Right);

                Console.WriteLine("Command: lmove newlist firstkey left right | Result: " + lmoveResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lmove newlist firstkey left right | Error: " + e.Message);
            }

            /**
             * Command: lmove firstkey newlist left right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                lmoveResult = rdb.ListMove("firstkey", "newlist", ListSide.Left, ListSide.Right);

                Console.WriteLine("Command: lmove firstkey newlist left right | Result: " + lmoveResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lmove firstkey newlist left right | Error: " + e.Message);
            }

            /**
             * Use a non existing list/key as source
             * Nothing is added to the destination list, as there is nothing in the source
             * (nil) is retuned as a result
             *
             * Command: lmove nonexistingsource newlist left right
             * Result: (nil)
             */
            lmoveResult = rdb.ListMove("nonexistingsource", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove nonexistingsource newlist left right | Result: " + lmoveResult);

            /**
             * Check the nonexistingsource
             *
             * Command: lrange nonexistingsource 0 -1
             * Result: (empty array)
             */
            lrangeResult = rdb.ListRange("nonexistingsource", 0, -1);

            Console.WriteLine("Command: lrange nonexistingsource 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Check even if the key exist
             * It does not exist
             *
             * Command: exists nonexistingsource
             * Result: (integer) 0
             */
            checkExistance = rdb.KeyExists("nonexistingsource");

            Console.WriteLine("Command: exists nonexistingsource | Result: " + checkExistance);

            /**
             * Check if newlist was affected in any way by the previous LMOVE operation
             * It was not affected, as the sources did not exists
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "three"
             *      2) "one"
             *      3) "two"
             *      4) "four"
             *      5) "five"
             *      6) "six"
             *      7) "seven"
             *      8) "last last item"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Use the same list as source and destination
             *
             * Command: lmove newlist newlist left right
             * Result: "three"
             */
            lmoveResult = rdb.ListMove("newlist", "newlist", ListSide.Left, ListSide.Right);

            Console.WriteLine("Command: lmove newlist newlist left right | Result: " + lmoveResult);

            /**
             * Let's check the list
             * "three" was moved from left/head and added to right/tail
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "four"
             *      4) "five"
             *      5) "six"
             *      6) "seven"
             *      7) "last last item"
             *      8) "three"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }

            /**
             * Use the same list as source and desitnation
             * Pop and push at the same end
             *
             * Command: lmove newlist newlist left left
             * Result: "one"
             */
            lmoveResult = rdb.ListMove("newlist", "newlist", ListSide.Left, ListSide.Left);

            Console.WriteLine("Command: lmove newlist newlist left left | Result: " + lmoveResult);

            /**
             * Last operation results in the same list, as the item was popped and pushed at the same end
             *
             * Command: lrange newlist 0 -1
             * Result:
             *      1) "one"
             *      2) "two"
             *      3) "four"
             *      4) "five"
             *      5) "six"
             *      6) "seven"
             *      7) "last last item"
             *      8) "three"
             */
            lrangeResult = rdb.ListRange("newlist", 0, -1);

            Console.WriteLine("Command: lrange newlist 0 -1 | Result:");

            foreach (var item in lrangeResult)
            {
                Console.WriteLine(item);
            }
        }
    }
}

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
six
seven
last last item

Command: exists newlist | Result: False

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result:
one

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result:
one
two

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result:
three
one
two

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven
Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven
last last item

Command: lrange bigboxlist 0 -1 | Result:
Command: exists bigboxlist | Result: False

Command: set firstkey "some value here" | Result: True

Command: lmove newlist firstkey left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmove nonexistingsource newlist left right | Result:
Command: lrange nonexistingsource 0 -1 | Result:
Command: exists nonexistingsource | Result: False

Command: lrange newlist 0 -1 | Result:
three
one
two
four
five
six
seven
last last item

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result:
one
two
four
five
six
seven
last last item
three

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result:
one
two
four
five
six
seven
last last item
three

Notes

  • Use the method “ListMove” from StackExchange.Redis.
  • Signature of the method is-
    RedisValue ListMove(RedisKey sourceKey, RedisKey destinationKey, ListSide sourceSide, ListSide destinationSide, CommandFlags flags = CommandFlags.None)
<?php
// Redis LMOVE command example in PHP

require 'vendor/autoload.php';

// Connect to Redis
$redisClient = new Predis\Client([
    'scheme' => 'tcp',
    'host' => 'localhost',
    'port' => 6379,
]);


/**
 * Push items to list
 *
 * Command: rpush bigboxlist one two three four five six seven "last last item"
 * Result: (integer) 8
 */
$commandResult = $redisClient->rpush("bigboxlist", ["one", "two", "three", "four", "five", "six", "seven", "last last item"]);

echo "Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: " . $commandResult . "\n";

/**
 * Check list items
 *
 * Command: lrange bigboxlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "three"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 *      8) "last last item"
 */
$commandResult = $redisClient->lrange("bigboxlist", 0, -1);

echo "Command: lrange bigboxlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Check if "newlist" exists or not
 * It does not exist yet
 *
 * Command: exists newlist
 * Result: (integer) 0
 */
$commandResult = $redisClient->exists("newlist");

echo "Command: exists newlist | Result: " . $commandResult . "\n";

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the right(TAIL) newlist
 * The moved item is "one"
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "one"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Check newlist
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the right(TAIL) newlist
 * The moved item is "two"
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "two"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Here is the status of newlist after second move
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the left(HEAD) newlist
 * The moved item is "three"
 *
 * Command: lmove bigboxlist newlist left left
 * Result: "three"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "LEFT");

echo "Command: lmove bigboxlist newlist left left | Result: " . $commandResult . "\n";

/**
 * Status of newlist after the LMOVE operation
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Perform LMOVE multiple times
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "four"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Command: lmove bigboxlist newlist left right
 * Result: "five"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Command: lmove bigboxlist newlist left right
 * Result: "six"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Command: lmove bigboxlist newlist left right
 * Result: "seven"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Check status of mylist
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Pop item from the left(HEAD) of bigboxlist
 * Push item to the right(TAIL) newlist
 * The moved item is "last last item", this is the last item of bigboxlist
 *
 * Command: lmove bigboxlist newlist left right
 * Result: "last last item"
 */
$commandResult = $redisClient->lmove("bigboxlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove bigboxlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Check newlist
 * It has all the items now from bigboxlist
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 *      8) "last last item"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Check items of bigboxlist
 * This is empty now all the items are popped out of it
 *
 * Command: lrange bigboxlist 0 -1
 * Result: (empty array)
 */
$commandResult = $redisClient->lrange("bigboxlist", 0, -1);

echo "Command: lrange bigboxlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Check if bigboxlist key exists anymore
 * It does not exist. As it was deleted when the last item was popped out of it.
 *
 * Command: exists bigboxlist
 * Result: (integer) 0
 */
$commandResult = $redisClient->exists("bigboxlist");

echo "Command: exists bigboxlist | Result: " . $commandResult . "\n";

/**
 * Set a string value
 *
 * Command: set firstkey "some value here"
 * Result: OK
 */
$commandResult = $redisClient->set("firstkey", "some value here");

echo "Command: set firstkey \"some value here\" | Result: " . $commandResult . "\n";

/**
 * Try to use a string type key in the LMOVE 
 * It returns an error
 *
 * Command: lmove newlist firstkey left right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->lmove("newlist", "firstkey", "LEFT", "RIGHT");

    echo "Command: lmove newlist firstkey left right | Result: " . $commandResult . "\n";
} catch (\Exception $e) {
    echo "Command: lmove newlist firstkey left right | Error: " . $e->getMessage() . "\n";
}

/**
 * Command: lmove firstkey newlist left right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->lmove("firstkey", "newlist", "LEFT", "RIGHT");

    echo "Command: lmove firstkey newlist left right | Result: " . $commandResult . "\n";
} catch (\Exception $e) {
    echo "Command: lmove firstkey newlist left right | Error: " . $e->getMessage() . "\n";
}

/**
 * Use a non existing list/key as source
 * Nothing is added to the destination list, as there is nothing in the source
 * (nil) is retuned as a result
 *
 * Command: lmove nonexistingsource newlist left right
 * Result: (nil)
 */
$commandResult = $redisClient->lmove("nonexistingsource", "newlist", "LEFT", "RIGHT");

echo "Command: lmove nonexistingsource newlist left right | Result: " . $commandResult . "\n";

/**
 * Check the nonexistingsource
 *
 * Command: lrange nonexistingsource 0 -1
 * Result: (empty array)
 */
$commandResult = $redisClient->lrange("nonexistingsource", 0, -1);

echo "Command: lrange nonexistingsource 0 -1 | Result:";
print_r($commandResult);

/**
 * Check even if the key exist
 * It does not exist
 *
 * Command: exists nonexistingsource
 * Result: (integer) 0
 */
$commandResult = $redisClient->exists("nonexistingsource");

echo "Command: exists nonexistingsource | Result: " . $commandResult . "\n";

/**
 * Check if newlist was affected in any way by the previous LMOVE operation
 * It was not affected, as the sources did not exists
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "three"
 *      2) "one"
 *      3) "two"
 *      4) "four"
 *      5) "five"
 *      6) "six"
 *      7) "seven"
 *      8) "last last item"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Use the same list as source and destination
 *
 * Command: lmove newlist newlist left right
 * Result: "three"
 */
$commandResult = $redisClient->lmove("newlist", "newlist", "LEFT", "RIGHT");

echo "Command: lmove newlist newlist left right | Result: " . $commandResult . "\n";

/**
 * Let's check the list
 * "three" was moved from left/head and added to right/tail
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "four"
 *      4) "five"
 *      5) "six"
 *      6) "seven"
 *      7) "last last item"
 *      8) "three"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

/**
 * Use the same list as source and desitnation
 * Pop and push at the same end
 *
 * Command: lmove newlist newlist left left
 * Result: "one"
 */
$commandResult = $redisClient->lmove("newlist", "newlist", "LEFT", "LEFT");

echo "Command: lmove newlist newlist left left | Result: " . $commandResult . "\n";

/**
 * Last operation results in the same list, as the item was popped and pushed at the same end
 *
 * Command: lrange newlist 0 -1
 * Result:
 *      1) "one"
 *      2) "two"
 *      3) "four"
 *      4) "five"
 *      5) "six"
 *      6) "seven"
 *      7) "last last item"
 *      8) "three"
 */
$commandResult = $redisClient->lrange("newlist", 0, -1);

echo "Command: lrange newlist 0 -1 | Result:";
print_r($commandResult);

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result:Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => last last item
)

Command: exists newlist | Result: 0

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => one
)

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => one
    [1] => two
)

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => three
    [1] => one
    [2] => two
)

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => three
    [1] => one
    [2] => two
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
)

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => three
    [1] => one
    [2] => two
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => last last item
)

Command: lrange bigboxlist 0 -1 | Result:Array
(
)

Command: exists bigboxlist | Result: 0

Command: set firstkey "some value here" | Result: OK

Command: lmove newlist firstkey left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmove nonexistingsource newlist left right | Result:
Command: lrange nonexistingsource 0 -1 | Result:Array
(
)
Command: exists nonexistingsource | Result: 0

Command: lrange newlist 0 -1 | Result:Array
(
    [0] => three
    [1] => one
    [2] => two
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => last last item
)

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => one
    [1] => two
    [2] => four
    [3] => five
    [4] => six
    [5] => seven
    [6] => last last item
    [7] => three
)

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result:Array
(
    [0] => one
    [1] => two
    [2] => four
    [3] => five
    [4] => six
    [5] => seven
    [6] => last last item
    [7] => three
)

Notes

  • Use the method “lmove” of predis.
  • Signature of the method is-
    lmove(string $source, string $destination, string $where, string $to): string
# Redis LMOVE command example in Python

import redis
import time

# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
                          username='default', password='',
                          decode_responses=True)


# Push items to list
# Command: rpush bigboxlist one two three four five six seven "last last item"
# Result: (integer) 8
commandResult = redisClient.rpush("bigboxlist", "one", "two", "three", "four", "five", "six", "seven", "last last item")

print("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: {}".format(commandResult))

# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "three"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
#      8) "last last item"
commandResult = redisClient.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result:{}".format(commandResult))

# Check if "newlist" exists or not
# It does not exist yet
# Command: exists newlist
# Result: (integer) 0
commandResult = redisClient.exists("newlist")

print("Command: exists newlist | Result: {}".format(commandResult))

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "one"
# Command: lmove bigboxlist newlist left right
# Result: "one"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Check newlist
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "two"
# Command: lmove bigboxlist newlist left right
# Result: "two"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Here is the status of newlist after second move
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
#      2) "two"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Pop item from the left(HEAD) of bigboxlist
# Push item to the left(HEAD) newlist
# The moved item is "three"
# Command: lmove bigboxlist newlist left left
# Result: "three"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "LEFT")

print("Command: lmove bigboxlist newlist left left | Result: {}".format(commandResult))

# Status of newlist after the LMOVE operation
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Perform LMOVE multiple times
# Command: lmove bigboxlist newlist left right
# Result: "four"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Command: lmove bigboxlist newlist left right
# Result: "five"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Command: lmove bigboxlist newlist left right
# Result: "six"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Command: lmove bigboxlist newlist left right
# Result: "seven"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Check status of mylist
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "last last item", this is the last item of bigboxlist
# Command: lmove bigboxlist newlist left right
# Result: "last last item"
commandResult = redisClient.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: {}".format(commandResult))

# Check newlist
# It has all the items now from bigboxlist
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
#      8) "last last item"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Check items of bigboxlist
# This is empty now all the items are popped out of it
# Command: lrange bigboxlist 0 -1
# Result: (empty array)
commandResult = redisClient.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result:{}".format(commandResult))

# Check if bigboxlist key exists anymore
# It does not exist. As it was deleted when the last item was popped out of it.
# Command: exists bigboxlist
# Result: (integer) 0
commandResult = redisClient.exists("bigboxlist")

print("Command: exists bigboxlist | Result: {}".format(commandResult))

# Set a string value
# Command: set firstkey "some value here"
# Result: OK
commandResult = redisClient.set("firstkey", "some value here")

print("Command: set firstkey \"some value here\" | Result: {}".format(commandResult))

# Try to use a string type key in the LMOVE 
# It returns an error
# Command: lmove newlist firstkey left right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.lmove("newlist", "firstkey", "LEFT", "RIGHT")

    print("Command: lmove newlist firstkey left right | Result: {}".format(commandResult))
except Exception as error:
    print("Command: lmove newlist firstkey left right | Error: " , error)

# Command: lmove firstkey newlist left right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.lmove("firstkey", "newlist", "LEFT", "RIGHT")

    print("Command: lmove firstkey newlist left right | Result: {}".format(commandResult))
except Exception as error:
    print("Command: lmove firstkey newlist left right | Error: ", error)

# Use a non existing list/key as source
# Nothing is added to the destination list, as there is nothing in the source
# (nil) is retuned as a result
# Command: lmove nonexistingsource newlist left right
# Result: (nil)
commandResult = redisClient.lmove("nonexistingsource", "newlist", "LEFT", "RIGHT")

print("Command: lmove nonexistingsource newlist left right | Result: {}".format(commandResult))

# Check the nonexistingsource
# Command: lrange nonexistingsource 0 -1
# Result: (empty array)
commandResult = redisClient.lrange("nonexistingsource", 0, -1)

print("Command: lrange nonexistingsource 0 -1 | Result:{}".format(commandResult))

# Check even if the key exist
# It does not exist
# Command: exists nonexistingsource
# Result: (integer) 0
commandResult = redisClient.exists("nonexistingsource")

print("Command: exists nonexistingsource | Result: {}".format(commandResult))

# Check if newlist was affected in any way by the previous LMOVE operation
# It was not affected, as the sources did not exists
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
#      8) "last last item"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Use the same list as source and destination
# Command: lmove newlist newlist left right
# Result: "three"
commandResult = redisClient.lmove("newlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove newlist newlist left right | Result: {}".format(commandResult))

# Let's check the list
# "three" was moved from left/head and added to right/tail
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "four"
#      4) "five"
#      5) "six"
#      6) "seven"
#      7) "last last item"
#      8) "three"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

# Use the same list as source and desitnation
# Pop and push at the same end
# Command: lmove newlist newlist left left
# Result: "one"
commandResult = redisClient.lmove("newlist", "newlist", "LEFT", "LEFT")

print("Command: lmove newlist newlist left left | Result: {}".format(commandResult))

# Last operation results in the same list, as the item was popped and pushed at the same end
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "four"
#      4) "five"
#      5) "six"
#      6) "seven"
#      7) "last last item"
#      8) "three"
commandResult = redisClient.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:{}".format(commandResult))

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result:['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'last last item']

Command: exists newlist | Result: 0

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result:['one']

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result:['one', 'two']

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result:['three', 'one', 'two']

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven
Command: lrange newlist 0 -1 | Result:['three', 'one', 'two', 'four', 'five', 'six', 'seven']

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result:['three', 'one', 'two', 'four', 'five', 'six', 'seven', 'last last item']

Command: lrange bigboxlist 0 -1 | Result:[]
Command: exists bigboxlist | Result: 0

Command: set firstkey "some value here" | Result: True
Command: lmove newlist firstkey left right | Error:  WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Error:  WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmove nonexistingsource newlist left right | Result: None
Command: lrange nonexistingsource 0 -1 | Result:[]
Command: exists nonexistingsource | Result: 0

Command: lrange newlist 0 -1 | Result:['three', 'one', 'two', 'four', 'five', 'six', 'seven', 'last last item']

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result:['one', 'two', 'four', 'five', 'six', 'seven', 'last last item', 'three']

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result:['one', 'two', 'four', 'five', 'six', 'seven', 'last last item', 'three']

Notes

  • Use method “lmove” from redis-py.
  • Signature of the method is –
    def lmove(self, first_list: str, second_list: str, src: str = “LEFT”, dest: str = “RIGHT”) -> ResponseT
# Redis LMOVE command example in Ruby

require 'redis'

redis = Redis.new(host: "localhost", port: 6379)


# Push items to list
# Command: rpush bigboxlist one two three four five six seven "last last item"
# Result: (integer) 8
commandResult = redis.rpush("bigboxlist", ["one", "two", "three", "four", "five", "six", "seven", "last last item"])

print("Command: rpush bigboxlist one two three four five six seven \"last last item\" | Result: ", commandResult, "\n")

# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "three"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
#      8) "last last item"
commandResult = redis.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result:", commandResult, "\n")

# Check if "newlist" exists or not
# It does not exist yet
# Command: exists newlist
# Result: (integer) 0
commandResult = redis.exists("newlist")

print("Command: exists newlist | Result: ", commandResult, "\n")

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "one"
# Command: lmove bigboxlist newlist left right
# Result: "one"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Check newlist
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "two"
# Command: lmove bigboxlist newlist left right
# Result: "two"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Here is the status of newlist after second move
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
#      2) "two"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Pop item from the left(HEAD) of bigboxlist
# Push item to the left(HEAD) newlist
# The moved item is "three"
# Command: lmove bigboxlist newlist left left
# Result: "three"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "LEFT")

print("Command: lmove bigboxlist newlist left left | Result: ", commandResult, "\n")

# Status of newlist after the LMOVE operation
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Perform LMOVE multiple times
# Command: lmove bigboxlist newlist left right
# Result: "four"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Command: lmove bigboxlist newlist left right
# Result: "five"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Command: lmove bigboxlist newlist left right
# Result: "six"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Command: lmove bigboxlist newlist left right
# Result: "seven"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Check status of mylist
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Pop item from the left(HEAD) of bigboxlist
# Push item to the right(TAIL) newlist
# The moved item is "last last item", this is the last item of bigboxlist
# Command: lmove bigboxlist newlist left right
# Result: "last last item"
commandResult = redis.lmove("bigboxlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove bigboxlist newlist left right | Result: ", commandResult, "\n")

# Check newlist
# It has all the items now from bigboxlist
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
#      8) "last last item"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Check items of bigboxlist
# This is empty now all the items are popped out of it
# Command: lrange bigboxlist 0 -1
# Result: (empty array)
commandResult = redis.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result:", commandResult, "\n")

# Check if bigboxlist key exists anymore
# It does not exist. As it was deleted when the last item was popped out of it.
# Command: exists bigboxlist
# Result: (integer) 0
commandResult = redis.exists("bigboxlist")

print("Command: exists bigboxlist | Result: ", commandResult, "\n")

# Set a string value
# Command: set firstkey "some value here"
# Result: OK
commandResult = redis.set("firstkey", "some value here")

print("Command: set firstkey \"some value here\" | Result: ", commandResult, "\n")

# Try to use a string type key in the LMOVE
# It returns an error
# Command: lmove newlist firstkey left right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.lmove("newlist", "firstkey", "LEFT", "RIGHT")

    print("Command: lmove newlist firstkey left right | Result: ", commandResult, "\n")
rescue => e
    print("Command: lmove newlist firstkey left right | Error: " , e, "\n")
end

# Command: lmove firstkey newlist left right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.lmove("firstkey", "newlist", "LEFT", "RIGHT")

    print("Command: lmove firstkey newlist left right | Result: ", commandResult, "\n")
rescue => e
    print("Command: lmove firstkey newlist left right | Error: ", e, "\n")
end

# Use a non existing list/key as source
# Nothing is added to the destination list, as there is nothing in the source
# (nil) is retuned as a result
# Command: lmove nonexistingsource newlist left right
# Result: (nil)
commandResult = redis.lmove("nonexistingsource", "newlist", "LEFT", "RIGHT")

print("Command: lmove nonexistingsource newlist left right | Result: ", commandResult, "\n")

# Check the nonexistingsource
# Command: lrange nonexistingsource 0 -1
# Result: (empty array)
commandResult = redis.lrange("nonexistingsource", 0, -1)

print("Command: lrange nonexistingsource 0 -1 | Result:", commandResult, "\n")

# Check even if the key exist
# It does not exist
# Command: exists nonexistingsource
# Result: (integer) 0
commandResult = redis.exists("nonexistingsource")

print("Command: exists nonexistingsource | Result: ", commandResult, "\n")

# Check if newlist was affected in any way by the previous LMOVE operation
# It was not affected, as the sources did not exists
# Command: lrange newlist 0 -1
# Result:
#      1) "three"
#      2) "one"
#      3) "two"
#      4) "four"
#      5) "five"
#      6) "six"
#      7) "seven"
#      8) "last last item"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Use the same list as source and destination
# Command: lmove newlist newlist left right
# Result: "three"
commandResult = redis.lmove("newlist", "newlist", "LEFT", "RIGHT")

print("Command: lmove newlist newlist left right | Result: ", commandResult, "\n")

# Let's check the list
# "three" was moved from left/head and added to right/tail
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "four"
#      4) "five"
#      5) "six"
#      6) "seven"
#      7) "last last item"
#      8) "three"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

# Use the same list as source and desitnation
# Pop and push at the same end
# Command: lmove newlist newlist left left
# Result: "one"
commandResult = redis.lmove("newlist", "newlist", "LEFT", "LEFT")

print("Command: lmove newlist newlist left left | Result: ", commandResult, "\n")

# Last operation results in the same list, as the item was popped and pushed at the same end
# Command: lrange newlist 0 -1
# Result:
#      1) "one"
#      2) "two"
#      3) "four"
#      4) "five"
#      5) "six"
#      6) "seven"
#      7) "last last item"
#      8) "three"
commandResult = redis.lrange("newlist", 0, -1)

print("Command: lrange newlist 0 -1 | Result:", commandResult, "\n")

Output:

Command: rpush bigboxlist one two three four five six seven "last last item" | Result: 8

Command: lrange bigboxlist 0 -1 | Result:["one", "two", "three", "four", "five", "six", "seven", "last last item"]

Command: exists newlist | Result: 0

Command: lmove bigboxlist newlist left right | Result: one
Command: lrange newlist 0 -1 | Result:["one"]

Command: lmove bigboxlist newlist left right | Result: two
Command: lrange newlist 0 -1 | Result:["one", "two"]

Command: lmove bigboxlist newlist left left | Result: three
Command: lrange newlist 0 -1 | Result:["three", "one", "two"]

Command: lmove bigboxlist newlist left right | Result: four
Command: lmove bigboxlist newlist left right | Result: five
Command: lmove bigboxlist newlist left right | Result: six
Command: lmove bigboxlist newlist left right | Result: seven
Command: lrange newlist 0 -1 | Result:["three", "one", "two", "four", "five", "six", "seven"]

Command: lmove bigboxlist newlist left right | Result: last last item
Command: lrange newlist 0 -1 | Result:["three", "one", "two", "four", "five", "six", "seven", "last last item"]

Command: lrange bigboxlist 0 -1 | Result:[]
Command: exists bigboxlist | Result: 0

Command: set firstkey "some value here" | Result: OK

Command: lmove newlist firstkey left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmove firstkey newlist left right | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmove nonexistingsource newlist left right | Result: 
Command: lrange nonexistingsource 0 -1 | Result:[]
Command: exists nonexistingsource | Result: 0

Command: lrange newlist 0 -1 | Result:["three", "one", "two", "four", "five", "six", "seven", "last last item"]

Command: lmove newlist newlist left right | Result: three
Command: lrange newlist 0 -1 | Result:["one", "two", "four", "five", "six", "seven", "last last item", "three"]

Command: lmove newlist newlist left left | Result: one
Command: lrange newlist 0 -1 | Result:["one", "two", "four", "five", "six", "seven", "last last item", "three"]

Notes

  • Use method “lmove” from the redis-rb.
  • Signature of the method is-
    # @param [String] source source key
    # @param [String] destination destination key
    # @param [String, Symbol] where_source from where to remove the element from the source list
    #     e.g. ‘LEFT’ – from head, ‘RIGHT’ – from tail
    # @param [String, Symbol] where_destination where to push the element to the source list
    #     e.g. ‘LEFT’ – to head, ‘RIGHT’ – to tail
    #
    # @return [nil, String] the element, or nil when the source key does not exist

    def lmove(source, destination, where_source, where_destination)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
LPUSH Command Details
RPUSH Command Details
LRANGE Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.