Skip to content

Commit

Permalink
Initial commit. And it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
roidrage committed Dec 31, 2009
0 parents commit bcee52c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
pkg
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2009 Mathias Meyer

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Empty file added README
Empty file.
24 changes: 24 additions & 0 deletions Rakefile
@@ -0,0 +1,24 @@
require 'rubygems'
require 'rake/gempackagetask'
require 'rubygems/specification'

spec = Gem::Specification.new do |s|
s.name = 'redis-session-store'
s.version = '0.1'
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["LICENSE"]
s.summary = "A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and Rails sessions only) in Redis."
s.description = s.summary
s.authors = "Mathias Meyer"
s.email = "meyer@paperplanes.de"
s.homepage = ""
s.add_dependency "redis"
s.require_path = 'lib'
s.autorequire = 'redis_session_store'
s.files = %w(README Rakefile) + Dir.glob("{lib}/**/*")
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end
61 changes: 61 additions & 0 deletions lib/redis_session_store.rb
@@ -0,0 +1,61 @@
require 'redis'

# Redis session storage for Rails, and for Rails only. Derived from
# the MemCacheStore code, simply dropping in Redis instead.
#
# Options:
# :key => Same as with the other cookie stores, key name
# :secret => Encryption secret for the key
# :host => Redis host name, default is localhost
# :port => Redis port, default is 6370
# :db => Database number, defaults to 0. Useful to separate your session storage from other data
# :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others.

class RedisSessionStore < ActionController::Session::AbstractStore

def initialize(app, options = {})
# Support old :expires option
options[:expire_after] ||= options[:expires]

super

@default_options = {
:namespace => 'rack:session',
:server => 'localhost',
:port => '6379',
:db => 0,
:key_prefix => ""
}.update(options)

@pool = Redis.new(@default_options)
@mutex = Mutex.new

super
end

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

def get_session(env, sid)
sid ||= generate_sid
begin
data = @pool.call_command([:get, prefixed(sid)])
session = data.nil? ? {} : Marshal.load(data)
rescue Errno::ECONNREFUSED
session = {}
end
[sid, session]
end

def set_session(env, sid, session_data)
options = env['rack.session.options']
expiry = options[:expire_after] || nil
@pool.set(prefixed(sid), Marshal.dump(session_data), expiry)
return true
rescue Errno::ECONNREFUSED
return false
end

end

0 comments on commit bcee52c

Please sign in to comment.