RedisClient Session Store
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 withJSON
(Requires Rails 4+) -
:marshal
- serialize cookie values withMarshal
(Default) -
:hybrid
- transparently migrate existingMarshal
cookie values toJSON
(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.