dalli 2.0.5 → 2.1.0

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

Potentially problematic release.


This version of dalli might be problematic. Click here for more details.

data/History.md CHANGED
@@ -1,6 +1,11 @@
1
1
  Dalli Changelog
2
2
  =====================
3
3
 
4
+ 2.1.0
5
+ =======
6
+
7
+ - Add Railtie to auto-configure Dalli when included in Gemfile [#217, steveklabnik]
8
+
4
9
  2.0.5
5
10
  =======
6
11
 
data/README.md CHANGED
@@ -81,14 +81,11 @@ Here's a more comprehensive example that sets a reasonable default for maximum c
81
81
  config.cache_store = :dalli_store, 'cache-1.example.com', 'cache-2.example.com',
82
82
  { :namespace => NAME_OF_RAILS_APP, :expires_in => 1.day, :compress => true }
83
83
 
84
- To use Dalli for Rails session storage, in `config/initializers/session_store.rb`:
84
+ To use Dalli for Rails session storage that times out after 20 minutes, in `config/initializers/session_store.rb`:
85
85
 
86
- require 'action_dispatch/middleware/session/dalli_store'
87
- Rails.application.config.session_store :dalli_store, :memcache_server => ['host1', 'host2'], :namespace => 'sessions', :key => '_foundation_session', :expire_after => 30.minutes
86
+ Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 20.minutes
88
87
 
89
- Both cache and session stores support `:raise_errors` parameter, which propagates exceptions (e.g. if all memcache servers are down) instead of silently hiding errors.
90
-
91
- Dalli does not support Rails 2.x any longer.
88
+ Dalli does not support Rails 2.x.
92
89
 
93
90
 
94
91
  Configuration
@@ -1,6 +1,5 @@
1
1
  # encoding: ascii
2
2
  require 'dalli'
3
- require 'digest/md5'
4
3
 
5
4
  module ActiveSupport
6
5
  module Cache
@@ -164,6 +163,10 @@ module ActiveSupport
164
163
  # be used with care when using a shared cache.
165
164
  def clear(options=nil)
166
165
  @data.flush_all
166
+ rescue Dalli::DalliError => e
167
+ logger.error("DalliError: #{e.message}") if logger
168
+ raise if @raise_errors
169
+ nil
167
170
  end
168
171
 
169
172
  # Get the statistics from the memcached servers.
@@ -233,7 +236,6 @@ module ActiveSupport
233
236
  key = key.to_s.dup
234
237
  key = key.force_encoding("BINARY") if key.encoding_aware?
235
238
  key = key.gsub(ESCAPE_KEY_CHARS){ |match| "%#{match.getbyte(0).to_s(16).upcase}" }
236
- key = "#{key[0, 213]}:md5:#{Digest::MD5.hexdigest(key)}" if key.size > 250
237
239
  key
238
240
  end
239
241
 
@@ -4,6 +4,7 @@ require 'dalli/server'
4
4
  require 'dalli/socket'
5
5
  require 'dalli/version'
6
6
  require 'dalli/options'
7
+ require 'dalli/railtie' if defined?(Rails)
7
8
 
8
9
  module Dalli
9
10
  # generic error
@@ -1,3 +1,5 @@
1
+ require 'digest/md5'
2
+
1
3
  # encoding: ascii
2
4
  module Dalli
3
5
  class Client
@@ -83,7 +85,7 @@ module Dalli
83
85
  end
84
86
 
85
87
  def fetch(key, ttl=nil, options=nil)
86
- ttl ||= @options[:expires_in]
88
+ ttl ||= @options[:expires_in].to_i
87
89
  val = get(key, options)
88
90
  if val.nil? && block_given?
89
91
  val = yield
@@ -104,7 +106,7 @@ module Dalli
104
106
  # - false if the value was changed by someone else.
105
107
  # - true if the value was successfully updated.
106
108
  def cas(key, ttl=nil, options=nil, &block)
107
- ttl ||= @options[:expires_in]
109
+ ttl ||= @options[:expires_in].to_i
108
110
  (value, cas) = perform(:cas, key)
109
111
  value = (!value || value == 'Not found') ? nil : value
110
112
  if value
@@ -114,7 +116,7 @@ module Dalli
114
116
  end
115
117
 
116
118
  def set(key, value, ttl=nil, options=nil)
117
- ttl ||= @options[:expires_in]
119
+ ttl ||= @options[:expires_in].to_i
118
120
  perform(:set, key, value, ttl, 0, options)
119
121
  end
120
122
 
@@ -122,7 +124,7 @@ module Dalli
122
124
  # Conditionally add a key/value pair, if the key does not already exist
123
125
  # on the server. Returns true if the operation succeeded.
124
126
  def add(key, value, ttl=nil, options=nil)
125
- ttl ||= @options[:expires_in]
127
+ ttl ||= @options[:expires_in].to_i
126
128
  perform(:add, key, value, ttl, options)
127
129
  end
128
130
 
@@ -130,7 +132,7 @@ module Dalli
130
132
  # Conditionally add a key/value pair, only if the key already exists
131
133
  # on the server. Returns true if the operation succeeded.
132
134
  def replace(key, value, ttl=nil, options=nil)
133
- ttl ||= @options[:expires_in]
135
+ ttl ||= @options[:expires_in].to_i
134
136
  perform(:replace, key, value, ttl, options)
135
137
  end
136
138
 
@@ -172,7 +174,7 @@ module Dalli
172
174
  # #cas.
173
175
  def incr(key, amt=1, ttl=nil, default=nil)
174
176
  raise ArgumentError, "Positive values only: #{amt}" if amt < 0
175
- ttl ||= @options[:expires_in]
177
+ ttl ||= @options[:expires_in].to_i
176
178
  perform(:incr, key, amt.to_i, ttl, default)
177
179
  end
178
180
 
@@ -192,7 +194,7 @@ module Dalli
192
194
  # #cas.
193
195
  def decr(key, amt=1, ttl=nil, default=nil)
194
196
  raise ArgumentError, "Positive values only: #{amt}" if amt < 0
195
- ttl ||= @options[:expires_in]
197
+ ttl ||= @options[:expires_in].to_i
196
198
  perform(:decr, key, amt.to_i, ttl, default)
197
199
  end
198
200
 
@@ -257,7 +259,11 @@ module Dalli
257
259
  def validate_key(key)
258
260
  raise ArgumentError, "key cannot be blank" if !key || key.length == 0
259
261
  key = key_with_namespace(key)
260
- raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
262
+ if key.length > 250
263
+ namespace_length = @options[:namespace] ? @options[:namespace].size : 0
264
+ max_length_before_namespace = 212 - namespace_length
265
+ key = "#{key[0, max_length_before_namespace]}:md5:#{Digest::MD5.hexdigest(key)}"
266
+ end
261
267
  return key
262
268
  end
263
269
 
@@ -274,7 +280,11 @@ module Dalli
274
280
  Dalli.logger.warn "DEPRECATED: Dalli's :compression option is now just :compress => true. Please update your configuration."
275
281
  opts[:compress] = opts.delete(:compression)
276
282
  end
277
- opts[:expires_in] ||= 0
283
+ begin
284
+ opts[:expires_in] = opts[:expires_in].to_i if opts[:expires_in]
285
+ rescue NoMethodError
286
+ raise ArgumentError, "cannot convert :expires_in => #{opts[:expires_in].inspect} to an integer"
287
+ end
278
288
  opts
279
289
  end
280
290
  end
@@ -0,0 +1,7 @@
1
+ module Dalli
2
+ class Railtie < ::Rails::Railtie
3
+ config.before_configuration do
4
+ config.cache_store = :dalli_store
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Dalli
2
- VERSION = '2.0.5'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -14,6 +14,15 @@ describe 'Dalli' do
14
14
  # Rails.logger.expects :warn
15
15
  assert dc.instance_variable_get(:@options)[:compress]
16
16
  end
17
+
18
+ should 'raises error with invalid expires_in' do
19
+ bad_data = [{:bad => 'expires in data'}, Hash, [1,2,3]]
20
+ bad_data.each do |bad|
21
+ assert_raises ArgumentError do
22
+ dc = Dalli::Client.new('foo', {:expires_in => bad})
23
+ end
24
+ end
25
+ end
17
26
  end
18
27
 
19
28
  describe 'key validation' do
@@ -404,11 +413,16 @@ describe 'Dalli' do
404
413
  dc2.set('namespaced', 2)
405
414
  assert_equal 1, dc.get('namespaced')
406
415
  assert_equal 2, dc2.get('namespaced')
407
-
408
- dc3 = Dalli::Client.new('localhost:19122', :namespace => 'c' * 100)
409
- assert_raises ArgumentError do
410
- dc3.get "a" * 151
411
- end
416
+ end
417
+ end
418
+
419
+ should 'truncate cache keys that are too long' do
420
+ memcached do
421
+ @dalli = Dalli::Client.new('localhost:19122', :namespace => 'some:namspace')
422
+ key = "this cache key is far too long so it must be hashed and truncated and stuff" * 10
423
+ value = "some value"
424
+ assert_equal true, @dalli.set(key, value)
425
+ assert_equal value, @dalli.get(key)
412
426
  end
413
427
  end
414
428
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dalli
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-10 00:00:00.000000000 Z
12
+ date: 2012-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mini_shoulda
16
- requirement: &70288172335520 !ruby/object:Gem::Requirement
16
+ requirement: &70203098892260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70288172335520
24
+ version_requirements: *70203098892260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70288172334260 !ruby/object:Gem::Requirement
27
+ requirement: &70203098891120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70288172334260
35
+ version_requirements: *70203098891120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rails
38
- requirement: &70288172333720 !ruby/object:Gem::Requirement
38
+ requirement: &70203098890540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70288172333720
46
+ version_requirements: *70203098890540
47
47
  description: High performance memcached client for Ruby
48
48
  email: mperham@gmail.com
49
49
  executables: []
@@ -54,6 +54,7 @@ files:
54
54
  - lib/active_support/cache/dalli_store.rb
55
55
  - lib/dalli/client.rb
56
56
  - lib/dalli/options.rb
57
+ - lib/dalli/railtie.rb
57
58
  - lib/dalli/ring.rb
58
59
  - lib/dalli/server.rb
59
60
  - lib/dalli/socket.rb