• Stars
    star
    366
  • Rank 116,547 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple session store for Rails based on Redis.

Redis Session Store

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:

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.

More Repositories

1

lograge

An attempt to tame Rails' default policy to log everything.
Ruby
3,456
star
2

s3itch

An S3 proxy for Skitch's WebDAV sharing support
Ruby
93
star
3

coffee

77
star
4

crumble

How did these breadcrumbs in your Rails application? Oh right, with this plugin!
Ruby
74
star
5

relink

A Redis-based URL shortener.
Ruby
46
star
6

macistrano

A RubyCocoa client for Webistrano (http://labs.peritor.com/webistrano)
Ruby
43
star
7

run_later

A port of Merb's run_later functionality to thread-safe Rails (>= 2.2).
Ruby
38
star
8

paperplanes

My personal website, running on jekyll.
Ruby
31
star
9

shoulda-addons

Awesome add-ons to make Shoulda more awesome.
Ruby
27
star
10

riaktivity

Store user timelines in Riak.
Ruby
26
star
11

sinatra-statsd

Ruby
24
star
12

dotvim

My vim configuration stuff, based on the things that came with the Vim PeepCode.
Vim Script
22
star
13

megazoomer

Simply a fork of the original SIMBL plugin to enable full screen support for Cocoa applications UB-enabled. Original author is Ian Henderson (http://ianhenderson.org/megazoomer.html).
Objective-C
21
star
14

cap-ext-webistrano

A simple bridge so you can use Webistrano but still use the cap command.
Ruby
20
star
15

cap-ext-parallelize

A Capistrano extension that allows execution of tasks in parallel.
Ruby
10
star
16

scalarium-riak

Riak Cookbooks For Scalarium, based on Scott Likens' Cookbooks for the EY AppCloud.
Ruby
9
star
17

imacistrano

Oh look! It's Macistrano for the iPhone!
Objective-C
8
star
18

travis-slack

A /slash command to trigger Travis CI builds via Slack
JavaScript
6
star
19

activemessaging

Personal fork of the official activemessaging repository with personal patches.
Ruby
5
star
20

eventmachine-snippets

Scripts to play with ideas and EventMachine
Ruby
5
star
21

scripts-collection

A couple of scripts I use from time to time to ease every kind of self-repeating pain.
Ruby
5
star
22

couch_potato-extensions

Some extensions for CouchPotato, because more awesome is always a good thing.
Ruby
4
star
23

eventmachine-scotrubyconf

EventMachine talk at Scottish Ruby Conference 2011
Ruby
4
star
24

dotfiles

My dots, all in one place.
Python
4
star
25

unfollow-me

Since Qwitter is such a big fail I wanted a simple app that keeps history of people following and unfollowing me. Not that it's really important, I just thought it would be fun to build.
Ruby
4
star
26

riak-rugb

Riak talk for the Ruby User Group Berlin
JavaScript
3
star
27

hurt_logger

Ruby
3
star
28

threadlocal-attr-accessor

Tiny library to add support for thread local instance and class attribute accessors.
Ruby
3
star
29

hallenprojektor

A RubyCocoa client for http://hallenprojekt.de
Ruby
2
star
30

find_with_example

A tiny plugin I wrote out of simple curiosity.
Ruby
2
star
31

mattmatt.github.com

Home
2
star
32

allthefuckingtime.com

Sinatra app serving allthefuckingtime.com
2
star
33

couchdb-jaoo

Presentation on building web applications with just a browser and CouchDB
JavaScript
2
star
34

deception_toolkit

The source code from my diploma thesis. Felt like digging it up. Nothing to be too proud of though.
2
star
35

rugb_mongodb

My talk on MongoDB at the Ruby User Group Berlin
Ruby
2
star
36

mongodb-cloud-expo-prague2010

My talk on MongoDB at Cloud Expo 2010 in Prague
JavaScript
2
star
37

naked

A naked project. Nothing to see here, moving right along.
Ruby
2
star
38

riak-gotocph-2011

JavaScript
1
star
39

sysnockerl

Erlang
1
star
40

techtalksto-databases-2011

JavaScript
1
star
41

nosql-railswaycon-2011

Four Problems, Three Databases. Talk at RailsWayCon 2011
JavaScript
1
star
42

test-cookbooks

Some test cookbooks for Scalarium
Ruby
1
star
43

couchdb-rubyandrails2010

Slides for my CouchDB talk at RubyAndRails 2010
JavaScript
1
star
44

document-databases-mongouk2010

MongoDB's Place in the Document Universe. My talk about document databases at MongoUK.
Ruby
1
star
45

document-databases-databasepro-2010

Slides for my talk at the databasepro PowerDays 2010 on document databases.
JavaScript
1
star
46

railswaycon

A Sinatra app that does absolutely nothing.
Ruby
1
star
47

zshfu.com

It's a zsh party, and everyone's invited!
1
star
48

i18nify

1
star
49

nosql-berlinbuzzwords2010

Slides for my talk on NoSQL at Berlin Buzzwords 2010
Ruby
1
star
50

cassandra-20-slides-rugb

ZOMG, CASSANDRA IN 20 SLIDES!!!!
1
star
51

redis-railswaycon2010

Ruby and Redis - Simple meets Awesome, Slides for my talk at RailsWayCon 2010
JavaScript
1
star
52

isitonec2

Tiny website to check if a particular website is running on EC2
Ruby
1
star
53

riaktant-nodecamp

JavaScript
1
star
54

hubble

Ruby
1
star
55

riak-python-webinar

JavaScript
1
star