Skip to content
Redis stores for Ruby on Rails
Branch: master
Clone or download
Latest commit 2a7e362 Mar 20, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Appraisals Apr 13, 2017
gemfiles
lib Release v5.0.2 Apr 6, 2017
test Use always double quote strings, as official style guide recommends Jan 12, 2016
.gitignore Remove redis-store from direct dependencies Aug 12, 2017
.travis.yml
Appraisals Appraisals Apr 13, 2017
CHANGELOG.md Use markdown extension for CHANGELOG Apr 10, 2017
Gemfile Comment out local dependencies. Contributors can uncomment as needed Apr 6, 2017
MIT-LICENSE Moved back from jodosha/redis-store Aug 23, 2013
README.md Update README.md Mar 20, 2019
Rakefile Use always double quote strings, as official style guide recommends Jan 12, 2016
redis-rails.gemspec Remove redis-store from direct dependencies Aug 12, 2017

README.md

Redis stores for Ruby on Rails

redis-rails provides a full set of stores (Cache, Session, HTTP Cache) for Ruby on Rails. See the main redis-store readme for general guidelines.

A quick note about Rails 5.2

Rails 5.2.0 includes a Redis cache store out of the box, so you don't really need this gem anymore if you just need to store the fragment cache in Redis. Maintenance on the redis-activesupport gem will continue for security and compatibility issues, but we are no longer accepting new features. We are still actively maintaining all other gems in the redis-store family.

Installation

Add the following to your Gemfile:

gem 'redis-rails'

Usage

redis-rails packages storage drivers for Redis which implement the ActiveSupport fragment caching and ActionDispatch / Rack session storage APIs. The following section(s) explain how to configure each store:

Rails Fragment Cache

Configure the fragment cache store in config/environments/production.rb like so:

config.cache_store = :redis_store, "redis://localhost:6379/0/cache", { expires_in: 90.minutes }

The ActiveSupport::Cache::Store implementation assumes that your backend store (Redis, Memcached, etc) will be available at boot time. If you cannot guarantee this, you can use the raise_errors: false option to rescue connection errors.

You can also provide a hash instead of a URL:

config.cache_store = :redis_store, {
  host: "localhost",
  port: 6379,
  db: 0,
  password: "mysecret",
  namespace: "cache"
}, {
  expires_in: 90.minutes
}

Session Storage

You can also store your session data in Redis, keeping user-specific data isolated, shared, and highly available. Built upon redis-rack, we present the session data to the user as a signed/encrypted cookie, but we persist the data in Redis.

Add the following to your config/initializers/session_store.rb to use Redis as the session store.

MyApplication::Application.config.session_store :redis_store,
  servers: ["redis://localhost:6379/0/session"],
  expire_after: 90.minutes,
  key: "_#{Rails.application.class.parent_name.downcase}_session",
  threadsafe: false

A brief run-down of these options...

  • servers is an Array of Redis servers that we will attempt to find data from. This uses the same syntax as :redis_store
  • expire_after is the default TTL of session keys. This is also set as the expiry time of any cookies generated by the session store.
  • key is the name of the cookie on the client side
  • threadsafe is for applications that run on multiple instances. Set this to false if you want to disable the global mutex lock on session data. It's true by default, meaning the mutex will be enabled.

HTTP Caching

We also provide an adapter for Rack::Cache that lets you store HTTP caching data in Redis. To take advantage of this, add the following to Gemfile:

group :production do
  gem 'redis-rack-cache'
end

Then, add the following to config/environments/production.rb:

# config/environments/production.rb
config.action_dispatch.rack_cache = {
  metastore: "redis://localhost:6379/1/metastore",
  entitystore: "redis://localhost:6379/1/entitystore"
}

Usage with Redis Sentinel

You can also use Redis Sentinel to manage a cluster of Redis servers for high-availability data access. To do so, configure the sentinel servers like so:

sentinel_config = {
  url: "redis://mymaster/0",
  role: "master",
  sentinels: [{
    host: "127.0.0.1",
    port: 26379
  },{
    host: "127.0.0.1",
    port: 26380
  },{
    host: "127.0.0.1",
    port: 26381
  }]
}

You can then include this in your cache store configuration within config/environments/production.rb:

config.cache_store = :redis_store, sentinel_config.merge(
  namespace: "cache",
  expires_in: 1.days
)
config.session_store :redis_store, {
  servers: [
    sentinel_config.merge(
      namespace: "sessions"
    )
  ],
  expire_after: 2.days
}

Usage with Redis Cluster

Since there is no official Redis Cluster client for Ruby, teams can choose to use the Redis Cluster Proxy or the RedisCluster client available at https://github.com/antirez/redis-rb-cluster. We provide an option called :avoid_multi_commands, which can be set to true in order to avoid errors associated with reading from clusters.

Running tests

gem install bundler
git clone git://github.com/redis-store/redis-rails.git
cd redis-rails
RAILS_VERSION=5.0.1 bundle install
RAILS_VERSION=5.0.1 bundle exec rake

If you are on Snow Leopard, run env ARCHFLAGS="-arch x86_64" bundle exec rake

Status

Gem Version Build Status Code Climate

Copyright

2009 - 2018 Luca Guidi - http://lucaguidi.com, released under the MIT license

You can’t perform that action at this time.