GithubHelp home page GithubHelp logo

isabella232 / redis-session-store Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 18f/redis-session-store

0.0 0.0 0.0 147 KB

A simple session store for Rails based on Redis.

Home Page: http://github.com/roidrage/redis-session-store

License: MIT License

Ruby 100.00%

redis-session-store's Introduction

Redis Session Store

Build Status Code Climate Gem Version

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:

My::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://host:12345/2',
  }
}

Redis unavailability handling

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

My::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
My::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.

My::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.

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.

redis-session-store's People

Contributors

bbatsov avatar bmarini avatar cosine avatar dplummer avatar goncalossilva avatar jjeb avatar meatballhat avatar michaelxavier avatar monfresh avatar olek avatar pkarman avatar quarklemotion avatar roidrage avatar tlossen avatar todd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.