redis-store 1.0.0.1 → 1.1.0.rc

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 (53) hide show
  1. data/Gemfile +2 -34
  2. data/README.md +17 -220
  3. data/Rakefile +7 -54
  4. data/lib/redis-store.rb +11 -44
  5. data/lib/redis/distributed_store.rb +8 -1
  6. data/lib/redis/factory.rb +17 -21
  7. data/lib/redis/store.rb +3 -8
  8. data/lib/redis/store/interface.rb +4 -0
  9. data/lib/redis/store/marshalling.rb +4 -0
  10. data/lib/redis/store/version.rb +1 -8
  11. data/lib/tasks/redis.tasks.rb +167 -0
  12. data/redis-store.gemspec +22 -97
  13. data/{spec → test}/config/node-one.conf +2 -2
  14. data/{spec → test}/config/node-two.conf +2 -2
  15. data/{spec → test}/config/redis.conf +3 -2
  16. data/{spec/redis/distributed_store_spec.rb → test/redis/distributed_store_test.rb} +13 -15
  17. data/test/redis/factory_test.rb +98 -0
  18. data/test/redis/store/interface_test.rb +27 -0
  19. data/test/redis/store/marshalling_test.rb +127 -0
  20. data/test/redis/store/namespace_test.rb +86 -0
  21. data/test/redis/store/version_test.rb +7 -0
  22. data/test/redis/store_test.rb +17 -0
  23. data/test/test_helper.rb +22 -0
  24. metadata +85 -97
  25. data/.travis.yml +0 -7
  26. data/CHANGELOG +0 -311
  27. data/VERSION +0 -1
  28. data/lib/action_controller/session/redis_session_store.rb +0 -81
  29. data/lib/active_support/cache/redis_store.rb +0 -254
  30. data/lib/cache/merb/redis_store.rb +0 -79
  31. data/lib/cache/sinatra/redis_store.rb +0 -131
  32. data/lib/i18n/backend/redis.rb +0 -67
  33. data/lib/rack/cache/redis_entitystore.rb +0 -48
  34. data/lib/rack/cache/redis_metastore.rb +0 -40
  35. data/lib/rack/session/merb.rb +0 -32
  36. data/lib/rack/session/redis.rb +0 -88
  37. data/spec/action_controller/session/redis_session_store_spec.rb +0 -126
  38. data/spec/active_support/cache/redis_store_spec.rb +0 -426
  39. data/spec/cache/merb/redis_store_spec.rb +0 -143
  40. data/spec/cache/sinatra/redis_store_spec.rb +0 -192
  41. data/spec/i18n/backend/redis_spec.rb +0 -72
  42. data/spec/rack/cache/entitystore/pony.jpg +0 -0
  43. data/spec/rack/cache/entitystore/redis_spec.rb +0 -124
  44. data/spec/rack/cache/metastore/redis_spec.rb +0 -259
  45. data/spec/rack/session/redis_spec.rb +0 -234
  46. data/spec/redis/factory_spec.rb +0 -110
  47. data/spec/redis/store/interface_spec.rb +0 -23
  48. data/spec/redis/store/marshalling_spec.rb +0 -119
  49. data/spec/redis/store/namespace_spec.rb +0 -76
  50. data/spec/redis/store/version_spec.rb +0 -7
  51. data/spec/redis/store_spec.rb +0 -13
  52. data/spec/spec_helper.rb +0 -43
  53. data/tasks/redis.tasks.rb +0 -235
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- bundler_args: "--without development"
2
- script: "bundle exec rake"
3
- rvm:
4
- - 1.8.7
5
- - 1.9.2
6
- - jruby
7
- - rbx
data/CHANGELOG DELETED
@@ -1,311 +0,0 @@
1
- *1.0.0.1 (September 6, 2011)*
2
-
3
- * Bump version v1.0.0.1
4
-
5
- *1.0.0 (September 1, 2011)*
6
-
7
- * Bump version v1.0.0
8
- * Avoid encoding issues when sending strings to Redis. [Damian Janowski]
9
- * Fixed a bug that caused all the users to share the same session (with a session_id of 0 or 1) [Mathieu Ravaux]
10
- * ActiveSupport cache stores reply to read_multi with a hash, not an array. [Matt Griffin]
11
- * Rack::Session && Rack::Cache store can be created with options [aligo]
12
- * add destroy_session [aligo]
13
- * compatible with rails 3.1. rely on Rack::Session::Redis stores API. [aligo]
14
- * Fixed Marshalling semantic
15
-
16
- *1.0.0 [rc1] (June 5, 2011)*
17
-
18
- * Bump version v1.0.0.rc1
19
- * Re-implement the delete_entry because it s needed by the ActiveSupport::Cache [Cyril Mougel]
20
- * Change readme documentation for rack-cache to use single redis database with namespace support per Redis maintainers recommendation [Travis D. Warlick, Jr.]
21
- * Rack-Cache entity and meta store base classes should use the Redis::Factory.convert_to_redis_client_options method for DRYness and for namespace support [Travis D. Warlick, Jr.]
22
- * Modify #fetch for cache stores to return the yielded value instead of OK [Rune Botten]
23
- * Minor revisions to Readme language and organization [Jeff Casimir]
24
- * Re-implement the delete_entry because it s needed by the ActiveSupport::Cache implementation in Rails 3 [Cyril Mougel]
25
- * Refactor delete_matched [Andrei Kulakov]
26
-
27
- *1.0.0 [beta5] (April 2, 2011)*
28
-
29
- * Bump version v1.0.0.beta5
30
- * Introducing Rails.cache.reconnect, useful for Unicorn environments. Closes #21
31
- * Namespaced i18n backend should strip namespace when lists available locales. Closes #62
32
- * how to configure Redis session store in Sinatra [Christian von Kleist]
33
- * gracefully handle Connection Refused errors, allows database fall-back on fetch [Michael Kintzer]
34
- * Sinatra's API must have changed, its registered not register. [Michael D'Auria]
35
-
36
- *1.0.0 [beta4] (December 15, 2010)*
37
-
38
- * Bump version v1.0.0.beta4
39
- * Force Rails cache store when ActiveSupport is detected
40
- * Make sure to accept options when :servers isn't passed
41
- * Always force session store loading when using Rails
42
- * Make it compatible with Rails 3 sessions store [Andre Medeiros]
43
- * Better Rails 3 detection [Andre Medeiros]
44
- * Added support for password in Redis URI resolution [Jacob Gyllenstierna]
45
- * fix deleting values from session hash [Calvin Yu]
46
- * Use defensive design when build options for a store
47
- * ActiveSupport cache store doesn't accept any argument in Rails 2
48
- * Updated dependencies
49
-
50
- *1.0.0 [beta3] (September 10, 2010)*
51
-
52
- * Bump version v1.0.0.beta3
53
- * Updated gemspec
54
- * Made compatible with Ruby 1.9.2
55
- * Made compatible with Rails 3
56
- * Making the redis-store rails session store compatible with Rails 2.3.9. [Aaron Gibralter]
57
- * Updated to v2.3.9 the development dependencies for Rails 2.x
58
- * Added password support
59
- * Set default URI string for Rack session store according to redis gem
60
- * Require redis:// scheme as mandatory part of URI string
61
- * Updated locked dependencies
62
- * Added namespace support for Redis::DistrubutedStore
63
- * Specs for Interface module
64
- * Moved a spec to reflect lib/ structure
65
- * Made specs run again with bundler-1.0.0.rc.2
66
- * Prepare for bundler-1.0.0.rc.1
67
- * Use tmp/ for Redis database dumps
68
- * README, gemspec
69
- * Lookup for scoped keys
70
- * Introducing I18n::Backend::Redis
71
- * Don't pollute Redis namespace. closes #16
72
- * Don't look twice for Rails module
73
- * Fixed loading ActionDispatch::Session issue with Rails 3
74
- * ActiveSupport cache now uses new Rails 3 namespace API
75
- * Let Rack::Cache entity store to use plain Redis client
76
- * CHANGELOG
77
- * Gemspec
78
- * Redis::DistributedMarshaled => Redis::DistributedStore
79
- * Extracted expiration logic in Redis::Ttl
80
- * Introducing Redis::Store and extracted marshalling logic into Redis::Marshalling
81
- * CHANGELOG
82
- * Gemspec
83
- * Removed superfluous specs. Documentation.
84
- * Made the specs pass with Ruby 1.9.2 and Rails 3
85
- * Made the specs pass with Ruby 1.9.2
86
- * Prepare specs for Ruby 1.9.2
87
- * Made the specs pass for Rails 3
88
- * Namespaced keys for ActiveSupport::Cache::RedisStore
89
- * Got RedisSessionStore working again with namespace
90
- * Delegate MarshaledClient#to_s decoration to Redis::Namespace
91
- * Redis::Namespace
92
- * Accept :namespace option when instantiate a store
93
- * Test against merb-1.1.0 for now
94
- * Updated Rake tasks for Redis installation
95
- * Advanced configurations for Rails in README. closes #8
96
- * Locked gems
97
- * Changed the gemspec according to the new directories structure
98
- * Changed directories structure, according to the ActiveSupport loading policies
99
- * Typo in CHANGELOG
100
-
101
- *1.0.0 [beta2] (June 12, 2010)*
102
-
103
- * Bump version v1.0.0.beta2
104
- * Added committers names to CHANGELOG
105
- * Added CHANGELOG
106
- * Rake namespace: redis_cluster => redis:cluster
107
- * Fixed Rails 3 failing spec
108
- * Added ActiveSupport::Cache::RedisStore#read_multi
109
- * Enabled notifications for ActiveSupport::Cache::RedisStore
110
- * Moved spec
111
- * Use consistent Rails 3 check in session store
112
- * Make sure of use top-level module
113
- * Updated Rails 2.x development dependencies
114
-
115
- *1.0.0 [beta1] (June 9, 2010)*
116
-
117
- * Bump version v1.0.0.beta1
118
- * Made specs pass for Ruby 1.9.1
119
- * Updated instructions to test against Rails 3.x
120
- * Updated copyright year
121
- * Prepare for v1.0.0.beta1
122
- * Use Rails v3.0.0.beta4
123
- * Require redis >= 2.0.0 gem in Gemfile
124
- * Updated instructions for Rails 2 session store
125
- * Added instructions for Rails 3 configuration
126
- * Check against Rails#version instead of Rails::VERSION::STRING
127
- * Added redis gem as dependency
128
- * Changed spec_helper.rb according to the new Rails 3 check
129
- * Fix the rails3 check [Bruno Michel]
130
- * Added bundler cleanup Rake task
131
- * Fixed ActiveSupport::Cache::RedisStore#fetch
132
- * Re-enabled redis:console Rake task
133
- * Don't use Rspec#have with ActiveSupport 3
134
- * Fixed Rails 2 regression for ActiveSupport::Cache::RedisStore#delete_matched
135
- * Fixed issues for ActiveSupport::Cache::RedisStore #delete_matched, #read, #rwrite
136
- * Rails 3 namespace
137
- * Use Rails edge instead of 3.0.0.beta3
138
- * Made the specs run again
139
- * Updated Gemfile development/test dependencies for rails3.
140
- * Updated README instructions
141
- * Updated test configuration files, according to redis-2.0.0-rc1 defaults
142
- * Made specs pass with redis-2.0.0 gem
143
- * Don't support the old redis-rb namespace
144
- * Import the whole redis-rb old Rake tasks
145
- * Updated redis-rb dependency
146
-
147
- *0.3.8 [Final] (May 21, 2010)*
148
-
149
- * Fixed gemspec executable issue
150
- * Updated .gemspec
151
- * Version bump to 0.3.8
152
- * Started namespace migration for redis-2.0.0
153
- * Merge branch 'master' of http://github.com/akahn/redis-store
154
- * Fixed dependency issues. [Brian Takita]
155
- * Removed methopara because it require ruby 1.9.1. [Brian Takita]
156
- * Merge branch 'master' of http://github.com/dsander/redis-store [Brian Takita]
157
- * Moved RedisSessionStore to rack session directory. [Brian Takita]
158
- * Got RedisSessionStore working with passing specs. [Brian Takita]
159
- * Using modified version of RedisSessionStore (http://github.com/mattmatt/redis-session-store). Still needs testing. [Brian Takita]
160
- * Using redis 1.0.5. [Brian Takita]
161
- * Ignoring gem files and gemspecs with a prefix. [Brian Takita]
162
- * Added git and jeweler to dependencies. [Brian Takita]
163
- * Ignoring .bundle and rubymine files. [Brian Takita]
164
- * Added ability to add a prefix to the gem name via the GEM_PREFIX environment variable. [Brian Takita]
165
- * Fixed marshalling issues with the new Redis api. Fixed redis-server configurations for the latest version of redis-server. Added redis_cluster:start and redis_cluster:stop rake tasks. Spec suite passes. [Brian Takita]
166
- * Fixed redis deprecation warnings. [Brian Takita]
167
- * Using redis 1.0.4. Loading redis.tasks.rb from redis gem using Bundler. Fixed redis test configurations. [Brian Takita]
168
- * Supports redis 1.0.5 [Dominik Sander]
169
- * Add Rack::Session::Redis usage for Sinatra to README [Alexander Kahn]
170
- * Updated development and testing dependencies
171
- * Made compatible with rack-cache-0.5.2
172
-
173
- *0.3.7 [Final] (November 15, 2009)*
174
-
175
- * Version bump to 0.3.7
176
- * Version bump to 0.3.7
177
- * Updated spec instructions in README
178
- * Jeweler generated redis-store.gemspec
179
- * Removed no longer used Rake tasks. Added Gemcutter support via Jeweler.
180
- * Typo
181
- * Let Rcov task run with the Redis cluster started
182
- * Make spec suite pass again
183
- * README.textile -> README.md
184
- * Re-enabled Merb specs
185
- * Added *.rdb in .gitignore
186
- * Run detached Redis instances for spec suite
187
- * Start detached Redis server for specs
188
- * Added Gemfile, removed rubygems dependecy for testing.
189
- * Added *.swp to .gitignore
190
- * Added jeweler rake tasks
191
- * Make it work in classic Sinatra apps. [Dmitriy Timokhin]
192
- * Updated README with new instructions about redis-rb
193
-
194
- *0.3.6 [Final] (June 18, 2009)*
195
-
196
- * Prepare for v0.3.6
197
- * Updated README with new version
198
- * Changed Redis config files for specs
199
- * Updated README instructions
200
- * New filelist in gemspec
201
- * Fixed typo in spec
202
- * Don't try to unmarshal empty strings
203
- * Running specs according to the DEBUG env var
204
- * Added specs for uncovered Merb Cache API
205
- * Added rcov Rake task
206
- * Fix README
207
- * Fix typo in README
208
- * Updated README instructions
209
- * Removed RedisError catching
210
- * Declaring for now RedisError in spec_helper.rb
211
- * Removed unnecessary classes
212
- * Fix specs for DistributedMarshaledRedis
213
- * Fix some specs
214
- * Made Server#notify_observers private
215
- * Ensure to select the correct database after the socket connection
216
- * Reverted socket pooling. Ensure to always return an active socket.
217
- * Lowering default timeout from 10 to 0.1
218
-
219
- *0.3.5 [Final] (May 7, 2009)*
220
-
221
- * Prepare for v0.3.5
222
- * Totally replace ezmobius-redis-rb Server implementation
223
- * Fixed default Server timeout to 1 second
224
- * Little refactoring
225
- * Ensure Server#active? always returns a boolean and never raise an exception
226
- * Made configurable connection pool timeout and size
227
- * Socket connection pooling
228
- * Updated README
229
-
230
- *0.3.0 [Final] (May 3, 2009)*
231
-
232
- * Prepare for v0.3.0
233
- * README formatting issues
234
- * README formatting (again)
235
- * README formatting
236
- * Updated Rack::Session instructions for Sinatra
237
- * Make Rack::Session implementation working with Merb
238
- * Added instructions for Rack::Session
239
- * Expiration support for Rack::Session
240
- * Ensure to test Rails and Sinatra expiration implementations with both MarshaledRedis and DistrbutedMarshaledRedis
241
- * Expiration support for Merb::Cache
242
- * Use full qualified class names in specs. Minor spec fix.
243
- * Ported for foreword compatibility the expiration implementation from ezmobius/redis-rb
244
- * Little spec cleanup
245
- * Full support for Rack::Session
246
- * Extracted some logic into RedisFactory
247
- * Initial support for Rack::Session
248
-
249
- *0.2.0 [Final] (April 30, 2009)*
250
-
251
- * Prepare for v0.2.0
252
- * Links in README
253
- * Maybe someday I will use the right formatting rules at first attempt
254
- * Still formatting README
255
- * Formatting for README
256
- * Updated instructions for Rack::Cache
257
- * Don't require any cache store if no Ruby framework is detected
258
- * Implemented EntityStore for Rack::Cache
259
- * Added REDIS constant for Rack::Cache::Metastore
260
- * MetaStore implementation for Rack::Cache
261
-
262
- *0.1.0 [Final] (April 30, 2009)*
263
-
264
- * Prepare for v0.1.0
265
- * Sinatra cache support
266
-
267
- *0.0.3 [Final] (April 30, 2009)*
268
-
269
- * Prepare for v0.0.3
270
- * Updated instructions for Merb
271
- * Hack for Merb cyclic dependency
272
- * Renaming main lib
273
- * Merb store #writable always returns true. Added pending test.
274
- * Merb store #fetch refactoring
275
- * Updated Redis installation instructions
276
- * Implemented #fetch for Merb store
277
- * Implemented #exists?, #delete and #delete_all for Merb cache store
278
- * Renamed project name in Rakefile
279
- * Updated project name in README
280
- * Updated README with Merb instructions
281
- * Changed name in gemspec
282
- * New gemspec
283
- * Initial cache store support for Merb
284
- * Moving files around
285
- * Don't complain about missing db in tmp/
286
-
287
- *0.0.2 [Final] (April 16, 2009)*
288
-
289
- * Prepare for v0.0.2
290
- * Updated file list in gemspec
291
- * Updated README instructions
292
- * Re-enabled spec for RedisStore#fetch
293
- * Update rdoc
294
- * Fix RedisStore#clear when use a cluster
295
- * Test RedisStore both with single server and with a cluster
296
- * Fix port in DistributedMarshaledRedis spec
297
- * Don't slave Redis cluster node
298
- * Changed Redis cluster configuration
299
- * Added configuration for Redis cluster
300
- * Use a distributed system if the store uses more than one server
301
- * Accept params for client connection
302
-
303
- *0.0.1 [Final] (April 11, 2009)*
304
-
305
- * Added :unless_exist option to #write
306
- * Added failing specs for :expires_in option
307
- * Made optional args compatible with the Cache interface
308
- * Implemented #delete_matched, #clear, #stats
309
- * Implemented #delete, #exist?, #increment, #decrement
310
- * Introduced RedisStore
311
- * Initial import
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.0.0.1
@@ -1,81 +0,0 @@
1
- require "redis-store"
2
-
3
- module RedisStore
4
- module Rack
5
- module Session
6
- # Redis session storage for Rails, and for Rails only. Derived from
7
- # the MemCacheStore code, simply dropping in Redis instead.
8
- #
9
- # Options:
10
- # :key => Same as with the other cookie stores, key name
11
- # :secret => Encryption secret for the key
12
- # :host => Redis host name, default is localhost
13
- # :port => Redis port, default is 6379
14
- # :db => Database number, defaults to 0. Useful to separate your session storage from other data
15
- # :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others
16
- # :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis
17
- module Rails
18
- def initialize(app, options = {})
19
- # Support old :expires option
20
- options[:expire_after] ||= options[:expires]
21
-
22
- super
23
-
24
- options = options.dup
25
- servers = [ options.delete(:servers) ].flatten.compact
26
- servers = [ "redis://localhost:6379/0" ] if servers.empty?
27
- servers.map! do |server|
28
- server = Redis::Factory.convert_to_redis_client_options(server)
29
- server.merge(options)
30
- end
31
-
32
- @pool = Redis::Factory.create(*servers)
33
- end
34
-
35
- private
36
- def get_session(env, sid)
37
- sid ||= generate_sid
38
- begin
39
- session = @pool.get(sid) || {}
40
- rescue Errno::ECONNREFUSED
41
- session = {}
42
- end
43
- [sid, session]
44
- end
45
-
46
- def set_session(env, sid, session_data, opts=nil)
47
- options = env['rack.session.options']
48
- @pool.set(sid, session_data, options)
49
- return(::Redis::Store.rails3? ? sid : true)
50
- rescue Errno::ECONNREFUSED
51
- return false
52
- end
53
-
54
- def destroy(env)
55
- if sid = current_session_id(env)
56
- @pool.del(sid)
57
- sid
58
- end
59
- rescue Errno::ECONNREFUSED
60
- false
61
- end
62
- end
63
- end
64
- end
65
- end
66
- if ::Redis::Store.rails31?
67
- require 'action_dispatch/middleware/session/abstract_store'
68
- class ActionDispatch::Session::RedisSessionStore < Rack::Session::Redis
69
- include ActionDispatch::Session::Compatibility
70
- include ActionDispatch::Session::StaleSessionCheck
71
- end
72
- elsif ::Redis::Store.rails3?
73
- class ActionDispatch::Session::RedisSessionStore < ActionDispatch::Session::AbstractStore
74
- include RedisStore::Rack::Session::Rails
75
- end
76
- else
77
- class ActionController::Session::RedisSessionStore < ActionController::Session::AbstractStore
78
- include RedisStore::Rack::Session::Rails
79
- end
80
- end
81
-
@@ -1,254 +0,0 @@
1
- require "redis-store"
2
-
3
- module ::RedisStore
4
- module Cache
5
- module Rails2
6
- # Instantiate the store.
7
- #
8
- # Example:
9
- # RedisStore.new
10
- # # => host: localhost, port: 6379, db: 0
11
- #
12
- # RedisStore.new "example.com"
13
- # # => host: example.com, port: 6379, db: 0
14
- #
15
- # RedisStore.new "example.com:23682"
16
- # # => host: example.com, port: 23682, db: 0
17
- #
18
- # RedisStore.new "example.com:23682/1"
19
- # # => host: example.com, port: 23682, db: 1
20
- #
21
- # RedisStore.new "example.com:23682/1/theplaylist"
22
- # # => host: example.com, port: 23682, db: 1, namespace: theplaylist
23
- #
24
- # RedisStore.new "localhost:6379/0", "localhost:6380/0"
25
- # # => instantiate a cluster
26
- def initialize(*addresses)
27
- @data = ::Redis::Factory.create(addresses)
28
- end
29
-
30
- def write(key, value, options = nil)
31
- super
32
- method = options && options[:unless_exist] ? :setnx : :set
33
- @data.send method, key, value, options
34
- end
35
-
36
- def read(key, options = nil)
37
- super
38
- @data.get key, options
39
- end
40
-
41
- def delete(key, options = nil)
42
- super
43
- @data.del key
44
- end
45
-
46
- def exist?(key, options = nil)
47
- super
48
- @data.exists key
49
- end
50
-
51
- # Delete objects for matched keys.
52
- #
53
- # Example:
54
- # cache.del_matched "rab*"
55
- def delete_matched(matcher, options = nil)
56
- instrument(:delete_matched, matcher, options) do
57
- !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
58
- end
59
- end
60
-
61
- private
62
- def instrument(operation, key, options = nil)
63
- log(operation.to_s, key, options)
64
- yield
65
- end
66
- end
67
-
68
- module Rails3
69
- # Instantiate the store.
70
- #
71
- # Example:
72
- # RedisStore.new
73
- # # => host: localhost, port: 6379, db: 0
74
- #
75
- # RedisStore.new "example.com"
76
- # # => host: example.com, port: 6379, db: 0
77
- #
78
- # RedisStore.new "example.com:23682"
79
- # # => host: example.com, port: 23682, db: 0
80
- #
81
- # RedisStore.new "example.com:23682/1"
82
- # # => host: example.com, port: 23682, db: 1
83
- #
84
- # RedisStore.new "example.com:23682/1/theplaylist"
85
- # # => host: example.com, port: 23682, db: 1, namespace: theplaylist
86
- #
87
- # RedisStore.new "localhost:6379/0", "localhost:6380/0"
88
- # # => instantiate a cluster
89
- def initialize(*addresses)
90
- @data = ::Redis::Factory.create(addresses)
91
- super(addresses.extract_options!)
92
- end
93
-
94
- # Delete objects for matched keys.
95
- #
96
- # Example:
97
- # cache.del_matched "rab*"
98
- def delete_matched(matcher, options = nil)
99
- options = merged_options(options)
100
- instrument(:delete_matched, matcher.inspect) do
101
- matcher = key_matcher(matcher, options)
102
- begin
103
- !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
104
- rescue Errno::ECONNREFUSED => e
105
- false
106
- end
107
- end
108
- end
109
-
110
- protected
111
- def write_entry(key, entry, options)
112
- method = options && options[:unless_exist] ? :setnx : :set
113
- @data.send method, key, entry, options
114
- rescue Errno::ECONNREFUSED => e
115
- false
116
- end
117
-
118
- def read_entry(key, options)
119
- entry = @data.get key, options
120
- if entry
121
- entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
122
- end
123
- rescue Errno::ECONNREFUSED => e
124
- nil
125
- end
126
-
127
- ##
128
- # Implement the ActiveSupport::Cache#delete_entry
129
- #
130
- # It's really needed and use
131
- #
132
- def delete_entry(key, options)
133
- @data.del key
134
- rescue Errno::ECONNREFUSED => e
135
- false
136
- end
137
-
138
-
139
- # Add the namespace defined in the options to a pattern designed to match keys.
140
- #
141
- # This implementation is __different__ than ActiveSupport:
142
- # __it doesn't accept Regular expressions__, because the Redis matcher is designed
143
- # only for strings with wildcards.
144
- def key_matcher(pattern, options)
145
- prefix = options[:namespace].is_a?(Proc) ? options[:namespace].call : options[:namespace]
146
- if prefix
147
- raise "Regexps aren't supported, please use string with wildcards." if pattern.is_a?(Regexp)
148
- "#{prefix}:#{pattern}"
149
- else
150
- pattern
151
- end
152
- end
153
- end
154
-
155
- module Store
156
- include ::Redis::Store.rails3? ? Rails3 : Rails2
157
- end
158
- end
159
- end
160
-
161
- module ActiveSupport
162
- module Cache
163
- class RedisStore < Store
164
- include ::RedisStore::Cache::Store
165
-
166
- # Reads multiple keys from the cache using a single call to the
167
- # servers for all keys. Options can be passed in the last argument.
168
- #
169
- # Example:
170
- # cache.read_multi "rabbit", "white-rabbit"
171
- # cache.read_multi "rabbit", "white-rabbit", :raw => true
172
- def read_multi(*names)
173
- values = @data.mget(*names)
174
-
175
- # Remove the options hash before mapping keys to values
176
- names.extract_options!
177
-
178
- result = Hash[names.zip(values)]
179
- result.reject!{ |k,v| v.nil? }
180
- result
181
- end
182
-
183
- # Increment a key in the store.
184
- #
185
- # If the key doesn't exist it will be initialized on 0.
186
- # If the key exist but it isn't a Fixnum it will be initialized on 0.
187
- #
188
- # Example:
189
- # We have two objects in cache:
190
- # counter # => 23
191
- # rabbit # => #<Rabbit:0x5eee6c>
192
- #
193
- # cache.increment "counter"
194
- # cache.read "counter", :raw => true # => "24"
195
- #
196
- # cache.increment "counter", 6
197
- # cache.read "counter", :raw => true # => "30"
198
- #
199
- # cache.increment "a counter"
200
- # cache.read "a counter", :raw => true # => "1"
201
- #
202
- # cache.increment "rabbit"
203
- # cache.read "rabbit", :raw => true # => "1"
204
- def increment(key, amount = 1)
205
- instrument(:increment, key, :amount => amount) do
206
- @data.incrby key, amount
207
- end
208
- end
209
-
210
- # Decrement a key in the store
211
- #
212
- # If the key doesn't exist it will be initialized on 0.
213
- # If the key exist but it isn't a Fixnum it will be initialized on 0.
214
- #
215
- # Example:
216
- # We have two objects in cache:
217
- # counter # => 23
218
- # rabbit # => #<Rabbit:0x5eee6c>
219
- #
220
- # cache.decrement "counter"
221
- # cache.read "counter", :raw => true # => "22"
222
- #
223
- # cache.decrement "counter", 2
224
- # cache.read "counter", :raw => true # => "20"
225
- #
226
- # cache.decrement "a counter"
227
- # cache.read "a counter", :raw => true # => "-1"
228
- #
229
- # cache.decrement "rabbit"
230
- # cache.read "rabbit", :raw => true # => "-1"
231
- def decrement(key, amount = 1)
232
- instrument(:decrement, key, :amount => amount) do
233
- @data.decrby key, amount
234
- end
235
- end
236
-
237
- # Clear all the data from the store.
238
- def clear
239
- instrument(:clear, nil, nil) do
240
- @data.flushdb
241
- end
242
- end
243
-
244
- def stats
245
- @data.info
246
- end
247
-
248
- # Force client reconnection, useful Unicorn deployed apps.
249
- def reconnect
250
- @data.reconnect
251
- end
252
- end
253
- end
254
- end