redi 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +8 -26
  2. data/lib/redi/version.rb +1 -1
  3. data/lib/redi.rb +47 -67
  4. metadata +3 -3
data/README.md CHANGED
@@ -8,35 +8,17 @@ The idea comes from http://blog.zawodny.com/2011/02/26/redis-sharding-at-craigsl
8
8
 
9
9
  gem install redi
10
10
 
11
-
12
11
  Create a configuration file:
13
12
 
14
13
  development:
15
- :keyspace:
16
- - :n0: 0
17
- - :n1: 1
18
- - :n2: 0
19
- - :n3: 1
20
- - :n4: 0
21
- - :n5: 1
22
- - :n6: 0
23
- - :n7: 1
24
- - :n8: 0
25
- - :n9: 1
26
- - :n10: 0
27
- - :n11: 1
28
- - :n12: 0
29
- - :n13: 1
30
- - :n14: 0
31
- - :n15: 1
32
- - :n16: 0
33
- :servers:
34
- - :host: 192.168.0.10
35
- :port: 6379
36
- :db: 0
37
- - :host: 192.168.0.11
38
- :port: 6380
39
- :db: 0
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
40
22
 
41
23
  The keyspace mapping is very important. It helps you manage the exact mapping of buckets to servers and is how you can scale from 2 severs to 16.
42
24
  In the example, you have 16 buckets that map evenly to 2 servers. If you decide you need more memory (more severs) you can choose a segment of your
data/lib/redi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RedisRing
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/redi.rb CHANGED
@@ -15,6 +15,14 @@ class Redi
15
15
  pool.redis_by_key(key).set(key, val)
16
16
  end
17
17
 
18
+ def self.flushall
19
+ @pool.flushall
20
+ end
21
+
22
+ def self.mock!
23
+ pool.mock!
24
+ end
25
+
18
26
  def self.pool
19
27
  @pool ||= Pool.new(self.config)
20
28
  end
@@ -33,40 +41,37 @@ class Redi
33
41
  #
34
42
  # sample configuration:
35
43
  #
36
- # :keyspace:
37
- # - :n0: 0
38
- # - :n1: 1
39
- # - :n2: 0
40
- # - :n3: 1
41
- # - :n4: 0
42
- # :servers:
43
- # - :host:
44
- # :port:
45
- # :db:
46
- # - :host:
47
- # :port:
48
- # :db:
44
+ # - :host:
45
+ # :port:
46
+ # :db:
47
+ # :buckets: 0 - 64
48
+ # - :host:
49
+ # :port:
50
+ # :db:
51
+ # :buckets: 65 - 127
49
52
  #
50
53
  class Pool
51
54
  attr_reader :keyspace, :servers
52
55
  def initialize(config)
53
- @key_type = Struct.new(:id, :to_s)
54
- keyspace = config[:keyspace]
55
-
56
- # create bucket set
57
- @buckets = keyspace.each_with_index.map {|k,i| @key_type.new(i, k.keys.first) }
56
+ key_type = Struct.new(:id, :to_s)
58
57
 
59
58
  # build server pool
60
- @servers = config[:servers].map {|cfg| Redis.new(cfg) }
61
-
62
- # create the mapping from bucket to server
63
59
  @bucket2server = {}
64
- keyspace.each do|ks|
65
- @bucket2server[ks.keys.first] = @servers[ks.values.first]
66
- end
60
+ buckets = []
61
+ @servers = config.map {|cfg|
62
+ bucket_range = cfg.delete(:buckets)
63
+ s, e = bucket_range.split('-').map {|n| n.to_i }
64
+ conn = Redis.new(cfg)
65
+ (s..e).each do|i|
66
+ bucket_name = "n#{i}"
67
+ buckets << key_type.new(i, bucket_name)
68
+ @bucket2server[bucket_name] = conn
69
+ end
70
+ conn
71
+ }
67
72
 
68
73
  # create the keyring to map redis keys to buckets
69
- @keyring = Redis::HashRing.new(@buckets)
74
+ @keyring = Redis::HashRing.new(buckets)
70
75
  end
71
76
 
72
77
  def redis_by_key(key)
@@ -74,6 +79,14 @@ class Redi
74
79
  Redis::Namespace.new(bucket.to_s, :redis => @bucket2server[bucket.to_s])
75
80
  end
76
81
 
82
+ def flushall
83
+ @servers.map {|s| s.flushall }
84
+ end
85
+
86
+ def mock!
87
+ @servers.map! {|s| Mock.new }
88
+ end
89
+
77
90
  end
78
91
 
79
92
  class Mock
@@ -106,54 +119,21 @@ require 'rubygems'
106
119
  require 'test/unit'
107
120
 
108
121
  TEST_CONFIG = %(
109
- :keyspace:
110
- - :n0: 0
111
- - :n1: 1
112
- - :n2: 0
113
- - :n3: 1
114
- - :n4: 0
115
- - :n5: 1
116
- - :n6: 0
117
- - :n7: 1
118
- - :n8: 0
119
- - :n9: 1
120
- - :n10: 0
121
- - :n11: 1
122
- - :n12: 0
123
- - :n13: 1
124
- - :n14: 0
125
- - :n15: 1
126
- - :n16: 0
127
- - :n17: 1
128
- - :n18: 0
129
- - :n19: 1
130
- - :n20: 0
131
- - :n21: 1
132
- - :n22: 0
133
- - :n23: 1
134
- - :n24: 0
135
- - :n25: 1
136
- - :n26: 0
137
- - :n27: 1
138
- - :n28: 0
139
- - :n29: 1
140
- - :n30: 0
141
- - :n31: 1
142
- - :n32: 0
143
- :servers:
144
- - :host: 127.0.0.1
145
- :port: 6379
146
- :db: 0
147
- - :host: 127.0.0.1
148
- :port: 6379
149
- :db: 1
122
+ - :host: 127.0.0.1
123
+ :port: 6379
124
+ :db: 0
125
+ :buckets: 0-64
126
+ - :host: 127.0.0.1
127
+ :port: 6379
128
+ :db: 1
129
+ :buckets: 65-127
150
130
  )
151
131
  class TestIt < Test::Unit::TestCase
152
132
 
153
133
  def test_redis_pool
154
134
  pool = Redi::Pool.new(YAML.load(TEST_CONFIG))
155
135
  redis = pool.redis_by_key('me:foo:1')
156
- assert_equal :n25, redis.namespace
136
+ assert_equal "n25", redis.namespace
157
137
  redis.flushall
158
138
  redis.set("me:foo:1", "hello")
159
139
  assert_equal "hello", redis.get("me:foo:1")
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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Todd Fisher