rails3-redis-session-store 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +37 -0
  3. data/Rakefile +23 -0
  4. data/lib/redis-session-store.rb +69 -0
  5. metadata +76 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mathias Meyer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ A simple Redis-based session store for Redis. But why, you ask,
2
+ when there's [redis-store](http://github.com/jodosha/redis-store/)?
3
+ redis-store is a one-fits-all solution, and I found it not to work
4
+ properly with Rails, mostly due to a problem that seemed to lie in
5
+ Rack's Abstract::ID class. I wanted something that worked, so I
6
+ blatantly stole the code from Rails' MemCacheStore and turned it
7
+ into a Redis version. No support for fancy stuff like distributed
8
+ storage across several Redis instances. Feel free to add what you
9
+ seem fit.
10
+
11
+ This library doesn't offer anything related to caching, and is
12
+ only suitable for Rails applications. For other frameworks or
13
+ drop-in support for caching, check out
14
+ [redis-store](http://github.com/jodosha/redis-store/)
15
+
16
+ Installation
17
+ ============
18
+
19
+ gem install redis-session-store
20
+
21
+ Configuration
22
+ =============
23
+
24
+ See lib/redis-session-store.rb for a list of valid options.
25
+ Set them using:
26
+
27
+ ActionController::Base.session = {
28
+ :db => 2,
29
+ :expire_after => 120.minutes,
30
+ :key_prefix => "myapp:session:"
31
+ }
32
+
33
+
34
+ In your Rails app, throw in an initializer with the following contents
35
+ and the configuration above:
36
+
37
+ ActionController::Base.session_store = RedisSessionStore
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = 'rails3-redis-session-store'
7
+ s.version = '0.1.7'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.has_rdoc = true
10
+ s.extra_rdoc_files = ["LICENSE"]
11
+ s.summary = "A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and Rails sessions only) in Redis."
12
+ s.description = s.summary
13
+ s.authors = "Mathias Meyer"
14
+ s.email = "meyer@paperplanes.de"
15
+ s.homepage = "http://github.com/mattmatt/redis-session-store"
16
+ s.add_dependency "redis"
17
+ s.require_path = 'lib'
18
+ s.files = %w(README.md Rakefile) + Dir.glob("{lib}/**/*")
19
+ end
20
+
21
+ Rake::GemPackageTask.new(spec) do |pkg|
22
+ pkg.gem_spec = spec
23
+ end
@@ -0,0 +1,69 @@
1
+ require 'redis'
2
+
3
+ module ActionDispatch
4
+ module Session
5
+
6
+ # Redis session storage for Rails, and for Rails only. Derived from
7
+ # the MemCacheStore code, simply dropping in Redis instead.
8
+ #
9
+ # Options:
10
+ # :key => Same as with the other cookie stores, key name
11
+ # :secret => Encryption secret for the key
12
+ # :host => Redis host name, default is localhost
13
+ # :port => Redis port, default is 6379
14
+ # :db => Database number, defaults to 0. Useful to separate your session storage from other data
15
+ # :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others
16
+ # :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis
17
+
18
+ class RedisSessionStore < AbstractStore
19
+
20
+ def initialize(app, options = {})
21
+ # Support old :expires option
22
+ options[:expire_after] ||= options[:expires]
23
+
24
+ super
25
+
26
+ @default_options = {
27
+ :namespace => 'rack:session',
28
+ :host => 'localhost',
29
+ :port => '6379',
30
+ :db => 0,
31
+ :key_prefix => ""
32
+ }.update(options)
33
+
34
+ @pool = Redis.new(@default_options)
35
+ end
36
+
37
+ private
38
+ def prefixed(sid)
39
+ "#{@default_options[:key_prefix]}#{sid}"
40
+ end
41
+
42
+ def get_session(env, sid)
43
+ sid ||= generate_sid
44
+ begin
45
+ data = @pool.call_command([:get, prefixed(sid)])
46
+ session = data.nil? ? {} : Marshal.load(data)
47
+ rescue Errno::ECONNREFUSED
48
+ session = {}
49
+ end
50
+ [sid, session]
51
+ end
52
+
53
+ def set_session(env, sid, session_data)
54
+ options = env['rack.session.options']
55
+ expiry = options[:expire_after] || nil
56
+
57
+ @pool.pipelined do |redis|
58
+ redis.set(prefixed(sid), Marshal.dump(session_data))
59
+ redis.expire(prefixed(sid), expiry) if expiry
60
+ end
61
+
62
+ return true
63
+ rescue Errno::ECONNREFUSED
64
+ return false
65
+ end
66
+
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails3-redis-session-store
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 7
9
+ version: 0.1.7
10
+ platform: ruby
11
+ authors:
12
+ - Mathias Meyer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-18 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: redis
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and Rails sessions only) in Redis.
33
+ email: meyer@paperplanes.de
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ files:
41
+ - README.md
42
+ - Rakefile
43
+ - lib/redis-session-store.rb
44
+ - LICENSE
45
+ has_rdoc: true
46
+ homepage: http://github.com/mattmatt/redis-session-store
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and Rails sessions only) in Redis.
75
+ test_files: []
76
+