Module: PooledRedis
- Defined in:
- lib/pooled_redis.rb,
lib/pooled_redis/version.rb
Constant Summary collapse
- VERSION =
'0.2.1'
Class Method Summary collapse
Instance Method Summary collapse
- #redis ⇒ Object
-
#redis_config ⇒ Object
Override this method unless using Rails.
- #redis_pool ⇒ Object
- #redis_pool_config ⇒ Object
Class Method Details
.extend_rails ⇒ Object
39 40 41 |
# File 'lib/pooled_redis.rb', line 39 def extend_rails Rails.class_eval { extend PooledRedis } if defined?(Rails) end |
.gem_version ⇒ Object
4 5 6 |
# File 'lib/pooled_redis/version.rb', line 4 def self.gem_version Gem::Version.new VERSION end |
.setup_rails_cache(app) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pooled_redis.rb', line 43 def setup_rails_cache(app) # We need to use initializer to be able to access # Rails.configuration.database_configuration. app.initializer :configure_cache, before: :initialize_cache, group: :all do cache_config = Rails.configuration. database_configuration[Rails.env]['cache'].try!(:with_indifferent_access) adapter = cache_config.try!(:delete, :adapter).try!(:to_sym) next unless adapter if adapter == :redis_store # Workaround to support `:db` option: pool_config ||= { pool: cache_config.delete(:pool) || 5, timeout: cache_config.delete(:timeout) || 5, } cache_config = { pool: ConnectionPool.new(pool_config) { Redis::Store.new(cache_config) } } end app.config.cache_store = adapter, cache_config end end |
Instance Method Details
#redis ⇒ Object
96 97 98 |
# File 'lib/pooled_redis.rb', line 96 def redis @redis ||= redis_pool.simple_connection end |
#redis_config ⇒ Object
Override this method unless using Rails.
67 68 69 70 71 72 73 74 |
# File 'lib/pooled_redis.rb', line 67 def redis_config @redis_config ||= begin config = ActiveRecord::Base.connection_config[:redis].with_indifferent_access config[:logger] = Rails.logger if config.delete(:debug) require 'redis/namespace' if config[:namespace] config end end |
#redis_pool ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pooled_redis.rb', line 83 def redis_pool @redis_pool ||= begin block = if redis_config[:block] redis_config[:block] elsif redis_config[:namespace] -> { Redis::Namespace.new(redis_config[:namespace], redis: Redis.new(redis_config)) } else -> { Redis.new(redis_config) } end ConnectionPool.new(redis_pool_config, &block) end end |
#redis_pool_config ⇒ Object
76 77 78 79 80 81 |
# File 'lib/pooled_redis.rb', line 76 def redis_pool_config @redis_pool_config ||= { pool: redis_config.delete(:pool) || 5, timeout: redis_config.delete(:timeout) || 5, } end |