UNWATCH
Stops monitoring all keys that were being watched for changes in the current transaction.
Syntax
UNWATCH
Details
- Available since: 1.0.0
- Time complexity: O(1).
- ACL categories:
@transaction
,@write
The UNWATCH
command is used to cancel the monitoring of keys that were being watched for changes during a transaction. When a transaction is started with the MULTI
command and WATCH
was used to monitor specific keys, UNWATCH
stops the monitoring of all those keys. This is useful when you want to abort the current transaction without executing any commands and prevent any potential conflicts or issues due to key changes.
Examples
Unwatching Keys
-
Start a transaction and watch keys:
WATCH key1 key2
MULTI -
Queue commands:
SET key1 "value1"
SET key2 "value2" -
Unwatch the keys if needed:
UNWATCH
-
Decide not to execute the transaction and call
DISCARD
:DISCARD
After calling UNWATCH
, the keys key1
and key2
will no longer be monitored for changes. The transaction can now be discarded without executing the commands.
Using UNWATCH in a Transaction
-
Watch a key:
WATCH key1
-
Start a transaction:
MULTI
-
Queue commands:
SET key1 "value1"
-
Unwatch the key and execute:
UNWATCH
EXEC
In this case, UNWATCH
cancels the monitoring of key1
, and the EXEC
command will not execute if any changes occurred to key1
while being watched.
RESP2/RESP3 Reply
- Simple string reply:
OK
Example reply for UNWATCH
:
"OK"
This indicates that all watched keys have been unmonitored and the transaction state has been reset regarding key monitoring.
Notes
- The
UNWATCH
command is typically used in conjunction withWATCH
andMULTI
to manage transaction states and handle potential conflicts with watched keys. UNWATCH
is particularly useful when you want to ensure that watched keys are no longer monitored before committing or discarding a transaction.