redis-store 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of redis-store might be problematic. Click here for more details.

Files changed (39) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +13 -7
  3. data/README.md +11 -1
  4. data/Rakefile +19 -5
  5. data/VERSION +1 -1
  6. data/lib/cache/merb/redis_store.rb +6 -6
  7. data/lib/cache/rails/redis_store.rb +10 -10
  8. data/lib/cache/sinatra/redis_store.rb +14 -11
  9. data/lib/rack/cache/redis_entitystore.rb +2 -2
  10. data/lib/rack/cache/redis_metastore.rb +5 -5
  11. data/lib/rack/session/rails.rb +61 -0
  12. data/lib/rack/session/redis.rb +10 -10
  13. data/lib/redis-store.rb +8 -4
  14. data/lib/redis/distributed_marshaled.rb +18 -0
  15. data/lib/redis/factory.rb +26 -0
  16. data/lib/redis/marshaled_client.rb +54 -0
  17. data/lib/redis/namespace.rb +9 -0
  18. data/redis-store.gemspec +18 -13
  19. data/spec/cache/merb/redis_store_spec.rb +9 -14
  20. data/spec/cache/rails/redis_session_store_spec.rb +77 -0
  21. data/spec/cache/rails/redis_store_spec.rb +13 -17
  22. data/spec/cache/sinatra/redis_store_spec.rb +9 -14
  23. data/spec/config/master.conf +5 -5
  24. data/spec/config/single.conf +5 -5
  25. data/spec/config/slave.conf +6 -6
  26. data/spec/rack/cache/entitystore/redis_spec.rb +21 -11
  27. data/spec/rack/cache/metastore/redis_spec.rb +169 -16
  28. data/spec/rack/session/redis_spec.rb +7 -11
  29. data/spec/redis/distributed_marshaled_redis_spec.rb +9 -11
  30. data/spec/redis/factory_spec.rb +68 -0
  31. data/spec/redis/marshaled_client_spec.rb +54 -0
  32. data/spec/spec_helper.rb +11 -5
  33. data/tasks/redis.tasks.rb +16 -5
  34. metadata +28 -12
  35. data/lib/redis/distributed_marshaled_redis.rb +0 -10
  36. data/lib/redis/marshaled_redis.rb +0 -33
  37. data/lib/redis/redis_factory.rb +0 -27
  38. data/spec/redis/marshaled_redis_spec.rb +0 -54
  39. data/spec/redis/redis_factory_spec.rb +0 -34
data/lib/redis-store.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require "redis"
2
- require "dist_redis"
3
- require "redis/redis_factory"
4
- require "redis/marshaled_redis"
5
- require "redis/distributed_marshaled_redis"
2
+ require "redis/namespace"
3
+ require "redis/factory"
4
+ require "redis/marshaled_client"
5
+ require "redis/distributed_marshaled"
6
6
 
7
7
  # Cache store
8
8
  if defined?(Sinatra)
@@ -22,10 +22,14 @@ if defined?(Rack::Session)
22
22
  if defined?(Merb)
23
23
  require "rack/session/merb"
24
24
  end
25
+ if defined?(Rails)
26
+ require "rack/session/rails"
27
+ end
25
28
  end
26
29
 
27
30
  # Rack::Cache
28
31
  if defined?(Rack::Cache)
32
+ require "rack/cache/key"
29
33
  require "rack/cache/redis_metastore"
30
34
  require "rack/cache/redis_entitystore"
31
35
  end
@@ -0,0 +1,18 @@
1
+ class Redis
2
+ class DistributedMarshaled < Distributed
3
+ attr_reader :ring
4
+
5
+ def initialize(addresses)
6
+ nodes = addresses.map do |address|
7
+ MarshaledClient.new address
8
+ end
9
+ @ring = Redis::HashRing.new nodes
10
+ end
11
+
12
+ def nodes
13
+ ring.nodes
14
+ end
15
+
16
+ alias_method :flushdb, :delete_cloud!
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ class Redis
2
+ class Factory
3
+ def self.create(*redis_client_options)
4
+ redis_client_options = redis_client_options.flatten.compact.inject([]) do |result, address|
5
+ result << convert_to_redis_client_options(address)
6
+ result
7
+ end
8
+ if redis_client_options.size > 1
9
+ DistributedMarshaled.new redis_client_options
10
+ else
11
+ MarshaledClient.new redis_client_options.first || {}
12
+ end
13
+ end
14
+
15
+ def self.convert_to_redis_client_options(address_or_options)
16
+ return address_or_options if address_or_options.is_a?(Hash)
17
+ host, port = address_or_options.split /\:/
18
+ port, db = port.split /\// if port
19
+ options = {}
20
+ options[:host] = host if host
21
+ options[:port] = port if port
22
+ options[:db] = db.to_i if db
23
+ options
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ class Redis
2
+ class MarshaledClient < Client
3
+ def marshalled_set(key, val, options = nil)
4
+ val = marshal_value(val, options)
5
+ if expires_in = expires_in(options)
6
+ set_with_expire key, val, expires_in
7
+ else
8
+ set key, val
9
+ end
10
+ end
11
+
12
+ def marshalled_setnx(key, val, options = nil)
13
+ val = marshal_value(val, options)
14
+ if expires_in = expires_in(options)
15
+ setnx_with_expire key, val, expires_in
16
+ else
17
+ setnx key, val
18
+ end
19
+ end
20
+
21
+ def setnx_with_expire(key, value, ttl)
22
+ multi do
23
+ setnx(key, val)
24
+ expire(key, expires_in)
25
+ end
26
+ end
27
+
28
+ def marshalled_get(key, options = nil)
29
+ result = call_command([:get, key])
30
+ result = Marshal.load result if unmarshal?(result, options)
31
+ result
32
+ end
33
+
34
+ private
35
+ def marshal_value(val, options)
36
+ raw?(options) ? val : Marshal.dump(val)
37
+ end
38
+
39
+ def unmarshal?(result, options)
40
+ result && result.size > 0 && !raw?(options)
41
+ end
42
+
43
+ def raw?(options)
44
+ options && options[:raw]
45
+ end
46
+
47
+ def expires_in(options)
48
+ if options
49
+ # Rack::Session Merb Rails/Sinatra
50
+ options[:expire_after] || options[:expires_in] || options[:expire_in]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ begin
2
+ require "redis/dist_redis"
3
+ rescue LoadError
4
+ require "redis/distributed"
5
+ end
6
+
7
+ unless defined?(Redis::Distributed)
8
+ class Redis::Distributed < Redis::DistRedis; end
9
+ end
data/redis-store.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{redis-store}
8
- s.version = "0.3.7"
8
+ s.version = "0.3.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Luca Guidi"]
12
- s.date = %q{2009-11-15}
12
+ s.date = %q{2010-05-21}
13
13
  s.description = %q{Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks.}
14
14
  s.email = %q{guidi.luca@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,13 +28,16 @@ Gem::Specification.new do |s|
28
28
  "lib/rack/cache/redis_entitystore.rb",
29
29
  "lib/rack/cache/redis_metastore.rb",
30
30
  "lib/rack/session/merb.rb",
31
+ "lib/rack/session/rails.rb",
31
32
  "lib/rack/session/redis.rb",
32
33
  "lib/redis-store.rb",
33
- "lib/redis/distributed_marshaled_redis.rb",
34
- "lib/redis/marshaled_redis.rb",
35
- "lib/redis/redis_factory.rb",
34
+ "lib/redis/distributed_marshaled.rb",
35
+ "lib/redis/factory.rb",
36
+ "lib/redis/marshaled_client.rb",
37
+ "lib/redis/namespace.rb",
36
38
  "redis-store.gemspec",
37
39
  "spec/cache/merb/redis_store_spec.rb",
40
+ "spec/cache/rails/redis_session_store_spec.rb",
38
41
  "spec/cache/rails/redis_store_spec.rb",
39
42
  "spec/cache/sinatra/redis_store_spec.rb",
40
43
  "spec/config/master.conf",
@@ -45,26 +48,27 @@ Gem::Specification.new do |s|
45
48
  "spec/rack/cache/metastore/redis_spec.rb",
46
49
  "spec/rack/session/redis_spec.rb",
47
50
  "spec/redis/distributed_marshaled_redis_spec.rb",
48
- "spec/redis/marshaled_redis_spec.rb",
49
- "spec/redis/redis_factory_spec.rb",
51
+ "spec/redis/factory_spec.rb",
52
+ "spec/redis/marshaled_client_spec.rb",
50
53
  "spec/spec_helper.rb",
51
54
  "tasks/redis.tasks.rb"
52
55
  ]
53
56
  s.homepage = %q{http://github.com/jodosha/redis-store}
54
57
  s.rdoc_options = ["--charset=UTF-8"]
55
58
  s.require_paths = ["lib"]
56
- s.rubygems_version = %q{1.3.5}
59
+ s.rubygems_version = %q{1.3.7}
57
60
  s.summary = %q{Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks.}
58
61
  s.test_files = [
59
62
  "spec/cache/merb/redis_store_spec.rb",
63
+ "spec/cache/rails/redis_session_store_spec.rb",
60
64
  "spec/cache/rails/redis_store_spec.rb",
61
65
  "spec/cache/sinatra/redis_store_spec.rb",
62
66
  "spec/rack/cache/entitystore/redis_spec.rb",
63
67
  "spec/rack/cache/metastore/redis_spec.rb",
64
68
  "spec/rack/session/redis_spec.rb",
65
69
  "spec/redis/distributed_marshaled_redis_spec.rb",
66
- "spec/redis/marshaled_redis_spec.rb",
67
- "spec/redis/redis_factory_spec.rb",
70
+ "spec/redis/factory_spec.rb",
71
+ "spec/redis/marshaled_client_spec.rb",
68
72
  "spec/spec_helper.rb"
69
73
  ]
70
74
 
@@ -72,9 +76,10 @@ Gem::Specification.new do |s|
72
76
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
73
77
  s.specification_version = 3
74
78
 
75
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
79
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
76
80
  else
77
81
  end
78
82
  else
79
83
  end
80
84
  end
85
+
@@ -9,35 +9,30 @@ module Merb
9
9
  @rabbit = OpenStruct.new :name => "bunny"
10
10
  @white_rabbit = OpenStruct.new :color => "white"
11
11
  with_store_management do |store|
12
- store.write "rabbit", @rabbit
12
+ store.write "rabbit", @rabbit
13
13
  store.delete "rub-a-dub"
14
14
  end
15
15
  end
16
16
 
17
17
  it "should accept connection params" do
18
18
  redis = instantiate_store
19
- redis.host.should == "127.0.0.1"
20
- redis.port.should == 6379
21
- redis.db.should == 0
19
+ redis.to_s.should == "Redis Client connected to 127.0.0.1:6379 against DB 0"
22
20
 
23
21
  redis = instantiate_store "localhost"
24
- redis.host.should == "localhost"
22
+ redis.to_s.should == "Redis Client connected to localhost:6379 against DB 0"
25
23
 
26
24
  redis = instantiate_store "localhost:6380"
27
- redis.host.should == "localhost"
28
- redis.port.should == 6380
25
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 0"
29
26
 
30
27
  redis = instantiate_store "localhost:6380/13"
31
- redis.host.should == "localhost"
32
- redis.port.should == 6380
33
- redis.db.should == 13
28
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 13"
34
29
  end
35
-
30
+
36
31
  it "should instantiate a ring" do
37
32
  store = instantiate_store
38
- store.should be_kind_of(MarshaledRedis)
33
+ store.should be_kind_of(Redis::MarshaledClient)
39
34
  store = instantiate_store ["localhost:6379/0", "localhost:6379/1"]
40
- store.should be_kind_of(DistributedMarshaledRedis)
35
+ store.should be_kind_of(Redis::DistributedMarshaled)
41
36
  end
42
37
 
43
38
  it "should verify if writable" do
@@ -54,7 +49,7 @@ module Merb
54
49
 
55
50
  it "should read raw data" do
56
51
  with_store_management do |store|
57
- store.read("rabbit", {}, :raw => true).should == "\004\bU:\017OpenStruct{\006:\tname\"\nbunny"
52
+ store.read("rabbit", {}, :raw => true).should == Marshal.dump(@rabbit)
58
53
  end
59
54
  end
60
55
 
@@ -0,0 +1,77 @@
1
+ require File.join(File.dirname(__FILE__), "/../../spec_helper")
2
+
3
+ module ActionController
4
+ module Session
5
+ describe "ActionController::Session::RedisSessionStore" do
6
+ attr_reader :app
7
+ before(:each) do
8
+ @app = Object.new
9
+ @store = ActionController::Session::RedisSessionStore.new(app)
10
+ @dstore = ActionController::Session::RedisSessionStore.new app, :servers => ["localhost:6380/1", "localhost:6381/1"]
11
+ @rabbit = OpenStruct.new :name => "bunny"
12
+ @white_rabbit = OpenStruct.new :color => "white"
13
+ with_store_management do |store|
14
+ class << store
15
+ attr_reader :pool
16
+ public :get_session, :set_session
17
+ end
18
+ store.set_session({'rack.session.options' => {}}, "rabbit", @rabbit)
19
+ store.pool.del "counter"
20
+ store.pool.del "rub-a-dub"
21
+ end
22
+ end
23
+
24
+ it "should accept connection params" do
25
+ redis = instantiate_store
26
+ redis.to_s.should == "Redis Client connected to 127.0.0.1:6379 against DB 0"
27
+
28
+ redis = instantiate_store :servers => "localhost"
29
+ redis.to_s.should == "Redis Client connected to localhost:6379 against DB 0"
30
+
31
+ redis = instantiate_store :servers => "localhost:6380"
32
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 0"
33
+
34
+ redis = instantiate_store :servers => "localhost:6380/13"
35
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 13"
36
+ end
37
+
38
+ it "should instantiate a ring" do
39
+ store = instantiate_store
40
+ store.should be_kind_of(Redis::MarshaledClient)
41
+ store = instantiate_store :servers => ["localhost:6379/0", "localhost:6379/1"]
42
+ store.should be_kind_of(Redis::DistributedMarshaled)
43
+ end
44
+
45
+ it "should read the data" do
46
+ with_store_management do |store|
47
+ store.get_session({}, "rabbit").should === ["rabbit", @rabbit]
48
+ end
49
+ end
50
+
51
+ it "should write the data" do
52
+ with_store_management do |store|
53
+ store.set_session({"rack.session.options" => {}}, "rabbit", @white_rabbit)
54
+ store.get_session({}, "rabbit").should === ["rabbit", @white_rabbit]
55
+ end
56
+ end
57
+
58
+ it "should write the data with expiration time" do
59
+ with_store_management do |store|
60
+ store.set_session({"rack.session.options" => {:expires_in => 1.second}}, "rabbit", @white_rabbit)
61
+ store.get_session({}, "rabbit").should === ["rabbit", @white_rabbit]; sleep 2
62
+ store.get_session({}, "rabbit").should === ["rabbit", {}]
63
+ end
64
+ end
65
+
66
+ private
67
+ def instantiate_store(params={})
68
+ ActionController::Session::RedisSessionStore.new(app, params).instance_variable_get(:@pool)
69
+ end
70
+
71
+ def with_store_management
72
+ yield @store
73
+ yield @dstore
74
+ end
75
+ end
76
+ end
77
+ end
@@ -17,28 +17,23 @@ module ActiveSupport
17
17
 
18
18
  it "should accept connection params" do
19
19
  redis = instantiate_store
20
- redis.host.should == "127.0.0.1"
21
- redis.port.should == 6379
22
- redis.db.should == 0
20
+ redis.to_s.should == "Redis Client connected to 127.0.0.1:6379 against DB 0"
23
21
 
24
22
  redis = instantiate_store "localhost"
25
- redis.host.should == "localhost"
26
-
23
+ redis.to_s.should == "Redis Client connected to localhost:6379 against DB 0"
24
+
27
25
  redis = instantiate_store "localhost:6380"
28
- redis.host.should == "localhost"
29
- redis.port.should == 6380
26
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 0"
30
27
 
31
28
  redis = instantiate_store "localhost:6380/13"
32
- redis.host.should == "localhost"
33
- redis.port.should == 6380
34
- redis.db.should == 13
29
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 13"
35
30
  end
36
31
 
37
32
  it "should instantiate a ring" do
38
33
  store = instantiate_store
39
- store.should be_kind_of(MarshaledRedis)
34
+ store.should be_kind_of(Redis::MarshaledClient)
40
35
  store = instantiate_store ["localhost:6379/0", "localhost:6379/1"]
41
- store.should be_kind_of(DistributedMarshaledRedis)
36
+ store.should be_kind_of(Redis::DistributedMarshaled)
42
37
  end
43
38
 
44
39
  it "should read the data" do
@@ -57,7 +52,7 @@ module ActiveSupport
57
52
  it "should write the data with expiration time" do
58
53
  with_store_management do |store|
59
54
  store.write "rabbit", @white_rabbit, :expires_in => 1.second
60
- store.read("rabbit").should === @white_rabbit ; sleep 2
55
+ store.read("rabbit").should == @white_rabbit ; sleep 2
61
56
  store.read("rabbit").should be_nil
62
57
  end
63
58
  end
@@ -65,13 +60,13 @@ module ActiveSupport
65
60
  it "should not write data if :unless_exist option is true" do
66
61
  with_store_management do |store|
67
62
  store.write "rabbit", @white_rabbit, :unless_exist => true
68
- store.read("rabbit").should === @rabbit
63
+ store.read("rabbit").should == @rabbit
69
64
  end
70
65
  end
71
66
 
72
67
  it "should read raw data" do
73
68
  with_store_management do |store|
74
- store.read("rabbit", :raw => true).should == "\004\bU:\017OpenStruct{\006:\tname\"\nbunny"
69
+ store.read("rabbit", :raw => true).should == Marshal.dump(@rabbit)
75
70
  end
76
71
  end
77
72
 
@@ -154,11 +149,12 @@ module ActiveSupport
154
149
  store.fetch("rub-a-dub").should === "Flora de Cana"
155
150
  store.fetch("rabbit", :force => true).should be_nil # force cache miss
156
151
  store.fetch("rabbit", :force => true, :expires_in => 1.second) { @white_rabbit }
157
- store.fetch("rabbit").should === @white_rabbit ; sleep 2
152
+ store.fetch("rabbit").should == @white_rabbit
153
+ sleep 2
158
154
  store.fetch("rabbit").should be_nil
159
155
  end
160
156
  end
161
-
157
+
162
158
  private
163
159
  def instantiate_store(addresses = nil)
164
160
  ActiveSupport::Cache::RedisStore.new(addresses).instance_variable_get(:@data)
@@ -8,7 +8,7 @@ class App
8
8
  def set(key, value)
9
9
  @values[key] = value
10
10
  end
11
-
11
+
12
12
  def get(key)
13
13
  @values[key]
14
14
  end
@@ -38,28 +38,23 @@ module Sinatra
38
38
 
39
39
  it "should accept connection params" do
40
40
  redis = instantiate_store
41
- redis.host.should == "127.0.0.1"
42
- redis.port.should == 6379
43
- redis.db.should == 0
41
+ redis.to_s.should == "Redis Client connected to 127.0.0.1:6379 against DB 0"
44
42
 
45
43
  redis = instantiate_store "localhost"
46
- redis.host.should == "localhost"
47
-
44
+ redis.to_s.should == "Redis Client connected to localhost:6379 against DB 0"
45
+
48
46
  redis = instantiate_store "localhost:6380"
49
- redis.host.should == "localhost"
50
- redis.port.should == 6380
47
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 0"
51
48
 
52
49
  redis = instantiate_store "localhost:6380/13"
53
- redis.host.should == "localhost"
54
- redis.port.should == 6380
55
- redis.db.should == 13
50
+ redis.to_s.should == "Redis Client connected to localhost:6380 against DB 13"
56
51
  end
57
52
 
58
53
  it "should instantiate a ring" do
59
54
  store = instantiate_store
60
- store.should be_kind_of(MarshaledRedis)
55
+ store.should be_kind_of(Redis::MarshaledClient)
61
56
  store = instantiate_store ["localhost:6379/0", "localhost:6379/1"]
62
- store.should be_kind_of(DistributedMarshaledRedis)
57
+ store.should be_kind_of(Redis::DistributedMarshaled)
63
58
  end
64
59
 
65
60
  it "should read the data" do
@@ -92,7 +87,7 @@ module Sinatra
92
87
 
93
88
  it "should read raw data" do
94
89
  with_store_management do |store|
95
- store.read("rabbit", :raw => true).should == "\004\bU:\017OpenStruct{\006:\tname\"\nbunny"
90
+ store.read("rabbit", :raw => true).should == Marshal.dump(@rabbit)
96
91
  end
97
92
  end
98
93