Commit df9cb70
KJ Tsanaktsidis
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.1 parent 3230791 commit df9cb70
File tree
4 files changed
+64
-37
lines changed- lib/redis_client
- cluster
- test/redis_client
4 files changed
+64
-37
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
16 | 37 | | |
17 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
18 | 45 | | |
19 | 46 | | |
20 | | - | |
21 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
22 | 50 | | |
23 | | - | |
| 51 | + | |
24 | 52 | | |
25 | 53 | | |
26 | 54 | | |
27 | 55 | | |
28 | 56 | | |
29 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
30 | 63 | | |
31 | 64 | | |
32 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
| |||
311 | 310 | | |
312 | 311 | | |
313 | 312 | | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | 313 | | |
328 | 314 | | |
329 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
375 | 377 | | |
376 | 378 | | |
377 | 379 | | |
378 | 380 | | |
379 | 381 | | |
380 | 382 | | |
381 | 383 | | |
382 | | - | |
383 | | - | |
| 384 | + | |
| 385 | + | |
384 | 386 | | |
385 | 387 | | |
386 | 388 | | |
| |||
0 commit comments