dalli 2.7.0 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

data/Rakefile DELETED
@@ -1,42 +0,0 @@
1
- require 'appraisal'
2
- require 'rake/testtask'
3
- Rake::TestTask.new(:test) do |test|
4
- test.libs << 'test'
5
- test.pattern = 'test/**/test_*.rb'
6
- test.warning = true
7
- test.verbose = true
8
- end
9
-
10
- Rake::TestTask.new(:bench) do |test|
11
- test.libs << 'test'
12
- test.pattern = 'test/benchmark_test.rb'
13
- end
14
-
15
- begin
16
- require 'metric_fu'
17
- MetricFu::Configuration.run do |config|
18
- config.rcov[:rcov_opts] << "-Itest:lib"
19
- end
20
- rescue LoadError
21
- end
22
-
23
- task :default => :test
24
-
25
- task :test_all do
26
- system('rake test RAILS_VERSION="~> 3.0.0"')
27
- system('rake test RAILS_VERSION=">= 3.0.0"')
28
- end
29
-
30
- # 'gem install rdoc' to upgrade RDoc if this is giving you errors
31
- begin
32
- require 'rdoc/task'
33
- RDoc::Task.new do |rd|
34
- rd.rdoc_files.include("lib/**/*.rb")
35
- end
36
- rescue LoadError
37
- puts "Unable to load rdoc, run 'gem install rdoc' to fix this."
38
- end
39
-
40
- require 'rake/clean'
41
- CLEAN.include "**/*.rbc"
42
- CLEAN.include "**/.DS_Store"
data/dalli.gemspec DELETED
@@ -1,29 +0,0 @@
1
- require './lib/dalli/version'
2
-
3
- Gem::Specification.new do |s|
4
- s.name = %q{dalli}
5
- s.version = Dalli::VERSION
6
- s.license = "MIT"
7
-
8
- s.authors = ["Mike Perham"]
9
- s.description = %q{High performance memcached client for Ruby}
10
- s.email = %q{mperham@gmail.com}
11
- s.files = Dir.glob("lib/**/*") + [
12
- "LICENSE",
13
- "README.md",
14
- "History.md",
15
- "Rakefile",
16
- "Gemfile",
17
- "dalli.gemspec",
18
- "Performance.md",
19
- ]
20
- s.homepage = %q{http://github.com/mperham/dalli}
21
- s.rdoc_options = ["--charset=UTF-8"]
22
- s.require_paths = ["lib"]
23
- s.summary = %q{High performance memcached client for Ruby}
24
- s.test_files = Dir.glob("test/**/*")
25
- s.add_development_dependency(%q<minitest>, [">= 4.2.0"])
26
- s.add_development_dependency(%q<mocha>, [">= 0"])
27
- s.add_development_dependency(%q<rails>, ["~> 3"])
28
- end
29
-
@@ -1,81 +0,0 @@
1
- require 'active_support/cache'
2
- require 'action_dispatch/middleware/session/abstract_store'
3
- require 'dalli'
4
-
5
- # Dalli-based session store for Rails 3.0.
6
- module ActionDispatch
7
- module Session
8
- class DalliStore < AbstractStore
9
- def initialize(app, options = {})
10
- # Support old :expires option
11
- options[:expire_after] ||= options[:expires]
12
-
13
- super
14
-
15
- @default_options = { :namespace => 'rack:session' }.merge(@default_options)
16
-
17
- @pool = options[:cache] || begin
18
- Dalli::Client.new(
19
- @default_options[:memcache_server], @default_options)
20
- end
21
- @namespace = @default_options[:namespace]
22
-
23
- @raise_errors = !!@default_options[:raise_errors]
24
-
25
- super
26
- end
27
-
28
- def reset
29
- @pool.reset
30
- end
31
-
32
- private
33
-
34
- def get_session(env, sid)
35
- sid ||= generate_sid
36
- begin
37
- session = @pool.get(sid) || {}
38
- rescue Dalli::DalliError => ex
39
- # re-raise ArgumentError so Rails' session abstract_store.rb can autoload any missing models
40
- raise ArgumentError, ex.message if ex.message =~ /unmarshal/
41
- Rails.logger.warn("Session::DalliStore#get: #{ex.message}")
42
- session = {}
43
- end
44
- [sid, session]
45
- end
46
-
47
- def set_session(env, sid, session_data, options = nil)
48
- options ||= env[ENV_SESSION_OPTIONS_KEY]
49
- expiry = options[:expire_after]
50
- @pool.set(sid, session_data, expiry)
51
- sid
52
- rescue Dalli::DalliError
53
- Rails.logger.warn("Session::DalliStore#set: #{$!.message}")
54
- raise if @raise_errors
55
- false
56
- end
57
-
58
- def destroy_session(env, session_id, options)
59
- begin
60
- @pool.delete(session_id)
61
- rescue Dalli::DalliError
62
- Rails.logger.warn("Session::DalliStore#destroy_session: #{$!.message}")
63
- raise if @raise_errors
64
- end
65
- return nil if options[:drop]
66
- generate_sid
67
- end
68
-
69
- def destroy(env)
70
- if sid = current_session_id(env)
71
- @pool.delete(sid)
72
- end
73
- rescue Dalli::DalliError
74
- Rails.logger.warn("Session::DalliStore#destroy: #{$!.message}")
75
- raise if @raise_errors
76
- false
77
- end
78
-
79
- end
80
- end
81
- end
@@ -1,361 +0,0 @@
1
- # encoding: ascii
2
- require 'dalli'
3
-
4
- module ActiveSupport
5
- module Cache
6
- class DalliStore
7
-
8
- attr_reader :silence, :options
9
- alias_method :silence?, :silence
10
-
11
- # Silence the logger.
12
- def silence!
13
- @silence = true
14
- self
15
- end
16
-
17
- # Silence the logger within a block.
18
- def mute
19
- previous_silence, @silence = defined?(@silence) && @silence, true
20
- yield
21
- ensure
22
- @silence = previous_silence
23
- end
24
-
25
- ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/
26
-
27
- # Creates a new DalliStore object, with the given memcached server
28
- # addresses. Each address is either a host name, or a host-with-port string
29
- # in the form of "host_name:port". For example:
30
- #
31
- # ActiveSupport::Cache::DalliStore.new("localhost", "server-downstairs.localnetwork:8229")
32
- #
33
- # If no addresses are specified, then DalliStore will connect to
34
- # localhost port 11211 (the default memcached port).
35
- #
36
- # Connection Pool support
37
- #
38
- # If you are using multithreaded Rails, the Rails.cache singleton can become a source
39
- # of contention. You can use a connection pool of Dalli clients with Rails.cache by
40
- # passing :pool_size and/or :pool_timeout:
41
- #
42
- # config.cache_store = :dalli_store, 'localhost:11211', :pool_size => 10
43
- #
44
- # Both pool options default to 5. You must include the `connection_pool` gem if you
45
- # wish to use pool support.
46
- #
47
- def initialize(*addresses)
48
- addresses = addresses.flatten
49
- options = addresses.extract_options!
50
- @options = options.dup
51
-
52
- pool_options = {}
53
- pool_options[:size] = options[:pool_size] if options[:pool_size]
54
- pool_options[:timeout] = options[:pool_timeout] if options[:pool_timeout]
55
-
56
- @options[:compress] ||= @options[:compression]
57
- servers = if addresses.empty?
58
- nil # use the default from Dalli::Client
59
- else
60
- addresses
61
- end
62
- if pool_options.empty?
63
- @data = Dalli::Client.new(servers, @options)
64
- else
65
- @data = ::ConnectionPool.new(pool_options) { Dalli::Client.new(servers, @options.merge(:threadsafe => false)) }
66
- end
67
-
68
- extend Strategy::LocalCache
69
- end
70
-
71
- ##
72
- # Access the underlying Dalli::Client or ConnectionPool instance for
73
- # access to get_multi, etc.
74
- def dalli
75
- @data
76
- end
77
-
78
- def with(&block)
79
- @data.with(&block)
80
- end
81
-
82
- def fetch(name, options=nil)
83
- options ||= {}
84
- name = expanded_key name
85
-
86
- if block_given?
87
- unless options[:force]
88
- entry = instrument(:read, name, options) do |payload|
89
- read_entry(name, options).tap do |result|
90
- if payload
91
- payload[:super_operation] = :fetch
92
- payload[:hit] = !!result
93
- end
94
- end
95
- end
96
- end
97
-
98
- if !entry.nil?
99
- instrument(:fetch_hit, name, options) { |payload| }
100
- entry
101
- else
102
- result = instrument(:generate, name, options) do |payload|
103
- yield
104
- end
105
- write(name, result, options)
106
- result
107
- end
108
- else
109
- read(name, options)
110
- end
111
- end
112
-
113
- def read(name, options=nil)
114
- options ||= {}
115
- name = expanded_key name
116
-
117
- instrument(:read, name, options) do |payload|
118
- entry = read_entry(name, options)
119
- payload[:hit] = !!entry if payload
120
- entry
121
- end
122
- end
123
-
124
- def write(name, value, options=nil)
125
- options ||= {}
126
- name = expanded_key name
127
-
128
- instrument(:write, name, options) do |payload|
129
- with do |connection|
130
- options = options.merge(:connection => connection)
131
- write_entry(name, value, options)
132
- end
133
- end
134
- end
135
-
136
- def exist?(name, options=nil)
137
- options ||= {}
138
- name = expanded_key name
139
-
140
- log(:exist, name, options)
141
- !read_entry(name, options).nil?
142
- end
143
-
144
- def delete(name, options=nil)
145
- options ||= {}
146
- name = expanded_key name
147
-
148
- instrument(:delete, name, options) do |payload|
149
- delete_entry(name, options)
150
- end
151
- end
152
-
153
- # Reads multiple keys from the cache using a single call to the
154
- # servers for all keys. Keys must be Strings.
155
- def read_multi(*names)
156
- names.extract_options!
157
- mapping = names.inject({}) { |memo, name| memo[expanded_key(name)] = name; memo }
158
- instrument(:read_multi, names) do
159
- results = {}
160
- if local_cache
161
- mapping.keys.each do |key|
162
- if value = local_cache.read_entry(key, options)
163
- results[key] = value
164
- end
165
- end
166
- end
167
-
168
- data = with { |c| c.get_multi(mapping.keys - results.keys) }
169
- results.merge!(data)
170
- results.inject({}) do |memo, (inner, _)|
171
- entry = results[inner]
172
- # NB Backwards data compatibility, to be removed at some point
173
- value = (entry.is_a?(ActiveSupport::Cache::Entry) ? entry.value : entry)
174
- memo[mapping[inner]] = value
175
- local_cache.write_entry(inner, value, options) if local_cache
176
- memo
177
- end
178
- end
179
- end
180
-
181
- # Fetches data from the cache, using the given keys. If there is data in
182
- # the cache with the given keys, then that data is returned. Otherwise,
183
- # the supplied block is called for each key for which there was no data,
184
- # and the result will be written to the cache and returned.
185
- def fetch_multi(*names)
186
- options = names.extract_options!
187
- mapping = names.inject({}) { |memo, name| memo[expanded_key(name)] = name; memo }
188
-
189
- instrument(:fetch_multi, names) do
190
- with do |connection|
191
- results = connection.get_multi(mapping.keys)
192
-
193
- connection.multi do
194
- mapping.inject({}) do |memo, (expanded, name)|
195
- memo[name] = results[expanded]
196
- if memo[name].nil?
197
- value = yield(name)
198
- memo[name] = value
199
- options = options.merge(:connection => connection)
200
- write_entry(expanded, value, options)
201
- end
202
-
203
- memo
204
- end
205
- end
206
- end
207
- end
208
- end
209
-
210
- # Increment a cached value. This method uses the memcached incr atomic
211
- # operator and can only be used on values written with the :raw option.
212
- # Calling it on a value not stored with :raw will fail.
213
- # :initial defaults to the amount passed in, as if the counter was initially zero.
214
- # memcached counters cannot hold negative values.
215
- def increment(name, amount = 1, options=nil)
216
- options ||= {}
217
- name = expanded_key name
218
- initial = options.has_key?(:initial) ? options[:initial] : amount
219
- expires_in = options[:expires_in]
220
- instrument(:increment, name, :amount => amount) do
221
- with { |c| c.incr(name, amount, expires_in, initial) }
222
- end
223
- rescue Dalli::DalliError => e
224
- logger.error("DalliError: #{e.message}") if logger
225
- raise if raise_errors?
226
- nil
227
- end
228
-
229
- # Decrement a cached value. This method uses the memcached decr atomic
230
- # operator and can only be used on values written with the :raw option.
231
- # Calling it on a value not stored with :raw will fail.
232
- # :initial defaults to zero, as if the counter was initially zero.
233
- # memcached counters cannot hold negative values.
234
- def decrement(name, amount = 1, options=nil)
235
- options ||= {}
236
- name = expanded_key name
237
- initial = options.has_key?(:initial) ? options[:initial] : 0
238
- expires_in = options[:expires_in]
239
- instrument(:decrement, name, :amount => amount) do
240
- with { |c| c.decr(name, amount, expires_in, initial) }
241
- end
242
- rescue Dalli::DalliError => e
243
- logger.error("DalliError: #{e.message}") if logger
244
- raise if raise_errors?
245
- nil
246
- end
247
-
248
- # Clear the entire cache on all memcached servers. This method should
249
- # be used with care when using a shared cache.
250
- def clear(options=nil)
251
- instrument(:clear, 'flushing all keys') do
252
- with { |c| c.flush_all }
253
- end
254
- rescue Dalli::DalliError => e
255
- logger.error("DalliError: #{e.message}") if logger
256
- raise if raise_errors?
257
- nil
258
- end
259
-
260
- # Clear any local cache
261
- def cleanup(options=nil)
262
- end
263
-
264
- # Get the statistics from the memcached servers.
265
- def stats
266
- with { |c| c.stats }
267
- end
268
-
269
- def reset
270
- with { |c| c.reset }
271
- end
272
-
273
- def logger
274
- Dalli.logger
275
- end
276
-
277
- def logger=(new_logger)
278
- Dalli.logger = new_logger
279
- end
280
-
281
- protected
282
-
283
- # Read an entry from the cache.
284
- def read_entry(key, options) # :nodoc:
285
- entry = with { |c| c.get(key, options) }
286
- # NB Backwards data compatibility, to be removed at some point
287
- entry.is_a?(ActiveSupport::Cache::Entry) ? entry.value : entry
288
- rescue Dalli::DalliError => e
289
- logger.error("DalliError: #{e.message}") if logger
290
- raise if raise_errors?
291
- nil
292
- end
293
-
294
- # Write an entry to the cache.
295
- def write_entry(key, value, options) # :nodoc:
296
- # cleanup LocalCache
297
- cleanup if options[:unless_exist]
298
- method = options[:unless_exist] ? :add : :set
299
- expires_in = options[:expires_in]
300
- connection = options.delete(:connection)
301
- connection.send(method, key, value, expires_in, options)
302
- rescue Dalli::DalliError => e
303
- logger.error("DalliError: #{e.message}") if logger
304
- raise if raise_errors?
305
- false
306
- end
307
-
308
- # Delete an entry from the cache.
309
- def delete_entry(key, options) # :nodoc:
310
- with { |c| c.delete(key) }
311
- rescue Dalli::DalliError => e
312
- logger.error("DalliError: #{e.message}") if logger
313
- raise if raise_errors?
314
- false
315
- end
316
-
317
- private
318
- # Expand key to be a consistent string value. Invoke +cache_key+ if
319
- # object responds to +cache_key+. Otherwise, to_param method will be
320
- # called. If the key is a Hash, then keys will be sorted alphabetically.
321
- def expanded_key(key) # :nodoc:
322
- return key.cache_key.to_s if key.respond_to?(:cache_key)
323
-
324
- case key
325
- when Array
326
- if key.size > 1
327
- key = key.collect{|element| expanded_key(element)}
328
- else
329
- key = key.first
330
- end
331
- when Hash
332
- key = key.sort_by { |k,_| k.to_s }.collect{|k,v| "#{k}=#{v}"}
333
- end
334
-
335
- key = key.to_param
336
- if key.respond_to? :force_encoding
337
- key = key.dup
338
- key.force_encoding('binary')
339
- end
340
- key
341
- end
342
-
343
- def instrument(operation, key, options=nil)
344
- log(operation, key, options)
345
-
346
- payload = { :key => key }
347
- payload.merge!(options) if options.is_a?(Hash)
348
- ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield(payload) }
349
- end
350
-
351
- def log(operation, key, options=nil)
352
- return unless logger && logger.debug? && !silence?
353
- logger.debug("Cache #{operation}: #{key}#{options.blank? ? "" : " (#{options.inspect})"}")
354
- end
355
-
356
- def raise_errors?
357
- !!@options[:raise_errors]
358
- end
359
- end
360
- end
361
- end
data/lib/dalli/railtie.rb DELETED
@@ -1,7 +0,0 @@
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