redis_store_jr 0.1.0 → 0.2.0
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.
- data/.document +0 -2
- data/README.rdoc +24 -7
- data/VERSION +1 -1
- data/lib/redis_store.rb +3 -0
- data/lib/redis_store/marshalled_client.rb +1 -1
- data/lib/redis_store/session/redis_session_store.rb +49 -0
- data/spec/cache_spec.rb +1 -1
- data/spec/session_spec.rb +38 -0
- metadata +7 -4
data/.document
CHANGED
data/README.rdoc
CHANGED
@@ -2,19 +2,24 @@
|
|
2
2
|
|
3
3
|
This is a vastly simplified version of RedisStore.
|
4
4
|
|
5
|
-
|
6
|
-
redis-store gem. It does *not* support Sinatra, Merb,
|
7
|
-
options that his Gem does. So if you're
|
8
|
-
want to take a look at the
|
9
|
-
project
|
5
|
+
Much of this code was shamelessly lifted from an earlier version of Luca
|
6
|
+
Guidi's redis-store gem. It does *not* support Sinatra, Merb, distributed
|
7
|
+
storage, or many of the other options that his Gem does. So if you're
|
8
|
+
lookig for that stuff, you probably want to take a look at the original
|
9
|
+
project:
|
10
|
+
|
11
|
+
http://github.com/jodosha/redis-store
|
10
12
|
|
11
13
|
On the other hand, if you're looking for a super-simple Redis store interface
|
12
14
|
that works reliably with Rails and Redis 1.2, without the complicated
|
13
15
|
(and often unnecessary) dependencies, look no further.
|
14
16
|
|
17
|
+
Note that this gem is specifically designed to work with Redis 1.2 and v1.0.7
|
18
|
+
of the Redis gem.
|
19
|
+
|
15
20
|
== Installation
|
16
21
|
|
17
|
-
We assume you already have
|
22
|
+
We assume you already have Redis (http://code.google.com/p/redis/) set up and
|
18
23
|
running. If not, you know what to do.
|
19
24
|
|
20
25
|
In your environment.rb:
|
@@ -25,11 +30,23 @@ Or your Gemfile:
|
|
25
30
|
|
26
31
|
gem 'redis_store_jr', :require => 'redis_store'
|
27
32
|
|
28
|
-
|
33
|
+
To use the Rails.cache store, add the following to an initializer or your
|
34
|
+
environment-specific config:
|
29
35
|
|
30
36
|
require 'redis_store'
|
31
37
|
config.cache_store = :redis_store, 'localhost:6379'
|
32
38
|
|
39
|
+
Want to store user session data in Redis too? Of course you do.
|
40
|
+
|
41
|
+
ActionController::Base.session = {
|
42
|
+
:host => 'dbhost'
|
43
|
+
:port => 6379,
|
44
|
+
:db => 2,
|
45
|
+
:expire_after => 24.hours,
|
46
|
+
:key_prefix => "myapp:session:"
|
47
|
+
}
|
48
|
+
ActionController::Base.session_store = :redis_session_store
|
49
|
+
|
33
50
|
This has only been tested on Rails 2.3.x. No guarantees about Rails 3.
|
34
51
|
|
35
52
|
== Note on Patches/Pull Requests
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/redis_store.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
gem 'redis', '~> 1.0'
|
2
2
|
gem 'activesupport', '~> 2.3'
|
3
|
+
gem 'actionpack', '~> 2.3'
|
3
4
|
|
4
5
|
require 'redis'
|
5
6
|
require 'active_support'
|
7
|
+
require 'action_controller'
|
6
8
|
|
7
9
|
require 'redis_store/marshalled_client'
|
8
10
|
require 'redis_store/cache/redis_store'
|
11
|
+
require 'redis_store/session/redis_session_store'
|
9
12
|
|
10
13
|
class RedisStore
|
11
14
|
class Config < Hash
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Session
|
5
|
+
class RedisSessionStore < AbstractStore
|
6
|
+
def initialize(app, options = {})
|
7
|
+
options[:expire_after] ||= options[:expires]
|
8
|
+
|
9
|
+
super
|
10
|
+
|
11
|
+
@options = {
|
12
|
+
:namespace => 'rack:session',
|
13
|
+
:key_prefix => ''
|
14
|
+
}.update(options)
|
15
|
+
|
16
|
+
@data = options[:cache] || ::RedisStore::MarshalledClient.new(::RedisStore::Config.new(options))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def prefixed(sid)
|
22
|
+
"#{@options[:key_prefix]}#{sid}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_session(env, sid)
|
26
|
+
sid ||= generate_sid
|
27
|
+
begin
|
28
|
+
data = @data.marshalled_get(prefixed(sid))
|
29
|
+
session = data.nil? ? {} : data
|
30
|
+
rescue Errno::ECONNREFUSED
|
31
|
+
session = {}
|
32
|
+
end
|
33
|
+
[sid, session]
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_session(env, sid, session_data)
|
37
|
+
options = env['rack.session.options']
|
38
|
+
expiry = options[:expires_in] || options[:expire_after] || nil
|
39
|
+
|
40
|
+
write_options = expiry.nil? ? {} : { :expires_in => expiry }
|
41
|
+
@data.marshalled_set(prefixed(sid), session_data, write_options)
|
42
|
+
|
43
|
+
return true
|
44
|
+
rescue Errno::ECONNREFUSED
|
45
|
+
return false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/cache_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe ActiveSupport::Cache::RedisStore do
|
|
18
18
|
|
19
19
|
it 'should read and write data with an expiration' do
|
20
20
|
with_store do |store|
|
21
|
-
store.write('abc', @obj, :
|
21
|
+
store.write('abc', @obj, :expires_in => 1.second)
|
22
22
|
store.read('abc').should == @obj ; sleep 2
|
23
23
|
store.read('abc').should be_nil
|
24
24
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe ActionController::Session::RedisSessionStore do
|
4
|
+
before(:each) do
|
5
|
+
@app = Object.new
|
6
|
+
@store = ActionController::Session::RedisSessionStore.new(@app)
|
7
|
+
@obj = OpenStruct.new(:name => 'Test')
|
8
|
+
|
9
|
+
with_store do |store|
|
10
|
+
class << store
|
11
|
+
public :get_session, :set_session
|
12
|
+
end
|
13
|
+
|
14
|
+
store.set_session({'rack.session.options' => {}}, 'abc', @obj)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should save and retrieve session information' do
|
19
|
+
with_store do |store|
|
20
|
+
store.set_session({ 'rack.session.options' => {} }, 'abc', @obj)
|
21
|
+
store.get_session({}, 'abc').should === ['abc', @obj]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should save session data with an expiration time' do
|
26
|
+
with_store do |store|
|
27
|
+
store.set_session({ 'rack.session.options' => { :expires_in => 1.second } }, 'abc', @obj)
|
28
|
+
store.get_session({}, 'abc').should === ['abc', @obj]; sleep 2
|
29
|
+
store.get_session({}, 'abc').should === ['abc', {}]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def with_store
|
36
|
+
yield @store
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_store_jr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nick Plante
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-05 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -97,8 +97,10 @@ files:
|
|
97
97
|
- lib/redis_store.rb
|
98
98
|
- lib/redis_store/cache/redis_store.rb
|
99
99
|
- lib/redis_store/marshalled_client.rb
|
100
|
+
- lib/redis_store/session/redis_session_store.rb
|
100
101
|
- spec/cache_spec.rb
|
101
102
|
- spec/marshalled_client_spec.rb
|
103
|
+
- spec/session_spec.rb
|
102
104
|
- spec/spec.opts
|
103
105
|
- spec/spec_helper.rb
|
104
106
|
has_rdoc: true
|
@@ -138,4 +140,5 @@ summary: Simple Redis Cache Store for Rails
|
|
138
140
|
test_files:
|
139
141
|
- spec/cache_spec.rb
|
140
142
|
- spec/marshalled_client_spec.rb
|
143
|
+
- spec/session_spec.rb
|
141
144
|
- spec/spec_helper.rb
|