Commit 33e3ca0
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 7eff2f2 commit 33e3ca0
File tree
4 files changed
+68
-38
lines changed- lib/redis_client
- cluster
- test/redis_client
4 files changed
+68
-38
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 | + | |
107 | 104 | | |
108 | 105 | | |
109 | 106 | | |
| |||
128 | 125 | | |
129 | 126 | | |
130 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
131 | 140 | | |
132 | 141 | | |
133 | 142 | | |
| |||
| 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 | 16 | | |
14 | | - | |
15 | | - | |
16 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
17 | 44 | | |
18 | 45 | | |
19 | 46 | | |
20 | | - | |
| 47 | + | |
21 | 48 | | |
22 | 49 | | |
23 | 50 | | |
24 | | - | |
25 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
26 | 54 | | |
27 | | - | |
| 55 | + | |
28 | 56 | | |
29 | 57 | | |
30 | 58 | | |
31 | 59 | | |
32 | | - | |
| 60 | + | |
33 | 61 | | |
34 | 62 | | |
35 | 63 | | |
36 | 64 | | |
37 | 65 | | |
38 | 66 | | |
39 | 67 | | |
40 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
41 | 74 | | |
42 | 75 | | |
43 | 76 | | |
| |||
| 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 | |
|---|---|---|---|
| |||
461 | 461 | | |
462 | 462 | | |
463 | 463 | | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
469 | 471 | | |
470 | 472 | | |
471 | 473 | | |
472 | 474 | | |
473 | 475 | | |
474 | 476 | | |
475 | 477 | | |
476 | | - | |
477 | | - | |
| 478 | + | |
| 479 | + | |
478 | 480 | | |
479 | 481 | | |
480 | 482 | | |
| |||
0 commit comments