0.01
No commit activity in last 3 years
No release in over 3 years
Provides `Rails.redis_pool` & `Rails.redis` methods and configuration via `database.yml`.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 0.6.0
~> 10.0
~> 3.1.0
~> 1.2.0

Runtime

 Project Readme

PooledRedis

Gem Version Build Status

Simple way to access redis connections without global variables.

Provides Rails.redis_pool & Rails.redis methods and configuration via database.yml. You can add this methods to custom modules.

Installation

Add this line to your application's Gemfile:

gem 'pooled_redis'

Usage

  • Add redis section to your database.yml with options supported by Redis.new
development:
  redis:
    db: 2
production:
  redis:
    url: 'redis://mymaster'
    sentinels:
      - host: host
      - host: other
  • You can also provide pool & timeout values for ConnectionPool.
  • Use debug: true to set redis logger to Rails.logger.
  • Pass namespace if you want to use redis-namespace.
  • Use Rails.redis_pool & Rails.redis method.

PooledRedis uses ConnectionPool for pooling connections. .redis returns proxy object that checkouts connection for every method call. So you may want to avoid it for bulk operations.

Rails.cache configuration & Redis::Store support

PooledRedis provides configuration of Rails.cache via database.yml. To enable this add following to your config/application.rb (inside Application class):

PooledRedis.setup_rails_cache(self)

And cache sections to database.yml:

development:
  cache:
    adapter: redis_store
    db: 3
    expires_in: 3600

production:
  cache:
    adapter: redis_store
    url: 'redis://mycachemaster'
    sentinels:
      - host: host
      - host: other

# You can also use other adapters:
test:
  cache:
    adapter: null_store

You need to add gem 'redis-activesupport' to your Gemfile. It supports only new version of Redis::Store with support of ConnectionPool (currently it's only available in master: gem 'redis-activesupport', '~> 4.0.0', github: 'redis-store/redis-activesupport', ref: 'd09ae04').

Custom modules or without rails

  • Extend or include PooledRedis module.
  • Override redis_config method to return valid configuration.
  • Use redis_pool & redis methods.
class Storage
  extend PooledRedis

  class << self
    def redis_config
      read_your_yaml.symbolize_keys
    end
  end

  # ...

  def save
    self.class.redis.set id, to_json
  end
end

Storage.redis_pool.with { |r| r.get :some_key }
Storage.redis.get :some_key

Advanced usage

You can return hash containing :block from redis_config. This block will be used as a block to instantiate connection in ConnectionPool.

Licence

MIT