Module: RedisSelector
- Defined in:
- lib/redis_selector.rb,
lib/redis_selector/version.rb
Constant Summary collapse
- DEFAULT_REDIS =
{ 'host' => 'localhost', }.freeze
- VERSION =
"0.1.0"
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
-
.mock_redises ⇒ Object
Returns the value of attribute mock_redises.
-
.mocking ⇒ Object
Returns the value of attribute mocking.
Class Method Summary collapse
Instance Method Summary collapse
-
#with_redis(what) ⇒ Object
Rather than have one big global Redis, we let you get a Redis for a specific purpose.
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
11 12 13 |
# File 'lib/redis_selector.rb', line 11 def config @config end |
.mock_redises ⇒ Object
Returns the value of attribute mock_redises.
10 11 12 |
# File 'lib/redis_selector.rb', line 10 def mock_redises @mock_redises end |
.mocking ⇒ Object
Returns the value of attribute mocking.
9 10 11 |
# File 'lib/redis_selector.rb', line 9 def mocking @mocking end |
Class Method Details
.configure(config) ⇒ Object
26 27 28 |
# File 'lib/redis_selector.rb', line 26 def self.configure(config) self.config = config end |
.mock! ⇒ Object
15 16 17 18 |
# File 'lib/redis_selector.rb', line 15 def self.mock! require 'mock_redis' self.mocking = true end |
.unmock! ⇒ Object
20 21 22 23 |
# File 'lib/redis_selector.rb', line 20 def self.unmock! require 'redis' self.mocking = false end |
Instance Method Details
#with_redis(what) ⇒ Object
Rather than have one big global Redis, we let you get a Redis for a specific purpose. That way, if thing X’s data set gets too large, you can point Redis.for(X) at a different redis-server instance, thus allowing for sharding based on logical data grouping.
This doesn’t address the problem of data migration.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/redis_selector.rb', line 37 def with_redis(what) redis_selector = Module.nesting[0] config = redis_selector.config || {} redis_info = (config[what] || config['default'] || DEFAULT_REDIS). inject({}) do |opts, (k, v)| opts.merge!(k.to_sym => v) end redis = if redis_selector.mocking # A MockRedis instance doesn't persist anywhere once we # drop a reference to it, while a real Redis # does. That's why we hold onto this mock like this; # otherwise, repeated calls to with_redis(:foo) each get # a completely empty mock. redis_selector.mock_redises[redis_info[:host]] ||= MockRedis.new(redis_info) else Redis.new(redis_info) end result = yield redis result ensure redis.quit if redis end |