dalli 2.0.4 → 2.0.5

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,13 @@
1
1
  Dalli Changelog
2
2
  =====================
3
3
 
4
+ 2.0.5
5
+ =======
6
+
7
+ - Create proper keys for arrays of objects passed as keys [twinturbo, #211]
8
+ - Handle long key with namespace [#212]
9
+ - Add NODELAY to TCP socket options [#206]
10
+
4
11
  2.0.4
5
12
  =======
6
13
 
@@ -46,6 +46,8 @@ module ActiveSupport
46
46
 
47
47
  def fetch(name, options=nil)
48
48
  options ||= {}
49
+ name = expanded_key name
50
+
49
51
  if block_given?
50
52
  unless options[:force]
51
53
  entry = instrument(:read, name, options) do |payload|
@@ -71,6 +73,8 @@ module ActiveSupport
71
73
 
72
74
  def read(name, options=nil)
73
75
  options ||= {}
76
+ name = expanded_key name
77
+
74
78
  instrument(:read, name, options) do |payload|
75
79
  entry = read_entry(name, options)
76
80
  payload[:hit] = !!entry if payload
@@ -80,6 +84,8 @@ module ActiveSupport
80
84
 
81
85
  def write(name, value, options=nil)
82
86
  options ||= {}
87
+ name = expanded_key name
88
+
83
89
  instrument(:write, name, options) do |payload|
84
90
  write_entry(name, value, options)
85
91
  end
@@ -87,20 +93,24 @@ module ActiveSupport
87
93
 
88
94
  def exist?(name, options=nil)
89
95
  options ||= {}
96
+ name = expanded_key name
97
+
90
98
  !read_entry(name, options).nil?
91
99
  end
92
100
 
93
101
  def delete(name, options=nil)
102
+ options ||= {}
103
+ name = expanded_key name
104
+
94
105
  delete_entry(name, options)
95
106
  end
96
107
 
97
108
  # Reads multiple keys from the cache using a single call to the
98
109
  # servers for all keys. Keys must be Strings.
99
110
  def read_multi(*names)
100
- names.extract_options!
111
+ options = names.extract_options!
101
112
  names = names.flatten
102
-
103
- mapping = names.inject({}) { |memo, name| memo[escape(name)] = name; memo }
113
+ mapping = names.inject({}) { |memo, name| memo[escape(expanded_key(name))] = name; memo }
104
114
  instrument(:read_multi, names) do
105
115
  results = @data.get_multi(mapping.keys)
106
116
  results.inject({}) do |memo, (inner, value)|
@@ -119,6 +129,7 @@ module ActiveSupport
119
129
  # memcached counters cannot hold negative values.
120
130
  def increment(name, amount = 1, options=nil)
121
131
  options ||= {}
132
+ name = expanded_key name
122
133
  initial = options.has_key?(:initial) ? options[:initial] : amount
123
134
  expires_in = options[:expires_in]
124
135
  instrument(:increment, name, :amount => amount) do
@@ -137,6 +148,7 @@ module ActiveSupport
137
148
  # memcached counters cannot hold negative values.
138
149
  def decrement(name, amount = 1, options=nil)
139
150
  options ||= {}
151
+ name = expanded_key name
140
152
  initial = options.has_key?(:initial) ? options[:initial] : 0
141
153
  expires_in = options[:expires_in]
142
154
  instrument(:decrement, name, :amount => amount) do
@@ -197,6 +209,25 @@ module ActiveSupport
197
209
  end
198
210
 
199
211
  private
212
+ # Expand key to be a consistent string value. Invoke +cache_key+ if
213
+ # object responds to +cache_key+. Otherwise, to_param method will be
214
+ # called. If the key is a Hash, then keys will be sorted alphabetically.
215
+ def expanded_key(key) # :nodoc:
216
+ return key.cache_key.to_s if key.respond_to?(:cache_key)
217
+
218
+ case key
219
+ when Array
220
+ if key.size > 1
221
+ key = key.collect{|element| expanded_key(element)}
222
+ else
223
+ key = key.first
224
+ end
225
+ when Hash
226
+ key = key.sort_by { |k,_| k.to_s }.collect{|k,v| "#{k}=#{v}"}
227
+ end
228
+
229
+ key.to_param
230
+ end
200
231
 
201
232
  def escape(key)
202
233
  key = key.to_s.dup
@@ -243,8 +243,7 @@ module Dalli
243
243
  # Chokepoint method for instrumentation
244
244
  def perform(op, key, *args)
245
245
  key = key.to_s
246
- validate_key(key)
247
- key = key_with_namespace(key)
246
+ key = validate_key(key)
248
247
  begin
249
248
  server = ring.server_for_key(key)
250
249
  server.request(op, key, *args)
@@ -257,7 +256,9 @@ module Dalli
257
256
 
258
257
  def validate_key(key)
259
258
  raise ArgumentError, "key cannot be blank" if !key || key.length == 0
259
+ key = key_with_namespace(key)
260
260
  raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
261
+ return key
261
262
  end
262
263
 
263
264
  def key_with_namespace(key)
@@ -1,3 +1,3 @@
1
1
  module Dalli
2
- VERSION = '2.0.4'
2
+ VERSION = '2.0.5'
3
3
  end
@@ -1,6 +1,12 @@
1
1
  # encoding: utf-8
2
2
  require 'helper'
3
3
 
4
+ class MockUser
5
+ def cache_key
6
+ "users/1/21348793847982314"
7
+ end
8
+ end
9
+
4
10
  describe 'ActiveSupport' do
5
11
  context 'active_support caching' do
6
12
 
@@ -50,6 +56,11 @@ describe 'ActiveSupport' do
50
56
  @dalli.write('false', false)
51
57
  dvalue = @dalli.fetch('false') { flunk }
52
58
  assert_equal false, dvalue
59
+
60
+ user = MockUser.new
61
+ @dalli.write(user.cache_key, false)
62
+ dvalue = @dalli.fetch(user) { flunk }
63
+ assert_equal false, dvalue
53
64
  end
54
65
  end
55
66
  end
@@ -118,6 +129,16 @@ describe 'ActiveSupport' do
118
129
 
119
130
  dres = @dalli.delete(y)
120
131
  assert_equal true, dres
132
+
133
+ user = MockUser.new
134
+ dres = @dalli.write(user.cache_key, "foo")
135
+ assert_equal true, dres
136
+
137
+ dres = @dalli.read(user)
138
+ assert_equal "foo", dres
139
+
140
+ dres = @dalli.delete(user)
141
+ assert_equal true, dres
121
142
  end
122
143
  end
123
144
  end
@@ -149,6 +170,13 @@ describe 'ActiveSupport' do
149
170
 
150
171
  assert_equal nil, @dalli.decrement('counterZ2', 1, :initial => nil)
151
172
  assert_equal nil, @dalli.read('counterZ2')
173
+
174
+ user = MockUser.new
175
+ assert_equal true, @dalli.write(user, 0, :raw => true)
176
+ assert_equal 1, @dalli.increment(user)
177
+ assert_equal 2, @dalli.increment(user)
178
+ assert_equal 1, @dalli.decrement(user)
179
+ assert_equal "1", @dalli.read(user, :raw => true)
152
180
  end
153
181
  end
154
182
  end
@@ -164,6 +192,10 @@ describe 'ActiveSupport' do
164
192
  assert_equal true, @dalli.exist?(:false_value)
165
193
 
166
194
  assert_equal false, @dalli.exist?(:bar)
195
+
196
+ user = MockUser.new
197
+ @dalli.write(user, 'foo')
198
+ assert_equal true, @dalli.exist?(user)
167
199
  end
168
200
  end
169
201
  end
@@ -404,6 +404,11 @@ describe 'Dalli' do
404
404
  dc2.set('namespaced', 2)
405
405
  assert_equal 1, dc.get('namespaced')
406
406
  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
407
412
  end
408
413
  end
409
414
 
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.4
4
+ version: 2.0.5
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-05 00:00:00.000000000 Z
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mini_shoulda
16
- requirement: &70263471981140 !ruby/object:Gem::Requirement
16
+ requirement: &70288172335520 !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: *70263471981140
24
+ version_requirements: *70288172335520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70263471980400 !ruby/object:Gem::Requirement
27
+ requirement: &70288172334260 !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: *70263471980400
35
+ version_requirements: *70288172334260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rails
38
- requirement: &70263471979400 !ruby/object:Gem::Requirement
38
+ requirement: &70288172333720 !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: *70263471979400
46
+ version_requirements: *70288172333720
47
47
  description: High performance memcached client for Ruby
48
48
  email: mperham@gmail.com
49
49
  executables: []