百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

redis-session-store

> 编程语言
开源

基于 Redis 的简单 Rails 会话存储。

371 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

基于 Redis 的简单 Rails 会话存储。

Redis Session Store

A simple Redis-based session store for Rails. But why, you ask, when there's redis-store? redis-store is a one-size-fits-all solution, and I found it not to work properly with Rails, mostly due to a problem that seemed to lie in Rack's Abstract::ID class. I wanted something that worked, so I blatantly stole the code from Rails' MemCacheStore and turned it into a Redis version. No support for fancy stuff like distributed storage across several Redis instances. Feel free to add what you see fit.

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-session-store'

Configuration

See lib/redis-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_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_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_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_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.

Rails 2 Compatibility

This gem is currently only compatible with Rails 3+. If you need Rails 2 compatibility, be sure to pin to a lower version like so:

gem 'redis-session-store', '< 0.3'

Contributing, Authors, & License

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言