w3resource

Redis Lists: LPUSHX

Redis LPUSHX Command

Redis LPUSHX command is used to insert value at the head of the list stored at key, only if a key already exists and holds a list.

Here are some common uses and scenarios for the "LPUSHX" command:

  • Conditional Prepending: Add elements to the beginning of a list only if the list is already present.
  • Queue Management: Ensure elements are added to active queues without creating new ones.
  • Task Prioritization: Insert high-priority tasks at the front of an existing task list.
  • Event Handling: Add events to the start of an existing event list for immediate processing.
  • Data Integrity: Prevent the accidental creation of lists when trying to prepend elements.

Syntax:

LPUSHX KEY_NAME VALUE1.. VALUEN     

Available since

2.2.0.

Return Value

Integer replies the length of the list after the push operations.

Return Value Type

Integer

Example: Redis LPUSHX

127.0.0.1:6379> LPUSH mycolor1 white
(integer) 1
127.0.0.1:6379> LPUSHX mycolor1 black
(integer) 2
127.0.0.1:6379> LPUSHX mycolor1 red
(integer) 3
127.0.0.1:6379> LPUSHX mycolor2 blue
(integer) 0

The key mycolor2 not exists, so return 0

127.0.0.1:6379> EXISTS mycolor2
(integer) 0
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "red"
2) "black"
3) "white"

Previous: LPUSH
Next: LREM



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-lpushx-key-value.php