Low commit activity in last 3 years
Rails session store using low-level redis-client for Redis 6+ For great glory!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 13
~> 3
~> 1.25
~> 0.21

Runtime

>= 5.2.4.1, < 8
 Project Readme

RedisClient Session Store

Code Climate Gem Version

A fork of redis-session-store using low-level RESP3 wrapper redis-client instead of idiomatic redis.

This library doesn't offer anything related to caching, and is only suitable for Rails applications. For other frameworks or drop-in support for caching, check out redis-store.

Installation

For Rails 3+, adding this to your Gemfile will do the trick.

gem 'redis-client-session-store'

Configuration

See lib/redis-client-session-store.rb for a list of valid options. In your Rails app, throw in an initializer with the following contents:

Rails.application.config.session_store :redis_client_session_store,
  key: 'your_session_key',
  redis: {
    expire_after: 120.minutes,  # cookie expiration
    ttl: 120.minutes,           # Redis expiration, defaults to 'expire_after'
    key_prefix: 'myapp:session:',
    url: 'redis://localhost:6379/0',
  }

Redis unavailability handling

If you want to handle cases where Redis is unavailable, a custom callable handler may be provided as on_redis_down:

Rails.application.config.session_store :redis_client_session_store,
  # ... other options ...
  on_redis_down: ->(e, env, sid) { do_something_will_ya!(e) }
  redis: {
    # ... redis options ...
  }

Serializer

By default the Marshal serializer is used. With Rails 4, you can use JSON as a custom serializer:

  • :json - serialize cookie values with JSON (Requires Rails 4+)
  • :marshal - serialize cookie values with Marshal (Default)
  • :hybrid - transparently migrate existing Marshal cookie values to JSON (Requires Rails 4+)
  • CustomClass - You can just pass the constant name of any class that responds to .load and .dump
Rails.application.config.session_store :redis_client_session_store,
  # ... other options ...
  serializer: :hybrid
  redis: {
    # ... redis options ...
  }

Note: Rails 4 is required for using the :json and :hybrid serializers because the Flash object doesn't serialize well in 3.2. See Rails #13945 for more info.

Session load error handling

If you want to handle cases where the session data cannot be loaded, a custom callable handler may be provided as on_session_load_error which will be given the error and the session ID.

Rails.application.config.session_store :redis_client_session_store,
  # ... other options ...
  on_session_load_error: ->(e, sid) { do_something_will_ya!(e) }
  redis: {
    # ... redis options ...
  }

Note The session will always be destroyed when it cannot be loaded.

Other notes

It returns with_indifferent_access if ActiveSupport is defined.

Contributing, Authors, & License

See CONTRIBUTING.md, AUTHORS.md, and LICENSE, respectively.