redis_session_store 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/redis_session_store.rb +74 -0
- metadata +80 -0
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'redis'
|
2
|
+
# Redis session storage for Rails, and for Rails only. Derived from
|
3
|
+
# the MemCacheStore code, simply dropping in Redis instead.
|
4
|
+
#
|
5
|
+
# Options:
|
6
|
+
# :key => Same as with the other cookie stores, key name
|
7
|
+
# :secret => Encryption secret for the key
|
8
|
+
# :host => Redis host name, default is localhost
|
9
|
+
# :port => Redis port, default is 6379
|
10
|
+
# :db => Database number, defaults to 0. Useful to separate your session storage from other data
|
11
|
+
# :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others
|
12
|
+
# :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis
|
13
|
+
class ActionDispatch::Session::RedisSessionStore < ActionDispatch::Session::AbstractStore
|
14
|
+
def initialize(app, options = {})
|
15
|
+
# Support old :expires option
|
16
|
+
options[:expire_after] ||= options[:expires]
|
17
|
+
|
18
|
+
super
|
19
|
+
|
20
|
+
@default_options = {
|
21
|
+
:namespace => 'rack:session',
|
22
|
+
:host => 'localhost',
|
23
|
+
:port => '6379',
|
24
|
+
:db => 0,
|
25
|
+
:key_prefix => ""
|
26
|
+
}.update(options)
|
27
|
+
|
28
|
+
@pool = Redis.new(@default_options)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def prefixed(sid)
|
33
|
+
"#{@default_options[:key_prefix]}#{sid}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_session(env, sid)
|
37
|
+
sid ||= generate_sid
|
38
|
+
begin
|
39
|
+
data = @pool.get prefixed(sid)
|
40
|
+
session = data.nil? ? {} : Marshal.load(data)
|
41
|
+
# session = data.nil? ? {} : JSON.parse(data)
|
42
|
+
rescue Errno::ECONNREFUSED
|
43
|
+
session = {}
|
44
|
+
end
|
45
|
+
[sid, session]
|
46
|
+
end
|
47
|
+
|
48
|
+
def destroy(env)
|
49
|
+
if sid = current_session_id(env)
|
50
|
+
@pool.del(sid)
|
51
|
+
end
|
52
|
+
rescue Errno::ECONNREFUSED
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_session(env, sid, session_data)
|
57
|
+
options = env['rack.session.options']
|
58
|
+
expiry = options[:expire_after] || nil
|
59
|
+
# @pool.set(sid, session_data, options)
|
60
|
+
|
61
|
+
@pool.pipelined do
|
62
|
+
@pool.set(prefixed(sid), Marshal.dump(session_data))
|
63
|
+
# @pool.set(prefixed(sid), JSON.unparse(session_data))
|
64
|
+
@pool.expire(prefixed(sid), expiry.to_i) if expiry && expiry.to_i > 0
|
65
|
+
end
|
66
|
+
|
67
|
+
return sid
|
68
|
+
rescue Errno::ECONNREFUSED
|
69
|
+
return false
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis_session_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jaehwa Han
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-16 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: redis
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description:
|
36
|
+
email: drh@wafflestudio.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/redis_session_store.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: ""
|
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
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: nowarning
|
75
|
+
rubygems_version: 1.5.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Store rails session using redis.
|
79
|
+
test_files: []
|
80
|
+
|