Rack-compatible session storage middleware providing pluggable backends including Redis, Memcache, and encrypted cookie stores for Rack and Rails applications.
Add to your Gemfile:
gem 'knot-rack-session-store', '~> 2.1'Or install directly:
gem install knot-rack-session-storerequire 'rack'
require 'rack/session/store'
# config.ru
use Rack::Session::Store::Redis,
redis_url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/0'),
key: '_myapp_session',
expire_after: 3600,
secret: ENV['SESSION_SECRET']
run MyAppuse Rack::Session::Store::Memcache,
servers: ['localhost:11211'],
key: '_myapp_session',
expire_after: 1800use Rack::Session::Store::Cookie,
key: '_myapp_session',
secret: ENV['SESSION_SECRET'],
old_secret: ENV['SESSION_SECRET_OLD'] # for key rotation# config/initializers/session_store.rb
Rails.application.config.session_store :rack_session_redis_store,
redis_url: ENV.fetch('REDIS_URL'),
key: '_myapp_session',
expire_after: 2.hours| Option | Default | Description |
|---|---|---|
key |
_session_id |
Cookie name for the session ID |
expire_after |
nil (browser session) |
TTL in seconds |
secret |
required | HMAC secret for signed cookies |
namespace |
rack:session |
Redis/Memcache key prefix |
- Ruby >= 2.7.0
redisgem for Redis backenddalligem for Memcache backend
MIT License. See LICENSE for details.