goncalossilva_redis-session-store 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +46 -0
  3. data/Rakefile +2 -0
  4. data/lib/redis-session-store.rb +61 -0
  5. metadata +63 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Mathias Meyer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
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
+ :key => 'your_session_key',
29
+ :secret => 'your_long_secret',
30
+ :redis => {
31
+ :db => 2,
32
+ :expire_after => 120.minutes,
33
+ :key_prefix => "myapp:session:"
34
+ }
35
+ }
36
+
37
+
38
+ In your Rails app, throw in an initializer with the following contents
39
+ and the configuration above:
40
+
41
+ ActionController::Base.session_store = RedisSessionStore
42
+
43
+ Changes
44
+ =======
45
+
46
+ * Use setex for a single command instead of sending two commands. (Thanks dplummer)
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,61 @@
1
+ require 'redis'
2
+
3
+ # Redis session storage for Rails, and for Rails only. Derived from
4
+ # the MemCacheStore code, simply dropping in Redis instead.
5
+ #
6
+ # Options:
7
+ # :key => Same as with the other cookie stores, key name
8
+ # :secret => Encryption secret for the key
9
+ # :host => Redis host name, default is localhost
10
+ # :port => Redis port, default is 6379
11
+ # :db => Database number, defaults to 0. Useful to separate your session storage from other data
12
+ # :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others
13
+ # :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis
14
+
15
+ class RedisSessionStore < ActionController::Session::AbstractStore
16
+
17
+ def initialize(app, options = {})
18
+ super
19
+
20
+ redis_options = {}
21
+ @default_options.merge!(:namespace => 'rack:session')
22
+ @default_options.merge!(options[:redis]) if options[:redis]
23
+
24
+ @redis = Redis.new(redis_options)
25
+ end
26
+
27
+ private
28
+ def prefixed(sid)
29
+ "#{@default_options[:key_prefix]}#{sid}"
30
+ end
31
+
32
+ def get_session(env, sid)
33
+ sid ||= generate_sid
34
+ begin
35
+ data = @redis.get(prefixed(sid))
36
+ session = data.nil? ? {} : Marshal.load(ActiveSupport::Base64.decode64(data))
37
+ rescue Errno::ECONNREFUSED
38
+ session = {}
39
+ end
40
+ [sid, session]
41
+ end
42
+
43
+ def set_session(env, sid, session_data)
44
+ options = env['rack.session.options']
45
+ expiry = options[:expire_after] || nil
46
+ if expiry
47
+ @redis.setex(prefixed(sid), expiry, ActiveSupport::Base64.encode64(Marshal.dump(session_data))) if session_data
48
+ else
49
+ @redis.set(prefixed(sid), ActiveSupport::Base64.encode64(Marshal.dump(session_data))) if session_data
50
+ end
51
+ return true
52
+ rescue Errno::ECONNREFUSED
53
+ return false
54
+ end
55
+
56
+ def destroy(env)
57
+ @redis.del prefixed(env['rack.request.cookie_hash'][@key])
58
+ rescue
59
+ Rails.logger.warn("RedisSessionStore#destroy: Connection to redis refused")
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goncalossilva_redis-session-store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mathias Meyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &70119372463320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70119372463320
25
+ description: A drop-in replacement for e.g. MemCacheStore to store Rails sessions
26
+ (and Rails sessions only) in Redis.
27
+ email:
28
+ - meyer@paperplanes.de
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/redis-session-store.rb
37
+ - LICENSE
38
+ homepage: http://github.com/goncalossilva/redis-session-store
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.17
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and
62
+ Rails sessions only) in Redis.
63
+ test_files: []