jodosha-redis-store 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -4,14 +4,20 @@ h2. Installation
4
4
 
5
5
  Download and install Redis from "http://code.google.com/p/redis/":http://code.google.com/p/redis/
6
6
 
7
- wget "http://redis.googlecode.com/files/redis-0.094.tar.gz":http://redis.googlecode.com/files/redis-0.094.tar.gz
8
- tar -zxvf redis-0.094.tar.gz
9
- cd redis-0.094
7
+ curl -OL "http://redis.googlecode.com/files/redis-0.900_2.tar.gz":http://redis.googlecode.com/files/redis-0.900_2.tar.gz
8
+ tar -zxvf redis-0.900_2.tar.gz
9
+ mv redis-0.900_2 redis
10
+ cd redis
10
11
  make
11
12
 
12
13
  Install the gems
13
14
 
14
- sudo gem install ezmobius-redis-rb -s http://gems.github.com
15
+ -sudo gem install ezmobius-redis-rb -s http://gems.github.com-
16
+
17
+ git clone git://github.com/ezmobius/redis-rb.git
18
+ cd redis-rb
19
+ sudo rake install
20
+
15
21
  sudo gem install jodosha-redis-store -s http://gems.github.com
16
22
 
17
23
  h2. Cache store
@@ -26,7 +32,7 @@ h3. How to use with Rails
26
32
 
27
33
  h3. How to use with Merb
28
34
 
29
- dependency "jodosha-redis-store", "0.3.5"
35
+ dependency "jodosha-redis-store", "0.3.6"
30
36
  dependency("merb-cache", merb_gems_version) do
31
37
  Merb::Cache.setup do
32
38
  register(:redis, Merb::Cache::RedisStore, :servers => ["127.0.0.1:6379"])
@@ -65,7 +71,7 @@ h3. How to use with Rails
65
71
 
66
72
  h3. How to use with Merb
67
73
 
68
- dependency "jodosha-redis-store", "0.3.5"
74
+ dependency "jodosha-redis-store", "0.3.6"
69
75
  Merb::Config.use do |c|
70
76
  c[:session_store] = 'redis'
71
77
  end
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/testtask'
5
5
  require 'rake/rdoctask'
6
6
  require 'spec/rake/spectask'
7
7
 
8
- REDIS_STORE_VERSION = "0.3.5"
8
+ REDIS_STORE_VERSION = "0.3.6"
9
9
 
10
10
  task :default => :spec
11
11
 
@@ -28,6 +28,12 @@ task :files do
28
28
  puts "Test files:\n #{Dir['spec/**/*_spec.rb'].reject {|f| File.directory?(f)}.sort.inspect}"
29
29
  end
30
30
 
31
+ desc "Run all examples with RCov"
32
+ Spec::Rake::SpecTask.new(:rcov) do |t|
33
+ t.spec_files = FileList['spec/**/*_spec.rb']
34
+ t.rcov = true
35
+ end
36
+
31
37
  namespace :redis do
32
38
  desc 'Start the Redis cluster'
33
39
  task :start => :clean do
@@ -29,7 +29,7 @@ module Rack
29
29
  end
30
30
  session.instance_variable_set('@old', {}.merge(session))
31
31
  return [sid, session]
32
- rescue RedisError, Errno::ECONNREFUSED
32
+ rescue Errno::ECONNREFUSED
33
33
  warn "#{self} is unable to find server."
34
34
  warn $!.inspect
35
35
  return [ nil, {} ]
@@ -50,7 +50,7 @@ module Rack
50
50
  session = merge_sessions session_id, old_session, new_session, session
51
51
  @pool.set session_id, session, options
52
52
  return session_id
53
- rescue RedisError, Errno::ECONNREFUSED
53
+ rescue Errno::ECONNREFUSED
54
54
  warn "#{self} is unable to find server."
55
55
  warn $!.inspect
56
56
  return false
data/lib/redis-store.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require "redis"
2
2
  require "dist_redis"
3
- require "redis/redis"
4
- require "redis/server"
5
3
  require "redis/redis_factory"
6
4
  require "redis/marshaled_redis"
7
5
  require "redis/distributed_marshaled_redis"
@@ -11,11 +11,15 @@ class MarshaledRedis < Redis
11
11
 
12
12
  def get(key, options = nil)
13
13
  result = super key
14
- result = Marshal.load result if result && !raw?(options)
14
+ result = Marshal.load result if unmarshal?(result, options)
15
15
  result
16
16
  end
17
-
17
+
18
18
  private
19
+ def unmarshal?(result, options)
20
+ result && result.size > 0 && !raw?(options)
21
+ end
22
+
19
23
  def raw?(options)
20
24
  options && options[:raw]
21
25
  end
data/redis-store.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "redis-store"
3
- s.version = "0.3.5"
4
- s.date = "2009-05-07"
3
+ s.version = "0.3.6"
4
+ s.date = "2009-06-18"
5
5
  s.summary = "Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks."
6
6
  s.author = "Luca Guidi"
7
7
  s.email = "guidi.luca@gmail.com"
8
8
  s.homepage = "http://lucaguidi.com"
9
9
  s.description = "Rack::Session, Rack::Cache and cache Redis stores for Ruby web frameworks."
10
10
  s.has_rdoc = true
11
- s.files = ["MIT-LICENSE", "README.textile", "Rakefile", "lib/cache/merb/redis_store.rb", "lib/cache/rails/redis_store.rb", "lib/cache/sinatra/redis_store.rb", "lib/rack/cache/redis_entitystore.rb", "lib/rack/cache/redis_metastore.rb", "lib/rack/session/merb.rb", "lib/rack/session/redis.rb", "lib/redis-store.rb", "lib/redis/distributed_marshaled_redis.rb", "lib/redis/marshaled_redis.rb", "lib/redis/redis.rb", "lib/redis/redis_factory.rb", "lib/redis/server.rb", "redis-store.gemspec", "spec/cache/merb/redis_store_spec.rb", "spec/cache/rails/redis_store_spec.rb", "spec/cache/sinatra/redis_store_spec.rb", "spec/config/master.conf", "spec/config/single.conf", "spec/config/slave.conf", "spec/rack/cache/entitystore/pony.jpg", "spec/rack/cache/entitystore/redis_spec.rb", "spec/rack/cache/metastore/redis_spec.rb", "spec/rack/session/redis_spec.rb", "spec/redis/distributed_marshaled_redis_spec.rb", "spec/redis/marshaled_redis_spec.rb", "spec/redis/redis_factory_spec.rb", "spec/redis/server_spec.rb", "spec/spec_helper.rb"]
12
- s.test_files = ["spec/cache/merb/redis_store_spec.rb", "spec/cache/rails/redis_store_spec.rb", "spec/cache/sinatra/redis_store_spec.rb", "spec/rack/cache/entitystore/redis_spec.rb", "spec/rack/cache/metastore/redis_spec.rb", "spec/rack/session/redis_spec.rb", "spec/redis/distributed_marshaled_redis_spec.rb", "spec/redis/marshaled_redis_spec.rb", "spec/redis/redis_factory_spec.rb", "spec/redis/server_spec.rb"]
11
+ s.files = ["MIT-LICENSE", "README.textile", "Rakefile", "lib/cache/merb/redis_store.rb", "lib/cache/rails/redis_store.rb", "lib/cache/sinatra/redis_store.rb", "lib/rack/cache/redis_entitystore.rb", "lib/rack/cache/redis_metastore.rb", "lib/rack/session/merb.rb", "lib/rack/session/redis.rb", "lib/redis-store.rb", "lib/redis/distributed_marshaled_redis.rb", "lib/redis/marshaled_redis.rb", "lib/redis/redis_factory.rb", "redis-store.gemspec", "spec/cache/merb/redis_store_spec.rb", "spec/cache/rails/redis_store_spec.rb", "spec/cache/sinatra/redis_store_spec.rb", "spec/config/master.conf", "spec/config/single.conf", "spec/config/slave.conf", "spec/rack/cache/entitystore/pony.jpg", "spec/rack/cache/entitystore/redis_spec.rb", "spec/rack/cache/metastore/redis_spec.rb", "spec/rack/session/redis_spec.rb", "spec/redis/distributed_marshaled_redis_spec.rb", "spec/redis/marshaled_redis_spec.rb", "spec/redis/redis_factory_spec.rb", "spec/spec_helper.rb"]
12
+ s.test_files = ["spec/cache/merb/redis_store_spec.rb", "spec/cache/rails/redis_store_spec.rb", "spec/cache/sinatra/redis_store_spec.rb", "spec/rack/cache/entitystore/redis_spec.rb", "spec/rack/cache/metastore/redis_spec.rb", "spec/rack/session/redis_spec.rb", "spec/redis/distributed_marshaled_redis_spec.rb", "spec/redis/marshaled_redis_spec.rb", "spec/redis/redis_factory_spec.rb"]
13
13
  s.extra_rdoc_files = ["README.textile"]
14
14
  end
@@ -16,21 +16,21 @@ module Merb
16
16
 
17
17
  it "should accept connection params" do
18
18
  redis = instantiate_store
19
- redis.host.should == "localhost"
20
- redis.port.should == "6379"
19
+ redis.host.should == "127.0.0.1"
20
+ redis.port.should == 6379
21
21
  redis.db.should == 0
22
22
 
23
- redis = instantiate_store "redis.com"
24
- redis.host.should == "redis.com"
25
-
26
- redis = instantiate_store "redis.com:6380"
27
- redis.host.should == "redis.com"
28
- redis.port.should == "6380"
23
+ redis = instantiate_store "localhost"
24
+ redis.host.should == "localhost"
25
+
26
+ redis = instantiate_store "localhost:6380"
27
+ redis.host.should == "localhost"
28
+ redis.port.should == 6380
29
29
 
30
- redis = instantiate_store "redis.com:6380/23"
31
- redis.host.should == "redis.com"
32
- redis.port.should == "6380"
33
- redis.db.should == 23
30
+ redis = instantiate_store "localhost:6380/13"
31
+ redis.host.should == "localhost"
32
+ redis.port.should == 6380
33
+ redis.db.should == 13
34
34
  end
35
35
 
36
36
  it "should instantiate a ring" do
@@ -87,6 +87,13 @@ module Merb
87
87
  end
88
88
  end
89
89
 
90
+ it "should write all the data" do
91
+ with_store_management do |store|
92
+ store.write_all "rabbit", @white_rabbit
93
+ store.read("rabbit").should === @white_rabbit
94
+ end
95
+ end
96
+
90
97
  it "should fetch data" do
91
98
  with_store_management do |store|
92
99
  store.fetch("rabbit").should == @rabbit
@@ -117,6 +124,13 @@ module Merb
117
124
  end
118
125
  end
119
126
 
127
+ it "should delete all the data with bang method" do
128
+ with_store_management do |store|
129
+ store.delete_all!
130
+ store.instance_variable_get(:@data).keys("*").should be_empty
131
+ end
132
+ end
133
+
120
134
  private
121
135
  def instantiate_store(addresses = nil)
122
136
  Merb::Cache::RedisStore.new(:servers => [addresses].flatten).instance_variable_get(:@data)
@@ -17,21 +17,21 @@ module ActiveSupport
17
17
 
18
18
  it "should accept connection params" do
19
19
  redis = instantiate_store
20
- redis.host.should == "localhost"
21
- redis.port.should == "6379"
20
+ redis.host.should == "127.0.0.1"
21
+ redis.port.should == 6379
22
22
  redis.db.should == 0
23
23
 
24
- redis = instantiate_store "redis.com"
25
- redis.host.should == "redis.com"
24
+ redis = instantiate_store "localhost"
25
+ redis.host.should == "localhost"
26
26
 
27
- redis = instantiate_store "redis.com:6380"
28
- redis.host.should == "redis.com"
29
- redis.port.should == "6380"
30
-
31
- redis = instantiate_store "redis.com:6380/23"
32
- redis.host.should == "redis.com"
33
- redis.port.should == "6380"
34
- redis.db.should == 23
27
+ redis = instantiate_store "localhost:6380"
28
+ redis.host.should == "localhost"
29
+ redis.port.should == 6380
30
+
31
+ redis = instantiate_store "localhost:6380/13"
32
+ redis.host.should == "localhost"
33
+ redis.port.should == 6380
34
+ redis.db.should == 13
35
35
  end
36
36
 
37
37
  it "should instantiate a ring" do
@@ -38,21 +38,21 @@ module Sinatra
38
38
 
39
39
  it "should accept connection params" do
40
40
  redis = instantiate_store
41
- redis.host.should == "localhost"
42
- redis.port.should == "6379"
41
+ redis.host.should == "127.0.0.1"
42
+ redis.port.should == 6379
43
43
  redis.db.should == 0
44
44
 
45
- redis = instantiate_store "redis.com"
46
- redis.host.should == "redis.com"
45
+ redis = instantiate_store "localhost"
46
+ redis.host.should == "localhost"
47
47
 
48
- redis = instantiate_store "redis.com:6380"
49
- redis.host.should == "redis.com"
50
- redis.port.should == "6380"
51
-
52
- redis = instantiate_store "redis.com:6380/23"
53
- redis.host.should == "redis.com"
54
- redis.port.should == "6380"
55
- redis.db.should == 23
48
+ redis = instantiate_store "localhost:6380"
49
+ redis.host.should == "localhost"
50
+ redis.port.should == 6380
51
+
52
+ redis = instantiate_store "localhost:6380/13"
53
+ redis.host.should == "localhost"
54
+ redis.port.should == 6380
55
+ redis.db.should == 13
56
56
  end
57
57
 
58
58
  it "should instantiate a ring" do
@@ -14,9 +14,9 @@ port 6380
14
14
  # If you want you can bind a single interface, if the bind option is not
15
15
  # specified all the interfaces will listen for connections.
16
16
  #
17
- # bind 127.0.0.1
17
+ bind 127.0.0.1
18
18
 
19
- # Close the connection after a client is idle for N seconds
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
20
  timeout 300
21
21
 
22
22
  # Save the DB on disk:
@@ -34,6 +34,9 @@ save 900 1
34
34
  save 300 10
35
35
  save 60 10000
36
36
 
37
+ # The filename where to dump the DB
38
+ dbfilename master.rdb
39
+
37
40
  # For default save/load DB in/from the working directory
38
41
  # Note that you must specify a directory not a file name.
39
42
  dir ./tmp
@@ -50,7 +53,9 @@ loglevel debug
50
53
  # output for logging but daemonize, logs will be sent to /dev/null
51
54
  logfile stdout
52
55
 
53
- # Set the number of databases.
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
54
59
  databases 16
55
60
 
56
61
  ################################# REPLICATION #################################
@@ -73,6 +78,35 @@ databases 16
73
78
 
74
79
  # requirepass foobared
75
80
 
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
76
110
  ############################### ADVANCED CONFIG ###############################
77
111
 
78
112
  # Glue small output buffers together in order to send small replies in a
@@ -84,4 +118,15 @@ glueoutputbuf yes
84
118
  # string in your dataset, but performs lookups against the shared objects
85
119
  # pool so it uses more CPU and can be a bit slower. Usually it's a good
86
120
  # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
87
131
  shareobjects no
132
+ shareobjectspoolsize 1024
@@ -14,9 +14,9 @@ port 6379
14
14
  # If you want you can bind a single interface, if the bind option is not
15
15
  # specified all the interfaces will listen for connections.
16
16
  #
17
- # bind 127.0.0.1
17
+ bind 127.0.0.1
18
18
 
19
- # Close the connection after a client is idle for N seconds
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
20
  timeout 5
21
21
 
22
22
  # Save the DB on disk:
@@ -34,6 +34,9 @@ save 900 1
34
34
  save 300 10
35
35
  save 60 10000
36
36
 
37
+ # The filename where to dump the DB
38
+ dbfilename single.rdb
39
+
37
40
  # For default save/load DB in/from the working directory
38
41
  # Note that you must specify a directory not a file name.
39
42
  dir ./tmp
@@ -50,7 +53,9 @@ loglevel debug
50
53
  # output for logging but daemonize, logs will be sent to /dev/null
51
54
  logfile stdout
52
55
 
53
- # Set the number of databases.
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
54
59
  databases 16
55
60
 
56
61
  ################################# REPLICATION #################################
@@ -73,6 +78,35 @@ databases 16
73
78
 
74
79
  # requirepass foobared
75
80
 
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
76
110
  ############################### ADVANCED CONFIG ###############################
77
111
 
78
112
  # Glue small output buffers together in order to send small replies in a
@@ -84,4 +118,15 @@ glueoutputbuf yes
84
118
  # string in your dataset, but performs lookups against the shared objects
85
119
  # pool so it uses more CPU and can be a bit slower. Usually it's a good
86
120
  # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
87
131
  shareobjects no
132
+ shareobjectspoolsize 1024
@@ -14,9 +14,9 @@ port 6381
14
14
  # If you want you can bind a single interface, if the bind option is not
15
15
  # specified all the interfaces will listen for connections.
16
16
  #
17
- # bind 127.0.0.1
17
+ bind 127.0.0.1
18
18
 
19
- # Close the connection after a client is idle for N seconds
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
20
  timeout 300
21
21
 
22
22
  # Save the DB on disk:
@@ -34,6 +34,9 @@ save 900 1
34
34
  save 300 10
35
35
  save 60 10000
36
36
 
37
+ # The filename where to dump the DB
38
+ dbfilename slave.rdb
39
+
37
40
  # For default save/load DB in/from the working directory
38
41
  # Note that you must specify a directory not a file name.
39
42
  dir ./tmp
@@ -50,7 +53,9 @@ loglevel debug
50
53
  # output for logging but daemonize, logs will be sent to /dev/null
51
54
  logfile stdout
52
55
 
53
- # Set the number of databases.
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
54
59
  databases 16
55
60
 
56
61
  ################################# REPLICATION #################################
@@ -60,7 +65,7 @@ databases 16
60
65
  # so for example it is possible to configure the slave to save the DB with a
61
66
  # different interval, or to listen to another port, and so on.
62
67
 
63
- # slaveof 127.0.0.1 6380
68
+ slaveof 127.0.0.1 6380
64
69
 
65
70
  ################################## SECURITY ###################################
66
71
 
@@ -73,6 +78,35 @@ databases 16
73
78
 
74
79
  # requirepass foobared
75
80
 
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
76
110
  ############################### ADVANCED CONFIG ###############################
77
111
 
78
112
  # Glue small output buffers together in order to send small replies in a
@@ -84,4 +118,15 @@ glueoutputbuf yes
84
118
  # string in your dataset, but performs lookups against the shared objects
85
119
  # pool so it uses more CPU and can be a bit slower. Usually it's a good
86
120
  # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
87
131
  shareobjects no
132
+ shareobjectspoolsize 1024
@@ -16,14 +16,14 @@ module Rack
16
16
  cache = Rack::Cache::EntityStore::Redis.resolve(uri("redis://127.0.0.1")).cache
17
17
  cache.should be_kind_of(::Redis)
18
18
  cache.host.should == "127.0.0.1"
19
- cache.port.should == "6379"
20
- cache.db.should == "0"
19
+ cache.port.should == 6379
20
+ cache.db.should == 0
21
21
 
22
22
  cache = Rack::Cache::EntityStore::Redis.resolve(uri("redis://127.0.0.1:6380")).cache
23
23
  cache.port.should == 6380
24
24
 
25
- cache = Rack::Cache::EntityStore::Redis.resolve(uri("redis://127.0.0.1/11")).cache
26
- cache.db.should == "11"
25
+ cache = Rack::Cache::EntityStore::Redis.resolve(uri("redis://127.0.0.1/13")).cache
26
+ cache.db.should == 13
27
27
  end
28
28
 
29
29
  it 'responds to all required messages' do
@@ -16,14 +16,14 @@ module Rack
16
16
  cache = Rack::Cache::MetaStore::Redis.resolve(uri("redis://127.0.0.1")).cache
17
17
  cache.should be_kind_of(::MarshaledRedis)
18
18
  cache.host.should == "127.0.0.1"
19
- cache.port.should == "6379"
20
- cache.db.should == "0"
19
+ cache.port.should == 6379
20
+ cache.db.should == 0
21
21
 
22
22
  cache = Rack::Cache::MetaStore::Redis.resolve(uri("redis://127.0.0.1:6380")).cache
23
23
  cache.port.should == 6380
24
24
 
25
- cache = Rack::Cache::MetaStore::Redis.resolve(uri("redis://127.0.0.1/11")).cache
26
- cache.db.should == "11"
25
+ cache = Rack::Cache::MetaStore::Redis.resolve(uri("redis://127.0.0.1/13")).cache
26
+ cache.db.should == 13
27
27
  end
28
28
 
29
29
  it 'writes a list of negotation tuples with #write' do
@@ -29,7 +29,7 @@ module Rack
29
29
  pool = Rack::Session::Redis.new(@incrementor, :redis_server => "localhost:6380/1").pool
30
30
  pool.should be_kind_of(MarshaledRedis)
31
31
  pool.host.should == "localhost"
32
- pool.port.should == "6380"
32
+ pool.port.should == 6380
33
33
  pool.db.should == 1
34
34
 
35
35
  pool = Rack::Session::Redis.new(@incrementor, :redis_server => ["localhost:6379", "localhost:6380"]).pool
@@ -153,7 +153,7 @@ module Rack
153
153
  # anyone know how to do this better?
154
154
  specify "multithread: should cleanly merge sessions" do
155
155
  next unless $DEBUG
156
- warn 'Running multithread test for Session::Memcache'
156
+ warn 'Running multithread test for Session::Redis'
157
157
  pool = Rack::Session::Redis.new(@incrementor)
158
158
  req = Rack::MockRequest.new(pool)
159
159
 
@@ -16,11 +16,11 @@ describe "DistributedMarshaledRedis" do
16
16
  end
17
17
 
18
18
  it "should accept connection params" do
19
- dmr = DistributedMarshaledRedis.new [ :host => "redis.com", :port => "6380", :db => 1 ]
19
+ dmr = DistributedMarshaledRedis.new [ :host => "localhost", :port => "6380", :db => "1" ]
20
20
  dmr.ring.should have(1).node
21
21
  mr = dmr.ring.nodes.first
22
- mr.host.should == "redis.com"
23
- mr.port.should == "6380"
22
+ mr.host.should == "localhost"
23
+ mr.port.should == 6380
24
24
  mr.db.should == 1
25
25
  end
26
26
 
@@ -27,6 +27,11 @@ describe "MarshaledRedis" do
27
27
  @mr.get("rabbit", :raw => true).should == %(#<OpenStruct color="white">)
28
28
  end
29
29
 
30
+ it "should not unmarshal object if getting an empty string" do
31
+ @mr.set "empty_string", ""
32
+ lambda { @mr.get("empty_string").should == "" }.should_not raise_error
33
+ end
34
+
30
35
  it "should not set an object if already exist" do
31
36
  @mr.set_unless_exists "rabbit", @white_rabbit
32
37
  @mr.get("rabbit").should === @rabbit
@@ -4,27 +4,27 @@ describe "RedisFactory" do
4
4
  it "should instantiate a MarshaledRedis store" do
5
5
  store = RedisFactory.create
6
6
  store.should be_kind_of(MarshaledRedis)
7
- store.host.should == "localhost"
8
- store.port.should == "6379"
7
+ store.host.should == "127.0.0.1"
8
+ store.port.should == 6379
9
9
  store.db.should == 0
10
10
  end
11
11
 
12
12
  it "should allow to specify host" do
13
- store = RedisFactory.create "redis.com"
14
- store.host.should == "redis.com"
13
+ store = RedisFactory.create "localhost"
14
+ store.host.should == "localhost"
15
15
  end
16
16
 
17
17
  it "should allow to specify port" do
18
- store = RedisFactory.create "redis.com:6380"
19
- store.host.should == "redis.com"
20
- store.port.should == "6380"
18
+ store = RedisFactory.create "localhost:6380"
19
+ store.host.should == "localhost"
20
+ store.port.should == 6380
21
21
  end
22
22
 
23
23
  it "should allow to specify db" do
24
- store = RedisFactory.create "redis.com:6380/23"
25
- store.host.should == "redis.com"
26
- store.port.should == "6380"
27
- store.db.should == 23
24
+ store = RedisFactory.create "localhost:6380/13"
25
+ store.host.should == "localhost"
26
+ store.port.should == 6380
27
+ store.db.should == 13
28
28
  end
29
29
 
30
30
  it "should instantiate a DistributedMarshaledRedis store" do
data/spec/spec_helper.rb CHANGED
@@ -12,4 +12,5 @@ require "activesupport"
12
12
  require "cache/rails/redis_store"
13
13
  require "cache/sinatra/redis_store"
14
14
 
15
- class Redis; attr_reader :db end
15
+ class Redis; attr_reader :host, :port, :db end
16
+ $DEBUG = ENV["DEBUG"] === "true"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jodosha-redis-store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-07 00:00:00 -07:00
12
+ date: 2009-06-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -35,9 +35,7 @@ files:
35
35
  - lib/redis-store.rb
36
36
  - lib/redis/distributed_marshaled_redis.rb
37
37
  - lib/redis/marshaled_redis.rb
38
- - lib/redis/redis.rb
39
38
  - lib/redis/redis_factory.rb
40
- - lib/redis/server.rb
41
39
  - redis-store.gemspec
42
40
  - spec/cache/merb/redis_store_spec.rb
43
41
  - spec/cache/rails/redis_store_spec.rb
@@ -52,7 +50,6 @@ files:
52
50
  - spec/redis/distributed_marshaled_redis_spec.rb
53
51
  - spec/redis/marshaled_redis_spec.rb
54
52
  - spec/redis/redis_factory_spec.rb
55
- - spec/redis/server_spec.rb
56
53
  - spec/spec_helper.rb
57
54
  has_rdoc: true
58
55
  homepage: http://lucaguidi.com
@@ -90,4 +87,3 @@ test_files:
90
87
  - spec/redis/distributed_marshaled_redis_spec.rb
91
88
  - spec/redis/marshaled_redis_spec.rb
92
89
  - spec/redis/redis_factory_spec.rb
93
- - spec/redis/server_spec.rb
data/lib/redis/redis.rb DELETED
@@ -1,14 +0,0 @@
1
- # Foreword compatibility with Redis > 0.0.3
2
- class Redis
3
- def set(key, val, expiry=nil)
4
- write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
5
- s = get_response == OK
6
- return expire(key, expiry) if s && expiry
7
- s
8
- end
9
-
10
- def expire(key, expiry=nil)
11
- write("EXPIRE #{key} #{expiry}\r\n")
12
- get_response == 1
13
- end
14
- end
data/lib/redis/server.rb DELETED
@@ -1,225 +0,0 @@
1
- require 'monitor'
2
- ##
3
- # This class represents a redis server instance.
4
-
5
- class Server
6
-
7
- ##
8
- # The host the redis server is running on.
9
-
10
- attr_reader :host
11
-
12
- ##
13
- # The port the redis server is listening on.
14
-
15
- attr_reader :port
16
-
17
- ##
18
- #
19
-
20
- attr_reader :replica
21
-
22
- ##
23
- # The time of next retry if the connection is dead.
24
-
25
- attr_reader :retry
26
-
27
- ##
28
- # A text status string describing the state of the server.
29
-
30
- attr_reader :status
31
-
32
- ##
33
- # Create a new Redis::Server object for the redis instance
34
- # listening on the given host and port.
35
-
36
- def initialize(host, port = DEFAULT_PORT, timeout = 10, size = 5)
37
- raise ArgumentError, "No host specified" if host.nil? or host.empty?
38
- raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
39
-
40
- @host = host
41
- @port = port.to_i
42
-
43
- @retry = nil
44
- @status = 'NOT CONNECTED'
45
- @timeout = timeout
46
- @size = size
47
-
48
- @reserved_sockets = {}
49
-
50
- @mutex = Monitor.new
51
- @queue = @mutex.new_cond
52
-
53
- @sockets = []
54
- @checked_out = []
55
- end
56
-
57
- ##
58
- # Return a string representation of the server object.
59
- def inspect
60
- "<Redis::Server: %s:%d (%s)>" % [@host, @port, @status]
61
- end
62
-
63
- ##
64
- # Try to connect to the redis server targeted by this object.
65
- # Returns the connected socket object on success or nil on failure.
66
-
67
- def socket
68
- if socket = @reserved_sockets[current_connection_id]
69
- socket
70
- else
71
- @reserved_sockets[current_connection_id] = checkout
72
- end
73
- end
74
-
75
- def connect_to(host, port, timeout=nil)
76
- addrs = Socket.getaddrinfo(host, nil)
77
- addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
78
- sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
79
- if timeout
80
- secs = Integer(timeout)
81
- usecs = Integer((timeout - secs) * 1_000_000)
82
- optval = [secs, usecs].pack("l_2")
83
- sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
84
- sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
85
- end
86
- sock.connect(Socket.pack_sockaddr_in(port, addr[3]))
87
- sock
88
- end
89
-
90
- ##
91
- # Close the connection to the redis server targeted by this
92
- # object. The server is not considered dead.
93
-
94
- def close
95
- @reserved_sockets.each do |name,sock|
96
- checkin sock
97
- end
98
- @reserved_sockets = {}
99
- @sockets.each do |sock|
100
- sock.close
101
- end
102
- @sockets = []
103
- @status = "NOT CONNECTED"
104
- end
105
-
106
- ##
107
- # Mark the server as dead and close its socket.
108
- def mark_dead(sock, error)
109
- sock.close if sock && !sock.closed?
110
- sock = nil
111
-
112
- reason = "#{error.class.name}: #{error.message}"
113
- @status = sprintf "%s:%s DEAD (%s)", @host, @port, reason
114
- puts @status
115
- end
116
-
117
- protected
118
- def new_socket
119
- sock = nil
120
- begin
121
- sock = connect_to(@host, @port, @timeout)
122
- sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
123
- @status = 'CONNECTED'
124
- rescue Errno::EPIPE, Errno::ECONNREFUSED => e
125
- if sock
126
- puts "Socket died... socket: #{sock.inspect}\n" if $debug
127
- sock.close
128
- end
129
- rescue SocketError, SystemCallError, IOError => err
130
- puts "Unable to open socket: #{err.class.name}, #{err.message}" if $debug
131
- mark_dead sock, err
132
- end
133
-
134
- return sock
135
- end
136
-
137
- def checkout
138
- @mutex.synchronize do
139
- loop do
140
- socket = if @checked_out.size < @sockets.size
141
- checkout_existing_socket
142
- elsif @sockets.size < @size
143
- checkout_new_socket
144
- end
145
- return socket if socket
146
- # No sockets available; wait for one
147
- if @queue.wait(@timeout)
148
- next
149
- else
150
- # try looting dead threads
151
- clear_stale_cached_sockets!
152
- if @size == @checked_out.size
153
- raise RedisError, "could not obtain a socket connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
154
- end
155
- end
156
- end
157
- end
158
- end
159
-
160
- def checkin(socket)
161
- @mutex.synchronize do
162
- @checked_out.delete socket
163
- @queue.signal
164
- end
165
- end
166
-
167
- def checkout_new_socket
168
- s = new_socket
169
- @sockets << s
170
- checkout_and_verify(s)
171
- end
172
-
173
- def checkout_existing_socket
174
- s = (@sockets - @checked_out).first
175
- checkout_and_verify(s)
176
- end
177
-
178
- def clear_stale_cached_sockets!
179
- remove_stale_cached_threads!(@reserved_sockets) do |name, socket|
180
- checkin socket
181
- end
182
- end
183
-
184
- def remove_stale_cached_threads!(cache, &block)
185
- keys = Set.new(cache.keys)
186
-
187
- Thread.list.each do |thread|
188
- keys.delete(thread.object_id) if thread.alive?
189
- end
190
- keys.each do |key|
191
- next unless cache.has_key?(key)
192
- block.call(key, cache[key])
193
- cache.delete(key)
194
- end
195
- end
196
-
197
- private
198
- def current_connection_id #:nodoc:
199
- Thread.current.object_id
200
- end
201
-
202
- def checkout_and_verify(s)
203
- s = verify!(s)
204
- @checked_out << s
205
- s
206
- end
207
-
208
- def verify!(s)
209
- reconnect!(s) unless active?(s)
210
- end
211
-
212
- def reconnect!(s)
213
- s.close
214
- connect_to(@host, @port, @timeout)
215
- end
216
-
217
- def active?(s)
218
- begin
219
- s.write("\0")
220
- Timeout.timeout(0.1){ s.read }
221
- rescue Exception
222
- false
223
- end
224
- end
225
- end
@@ -1,25 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "/../spec_helper")
2
-
3
- describe "Redis::Server" do
4
- before(:each) do
5
- @server = Server.new 'localhost', '6379'
6
- end
7
-
8
- it "should have a connection pool" do
9
- @server.instance_variable_get(:@sockets).should_not be_nil
10
- end
11
-
12
- it "should checkout active connections" do
13
- threads = []
14
- 100.times do
15
- threads << Thread.new do
16
- lambda {
17
- sleep 6 # redis will close idle connections in the meanwhile
18
- socket = @server.socket
19
- socket.write("INFO\r\n")
20
- socket.read(1)
21
- }.should_not raise_error(Exception)
22
- end
23
- end
24
- end
25
- end