Skip to content

Commit

Permalink
update for Rails 3.2, allow for already-initialized redis to be used …
Browse files Browse the repository at this point in the history
…as the connection. backwards compatability not considered
  • Loading branch information
mpd committed Feb 7, 2012
1 parent a28bc40 commit b865d8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/redis-session-store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others
# :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis

class RedisSessionStore < ActionController::Session::AbstractStore
class RedisSessionStore < ActionDispatch::Session::AbstractStore

def initialize(app, options = {})
# Support old :expires option
Expand All @@ -28,12 +28,12 @@ def initialize(app, options = {})
:key_prefix => ""
}.update(options)

@redis = Redis.new(@default_options)
@redis = options[:redis] || Redis.new(@default_options)
end

private
def prefixed(sid)
"#{@default_options[:key_prefix]}#{sid}"
"#{@default_options[:key_prefix]}:#{sid}"
end

def get_session(env, sid)
Expand All @@ -47,18 +47,19 @@ def get_session(env, sid)
[sid, session]
end

def set_session(env, sid, session_data)
options = env['rack.session.options']
def set_session(env, sid, session_data, options)
options ||= env['rack.session.options']
expiry = options[:expire_after] || nil

@redis.pipelined do
if expiry
@redis.setex(prefixed(sid), expiry, Marshal.dump(session_data))
else
@redis.set(prefixed(sid), Marshal.dump(session_data))
@redis.expire(prefixed(sid), expiry) if expiry
end

return true
sid
rescue Errno::ECONNREFUSED
return false
false
end

end
18 changes: 18 additions & 0 deletions redis-session-store.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "redis-session-store"
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.authors = ["Mathias Meyer", "Michael Dungan"]
#s.email = [""]
#s.homepage = ""
s.summary = %q{redis-backed sessions for Rails 3+}
s.description = %q{redis-backed sessions for Rails 3+}

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ["lib"]
end

0 comments on commit b865d8d

Please sign in to comment.