redi 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.md +4 -0
- data/README.md +14 -14
- data/lib/redi.rb +25 -5
- data/lib/redi/version.rb +1 -1
- metadata +5 -4
data/ChangeLog.md
ADDED
data/README.md
CHANGED
@@ -6,26 +6,26 @@ Pooled redis puts a layer of indirection between server pool and key ring
|
|
6
6
|
|
7
7
|
The idea comes from http://blog.zawodny.com/2011/02/26/redis-sharding-at-craigslist/
|
8
8
|
|
9
|
-
|
9
|
+
gem install redi
|
10
10
|
|
11
11
|
Create a configuration file:
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
development:
|
14
|
+
- :host: 192.168.0.10
|
15
|
+
:port: 6379
|
16
|
+
:db: 0
|
17
|
+
:buckets: 0 - 64
|
18
|
+
- :host: 192.168.0.11
|
19
|
+
:port: 6380
|
20
|
+
:db: 0
|
21
|
+
:buckets: 65 - 127
|
22
22
|
|
23
|
-
The
|
24
|
-
|
25
|
-
|
23
|
+
The configuration should look like a normal redis configuration with the addition of a buckets key. This tells redi how many buckets it should
|
24
|
+
hash keys to before mapping them to the configured server. In the example above, it would be possible to scale those 2 servers up to 128 servers without
|
25
|
+
rekeying, you can follow the 5 steps below to add a new server each time.
|
26
26
|
|
27
27
|
1. create new server
|
28
28
|
2. identify buckets to move to new server
|
29
29
|
3. setup new server as slave to replicate
|
30
30
|
4. update configuration to point buckets to new server
|
31
|
-
5.
|
31
|
+
5. use bucket key prefixes to prune old keys from old server.
|
data/lib/redi.rb
CHANGED
@@ -15,16 +15,20 @@ class Redi
|
|
15
15
|
pool.redis_by_key(key).set(key, val)
|
16
16
|
end
|
17
17
|
|
18
|
+
def self.del(key)
|
19
|
+
pool.redis_by_key(key).del(key)
|
20
|
+
end
|
21
|
+
|
18
22
|
def self.flushall
|
19
23
|
pool.flushall
|
20
24
|
end
|
21
25
|
|
22
26
|
def self.mock!
|
23
|
-
pool.mock!
|
27
|
+
pool(true).mock!
|
24
28
|
end
|
25
29
|
|
26
|
-
def self.pool
|
27
|
-
@pool ||= Pool.new(self.config)
|
30
|
+
def self.pool(mock=false)
|
31
|
+
@pool ||= Pool.new(self.config,mock)
|
28
32
|
end
|
29
33
|
|
30
34
|
def self.config=(config)
|
@@ -52,7 +56,7 @@ class Redi
|
|
52
56
|
#
|
53
57
|
class Pool
|
54
58
|
attr_reader :keyspace, :servers
|
55
|
-
def initialize(config)
|
59
|
+
def initialize(config,mock=false)
|
56
60
|
key_type = Struct.new(:id, :to_s)
|
57
61
|
|
58
62
|
# build server pool
|
@@ -61,7 +65,11 @@ class Redi
|
|
61
65
|
@servers = config.map {|cfg|
|
62
66
|
bucket_range = cfg.delete(:buckets)
|
63
67
|
s, e = bucket_range.split('-').map {|n| n.to_i }
|
64
|
-
|
68
|
+
if mock
|
69
|
+
conn = Mock.new
|
70
|
+
else
|
71
|
+
conn = Redis.new(cfg)
|
72
|
+
end
|
65
73
|
(s..e).each do|i|
|
66
74
|
bucket_name = "n#{i}"
|
67
75
|
buckets << key_type.new(i, bucket_name)
|
@@ -74,6 +82,11 @@ class Redi
|
|
74
82
|
@keyring = Redis::HashRing.new(buckets)
|
75
83
|
end
|
76
84
|
|
85
|
+
def qualified_key_for(key)
|
86
|
+
bucket = @keyring.get_node(key)
|
87
|
+
"#{bucket.to_s}:#{key}"
|
88
|
+
end
|
89
|
+
|
77
90
|
def redis_by_key(key)
|
78
91
|
bucket = @keyring.get_node(key)
|
79
92
|
Redis::Namespace.new(bucket.to_s, :redis => @bucket2server[bucket.to_s])
|
@@ -85,6 +98,9 @@ class Redi
|
|
85
98
|
|
86
99
|
def mock!
|
87
100
|
@servers.map! {|s| Mock.new }
|
101
|
+
@bucket2server.keys.each_with_index do|k,i|
|
102
|
+
@bucket2server[k] = @servers[i % @servers.size]
|
103
|
+
end
|
88
104
|
end
|
89
105
|
|
90
106
|
end
|
@@ -98,6 +114,10 @@ class Redi
|
|
98
114
|
@store[key]
|
99
115
|
end
|
100
116
|
|
117
|
+
def del(key)
|
118
|
+
@store.delete(key)
|
119
|
+
end
|
120
|
+
|
101
121
|
def set(key, val)
|
102
122
|
@store[key] = val
|
103
123
|
end
|
data/lib/redi/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Todd Fisher
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-10-
|
18
|
+
date: 2011-10-14 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -56,6 +56,7 @@ extensions: []
|
|
56
56
|
extra_rdoc_files: []
|
57
57
|
|
58
58
|
files:
|
59
|
+
- ChangeLog.md
|
59
60
|
- Gemfile
|
60
61
|
- Gemfile.lock
|
61
62
|
- README.md
|