redis-session-store 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS.md +12 -0
- data/CONTRIBUTING.md +6 -0
- data/Gemfile +7 -0
- data/LICENSE +1 -1
- data/README.md +32 -18
- data/Rakefile +10 -1
- data/lib/redis-session-store.rb +19 -10
- data/test/fake_action_controller_session_abstract_store.rb +22 -0
- data/test/redis_session_store_test.rb +103 -0
- metadata +25 -8
data/AUTHORS.md
ADDED
data/CONTRIBUTING.md
ADDED
data/Gemfile
ADDED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
|
1
|
+
Redis Session Store
|
2
|
+
===================
|
3
|
+
|
4
|
+
A simple Redis-based session store for Rails. But why, you ask,
|
2
5
|
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
|
6
|
+
redis-store is a one-size-fits-all solution, and I found it not to work
|
4
7
|
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
|
8
|
+
Rack's `Abstract::ID` class. I wanted something that worked, so I
|
9
|
+
blatantly stole the code from Rails' `MemCacheStore` and turned it
|
7
10
|
into a Redis version. No support for fancy stuff like distributed
|
8
11
|
storage across several Redis instances. Feel free to add what you
|
9
|
-
|
12
|
+
see fit.
|
10
13
|
|
11
14
|
This library doesn't offer anything related to caching, and is
|
12
15
|
only suitable for Rails applications. For other frameworks or
|
@@ -14,29 +17,40 @@ drop-in support for caching, check out
|
|
14
17
|
[redis-store](http://github.com/jodosha/redis-store/)
|
15
18
|
|
16
19
|
Installation
|
17
|
-
|
20
|
+
------------
|
18
21
|
|
19
|
-
|
22
|
+
``` bash
|
23
|
+
gem install redis-session-store
|
24
|
+
```
|
20
25
|
|
21
26
|
Configuration
|
22
|
-
|
27
|
+
-------------
|
23
28
|
|
24
|
-
See lib/redis-session-store.rb for a list of valid options.
|
29
|
+
See `lib/redis-session-store.rb` for a list of valid options.
|
25
30
|
Set them using:
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
``` ruby
|
33
|
+
ActionController::Base.session = {
|
34
|
+
:key => 'your_session_key',
|
35
|
+
:secret => 'your_long_secret',
|
36
|
+
:redis => {
|
37
|
+
:db => 2,
|
38
|
+
:expire_after => 120.minutes,
|
39
|
+
:key_prefix => "myapp:session:"
|
40
|
+
}
|
41
|
+
}
|
42
|
+
```
|
32
43
|
|
33
44
|
|
34
45
|
In your Rails app, throw in an initializer with the following contents
|
35
46
|
and the configuration above:
|
36
47
|
|
37
|
-
|
48
|
+
``` ruby
|
49
|
+
ActionController::Base.session_store = RedisSessionStore
|
50
|
+
```
|
38
51
|
|
39
|
-
|
40
|
-
|
52
|
+
Contributing, Authors, & License
|
53
|
+
--------------------------------
|
41
54
|
|
42
|
-
|
55
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md), [AUTHORS.md](AUTHORS.md), and
|
56
|
+
[LICENSE](LICENSE), respectively.
|
data/Rakefile
CHANGED
data/lib/redis-session-store.rb
CHANGED
@@ -6,22 +6,23 @@ require 'redis'
|
|
6
6
|
# Options:
|
7
7
|
# :key => Same as with the other cookie stores, key name
|
8
8
|
# :secret => Encryption secret for the key
|
9
|
-
# :
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
|
9
|
+
# :redis => {
|
10
|
+
# :host => Redis host name, default is localhost
|
11
|
+
# :port => Redis port, default is 6379
|
12
|
+
# :db => Database number, defaults to 0. Useful to separate your session storage from other data
|
13
|
+
# :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others
|
14
|
+
# :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis
|
15
|
+
# }
|
15
16
|
class RedisSessionStore < ActionController::Session::AbstractStore
|
16
17
|
|
17
18
|
def initialize(app, options = {})
|
18
19
|
super
|
19
20
|
|
20
|
-
|
21
|
-
:namespace => 'rack:session'
|
22
|
-
}.merge(options)
|
21
|
+
redis_options = options[:redis] || {}
|
23
22
|
|
24
|
-
@
|
23
|
+
@default_options.merge!(:namespace => 'rack:session')
|
24
|
+
@default_options.merge!(redis_options)
|
25
|
+
@redis = Redis.new(redis_options)
|
25
26
|
end
|
26
27
|
|
27
28
|
private
|
@@ -52,4 +53,12 @@ class RedisSessionStore < ActionController::Session::AbstractStore
|
|
52
53
|
rescue Errno::ECONNREFUSED
|
53
54
|
return false
|
54
55
|
end
|
56
|
+
|
57
|
+
def destroy(env)
|
58
|
+
if env['rack.request.cookie_hash'] && env['rack.request.cookie_hash'][@key]
|
59
|
+
@redis.del( prefixed(env['rack.request.cookie_hash'][@key]) )
|
60
|
+
end
|
61
|
+
rescue Errno::ECONNREFUSED
|
62
|
+
Rails.logger.warn("RedisSessionStore#destroy: Connection to redis refused")
|
63
|
+
end
|
55
64
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
unless defined?(ActionController::Session::AbstractStore)
|
2
|
+
module ActionController
|
3
|
+
module Session
|
4
|
+
class AbstractStore
|
5
|
+
def initialize(app, options = {})
|
6
|
+
@app = app
|
7
|
+
@default_options = {
|
8
|
+
:key => '_session_id',
|
9
|
+
:path => '/',
|
10
|
+
:domain => nil,
|
11
|
+
:expire_after => nil,
|
12
|
+
:secure => false,
|
13
|
+
:httponly => true,
|
14
|
+
:cookie_only => true
|
15
|
+
}.merge(options)
|
16
|
+
@key = @default_options[:key]
|
17
|
+
@cookie_only = @default_options[:cookie_only]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require File.expand_path('../fake_action_controller_session_abstract_store', __FILE__)
|
3
|
+
require 'redis-session-store'
|
4
|
+
|
5
|
+
describe RedisSessionStore do
|
6
|
+
def random_string
|
7
|
+
"#{rand}#{rand}#{rand}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def options
|
11
|
+
{}
|
12
|
+
end
|
13
|
+
|
14
|
+
def store
|
15
|
+
RedisSessionStore.new(nil, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def default_options
|
19
|
+
store.instance_variable_get(:@default_options)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'assigns a :namespace to @default_options' do
|
23
|
+
default_options[:namespace].must_equal 'rack:session'
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'when initializing with the redis sub-hash options' do
|
27
|
+
def options
|
28
|
+
{
|
29
|
+
:key => random_string,
|
30
|
+
:secret => random_string,
|
31
|
+
:redis => {
|
32
|
+
:host => 'hosty.local',
|
33
|
+
:port => 16379,
|
34
|
+
:db => 2,
|
35
|
+
:key_prefix => 'myapp:session:',
|
36
|
+
:expire_after => 60 * 120
|
37
|
+
}
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'creates a redis instance' do
|
42
|
+
store.instance_variable_get(:@redis).wont_equal nil
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'assigns the :host option to @default_options' do
|
46
|
+
default_options[:host].must_equal 'hosty.local'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'assigns the :port option to @default_options' do
|
50
|
+
default_options[:port].must_equal 16379
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'assigns the :db option to @default_options' do
|
54
|
+
default_options[:db].must_equal 2
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'assigns the :key_prefix option to @default_options' do
|
58
|
+
default_options[:key_prefix].must_equal 'myapp:session:'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'assigns the :expire_after option to @default_options' do
|
62
|
+
default_options[:expire_after].must_equal 60 * 120
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'when initializing with top-level redis options' do
|
67
|
+
def options
|
68
|
+
{
|
69
|
+
:key => random_string,
|
70
|
+
:secret => random_string,
|
71
|
+
:host => 'hostersons.local',
|
72
|
+
:port => 26379,
|
73
|
+
:db => 4,
|
74
|
+
:key_prefix => 'appydoo:session:',
|
75
|
+
:expire_after => 60 * 60
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'creates a redis instance' do
|
80
|
+
store.instance_variable_get(:@redis).wont_equal nil
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'assigns the :host option to @default_options' do
|
84
|
+
default_options[:host].must_equal 'hostersons.local'
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'assigns the :port option to @default_options' do
|
88
|
+
default_options[:port].must_equal 26379
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'assigns the :db option to @default_options' do
|
92
|
+
default_options[:db].must_equal 4
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'assigns the :key_prefix option to @default_options' do
|
96
|
+
default_options[:key_prefix].must_equal 'appydoo:session:'
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'assigns the :expire_after option to @default_options' do
|
100
|
+
default_options[:expire_after].must_equal 60 * 60
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-session-store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: A drop-in replacement for e.g. MemCacheStore to store Rails sessions
|
26
31
|
(and Rails sessions only) in Redis.
|
27
32
|
email:
|
@@ -30,12 +35,19 @@ executables: []
|
|
30
35
|
extensions: []
|
31
36
|
extra_rdoc_files:
|
32
37
|
- LICENSE
|
38
|
+
- AUTHORS.md
|
39
|
+
- CONTRIBUTING.md
|
33
40
|
files:
|
41
|
+
- AUTHORS.md
|
42
|
+
- CONTRIBUTING.md
|
43
|
+
- Gemfile
|
44
|
+
- LICENSE
|
34
45
|
- README.md
|
35
46
|
- Rakefile
|
36
47
|
- lib/redis-session-store.rb
|
37
|
-
-
|
38
|
-
|
48
|
+
- test/fake_action_controller_session_abstract_store.rb
|
49
|
+
- test/redis_session_store_test.rb
|
50
|
+
homepage: https://github.com/roidrage/redis-session-store
|
39
51
|
licenses: []
|
40
52
|
post_install_message:
|
41
53
|
rdoc_options: []
|
@@ -47,18 +59,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
59
|
- - ! '>='
|
48
60
|
- !ruby/object:Gem::Version
|
49
61
|
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: -3400927729934589322
|
50
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
66
|
none: false
|
52
67
|
requirements:
|
53
68
|
- - ! '>='
|
54
69
|
- !ruby/object:Gem::Version
|
55
70
|
version: '0'
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
hash: -3400927729934589322
|
56
74
|
requirements: []
|
57
75
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.8.
|
76
|
+
rubygems_version: 1.8.23
|
59
77
|
signing_key:
|
60
78
|
specification_version: 3
|
61
79
|
summary: A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and
|
62
80
|
Rails sessions only) in Redis.
|
63
81
|
test_files: []
|
64
|
-
has_rdoc: true
|