]>
Commit | Line | Data |
---|---|---|
ed9b544e | 1 | require 'redis' |
2 | require 'hash_ring' | |
3 | class DistRedis | |
4 | attr_reader :ring | |
69664139 | 5 | def initialize(opts={}) |
6 | hosts = [] | |
7 | ||
8 | db = opts[:db] || nil | |
9 | timeout = opts[:timeout] || nil | |
10 | ||
11 | raise Error, "No hosts given" unless opts[:hosts] | |
12 | ||
13 | opts[:hosts].each do |h| | |
14 | host, port = h.split(':') | |
15 | hosts << Redis.new(:host => host, :port => port, :db => db, :timeout => timeout, :db => db) | |
ed9b544e | 16 | end |
69664139 | 17 | |
18 | @ring = HashRing.new hosts | |
ed9b544e | 19 | end |
20 | ||
21 | def node_for_key(key) | |
22 | if key =~ /\{(.*)?\}/ | |
23 | key = $1 | |
24 | end | |
25 | @ring.get_node(key) | |
26 | end | |
27 | ||
28 | def add_server(server) | |
29 | server, port = server.split(':') | |
30 | @ring.add_node Redis.new(:host => server, :port => port) | |
31 | end | |
32 | ||
33 | def method_missing(sym, *args, &blk) | |
29fac617 | 34 | if redis = node_for_key(args.first.to_s) |
ed9b544e | 35 | redis.send sym, *args, &blk |
36 | else | |
37 | super | |
38 | end | |
39 | end | |
40 | ||
41 | def keys(glob) | |
42 | keyz = [] | |
43 | @ring.nodes.each do |red| | |
44 | keyz.concat red.keys(glob) | |
45 | end | |
46 | keyz | |
47 | end | |
48 | ||
49 | def save | |
50 | @ring.nodes.each do |red| | |
51 | red.save | |
52 | end | |
53 | end | |
54 | ||
55 | def bgsave | |
56 | @ring.nodes.each do |red| | |
57 | red.bgsave | |
58 | end | |
59 | end | |
60 | ||
61 | def quit | |
62 | @ring.nodes.each do |red| | |
63 | red.quit | |
64 | end | |
65 | end | |
66 | ||
67 | def delete_cloud! | |
68 | @ring.nodes.each do |red| | |
69 | red.keys("*").each do |key| | |
70 | red.delete key | |
71 | end | |
72 | end | |
73 | end | |
74 | ||
75 | end | |
76 | ||
77 | ||
78 | if __FILE__ == $0 | |
79 | ||
80 | r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381', 'localhost:6382' | |
81 | r['urmom'] = 'urmom' | |
82 | r['urdad'] = 'urdad' | |
83 | r['urmom1'] = 'urmom1' | |
84 | r['urdad1'] = 'urdad1' | |
85 | r['urmom2'] = 'urmom2' | |
86 | r['urdad2'] = 'urdad2' | |
87 | r['urmom3'] = 'urmom3' | |
88 | r['urdad3'] = 'urdad3' | |
89 | p r['urmom'] | |
90 | p r['urdad'] | |
91 | p r['urmom1'] | |
92 | p r['urdad1'] | |
93 | p r['urmom2'] | |
94 | p r['urdad2'] | |
95 | p r['urmom3'] | |
96 | p r['urdad3'] | |
97 | ||
98 | r.push_tail 'listor', 'foo1' | |
99 | r.push_tail 'listor', 'foo2' | |
100 | r.push_tail 'listor', 'foo3' | |
101 | r.push_tail 'listor', 'foo4' | |
102 | r.push_tail 'listor', 'foo5' | |
103 | ||
29fac617 | 104 | p r.pop_tail('listor') |
105 | p r.pop_tail('listor') | |
106 | p r.pop_tail('listor') | |
107 | p r.pop_tail('listor') | |
108 | p r.pop_tail('listor') | |
ed9b544e | 109 | |
110 | puts "key distribution:" | |
111 | ||
112 | r.ring.nodes.each do |red| | |
113 | p [red.port, red.keys("*")] | |
114 | end | |
115 | r.delete_cloud! | |
116 | p r.keys('*') | |
117 | ||
118 | end |