-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an explicit #watch method to RedisClient::Cluster
This returns a "watcher" object, which can either be used for three things: * To add keys to be watched on the same connection (by calling #watch * To begin a MULTI transaction on the connection (by calling #multi) * To UNWATCH the connection and return it to its original state (by calling... #unwatch) This means that the following pattern becomes possible in redis-cluster-client: ``` client.watch(["{slot}k1", "{slot}k2"]) do |watcher| # Further reads can be performed with client directly; this is # perfectly safe and they will be individually redirected if required # as normal. # If a read on a slot being watched is redirected, that's also OK, # because it means the final EXEC will fail (since the watch got # modified). current_value = client.call('GET', '{slot}k1') some_other_thing = client.call('GET', '{slot}something_unwatched') # You can add more keys to the watch if required # This could raise a redireciton error, and cause the whole watch # block to be re-attempted watcher.watch('{slot}differet_key') different_value = client.call('GET', '{slot}different_key') if do_transaction? # Once you're ready to perform a transaction, you can use multi... watcher.multi do |tx| # tx is now a pipeliend RedisClient::Cluster::Transaction # instance, like normal multi tx.call('SET', '{slot}k1', 'new_value') tx.call('SET', '{slot}k2', 'new_value') end # At this point, the transaction is committed else # You can abort the transaction by calling unwatch # (this will also happen if an exception is thrown) watcher.unwatch end end ``` This interface is what's required to make redis-clustering/redis-rb work correctly, I think.
- Loading branch information
KJ Tsanaktsidis
committed
Feb 21, 2024
1 parent
3230791
commit c7b69df
Showing
4 changed files
with
60 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters