w3resource

Redis Lists: RPUSHX

Redis RPUSHX Command

Redis RPUSHX command is used to insert a value at the tail of the list stored at key, only if a key already exists and holds a list. In contrary to RPUSH, no operation will be performed when a key does not yet exist.

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

  • Conditional Appending: Add elements to the end of a list only if the list is already present.
  • Queue Management: Ensure elements are added to active queues without creating new ones.
  • Event Handling: Append events to the end of an existing event list for immediate processing.
  • Data Integrity: Prevent the accidental creation of lists when trying to append elements.

Syntax:

RPUSHX KEY_NAME VALUE1..VALUEN

Available since

2.2.0.

Return Value

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

Return Value Type

Integer

Example: Redis RPUSHX

127.0.0.1:6379> del mycolor1
(integer) 1
127.0.0.1:6379> RPUSH mycolor1 white
(integer) 1
127.0.0.1:6379> RPUSHX mycolor1 black
(integer) 2
127.0.0.1:6379> RPUSHX mycolor1 red
(integer) 3
127.0.0.1:6379> RPUSHX 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) "white"
2) "black"
3) "red"

Previous: RPUSH
Next: Redis Sets



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://w3resource.com/redis/redis-rpushx-key-value.php