diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2012-08-17 21:26:33 -0700 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2012-08-17 21:26:33 -0700 |
commit | 9f8852128f2ad968e15b53728bb5c9fb3ba52e3b (patch) | |
tree | be5eab371d7b419f689f6573fcac0586d582c2f8 /activesupport/lib | |
parent | 366eb7216edd4515d4c5491df6868dbf9fecd261 (diff) | |
parent | bd680e3f5fc863318cff3d2c109db5a6cc822579 (diff) | |
download | rails-9f8852128f2ad968e15b53728bb5c9fb3ba52e3b.tar.gz rails-9f8852128f2ad968e15b53728bb5c9fb3ba52e3b.tar.bz2 rails-9f8852128f2ad968e15b53728bb5c9fb3ba52e3b.zip |
Merge pull request #6903 from guilleiguaran/dalli-memcache-store
Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/cache/mem_cache_store.rb | 53 |
1 files changed, 21 insertions, 32 deletions
diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index 2e1ccb72d8..5aa78cc9f3 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -1,7 +1,7 @@ begin - require 'memcache' + require 'dalli' rescue LoadError => e - $stderr.puts "You don't have memcache-client installed in your application. Please add it to your Gemfile and run bundle install" + $stderr.puts "You don't have dalli installed in your application. Please add it to your Gemfile and run bundle install" raise e end @@ -22,21 +22,13 @@ module ActiveSupport # MemCacheStore implements the Strategy::LocalCache strategy which implements # an in-memory cache inside of a block. class MemCacheStore < Store - module Response # :nodoc: - STORED = "STORED\r\n" - NOT_STORED = "NOT_STORED\r\n" - EXISTS = "EXISTS\r\n" - NOT_FOUND = "NOT_FOUND\r\n" - DELETED = "DELETED\r\n" - end - ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n def self.build_mem_cache(*addresses) addresses = addresses.flatten options = addresses.extract_options! addresses = ["localhost:11211"] if addresses.empty? - MemCache.new(addresses, options) + Dalli::Client.new(addresses, options) end # Creates a new MemCacheStore object, with the given memcached server @@ -90,11 +82,11 @@ module ActiveSupport # to zero. def increment(name, amount = 1, options = nil) # :nodoc: options = merged_options(options) - response = instrument(:increment, name, :amount => amount) do + instrument(:increment, name, :amount => amount) do @data.incr(escape_key(namespaced_key(name, options)), amount) end - response == Response::NOT_FOUND ? nil : response.to_i - rescue MemCache::MemCacheError + rescue Dalli::DalliError + logger.error("DalliError (#{e}): #{e.message}") if logger nil end @@ -104,11 +96,11 @@ module ActiveSupport # to zero. def decrement(name, amount = 1, options = nil) # :nodoc: options = merged_options(options) - response = instrument(:decrement, name, :amount => amount) do + instrument(:decrement, name, :amount => amount) do @data.decr(escape_key(namespaced_key(name, options)), amount) end - response == Response::NOT_FOUND ? nil : response.to_i - rescue MemCache::MemCacheError + rescue Dalli::DalliError + logger.error("DalliError (#{e}): #{e.message}") if logger nil end @@ -116,6 +108,9 @@ module ActiveSupport # be used with care when shared cache is being used. def clear(options = nil) @data.flush_all + rescue Dalli::DalliError => e + logger.error("DalliError (#{e}): #{e.message}") if logger + nil end # Get the statistics from the memcached servers. @@ -126,9 +121,9 @@ module ActiveSupport protected # Read an entry from the cache. def read_entry(key, options) # :nodoc: - deserialize_entry(@data.get(escape_key(key), true)) - rescue MemCache::MemCacheError => e - logger.error("MemCacheError (#{e}): #{e.message}") if logger + deserialize_entry(@data.get(escape_key(key), options)) + rescue Dalli::DalliError => e + logger.error("DalliError (#{e}): #{e.message}") if logger nil end @@ -137,23 +132,17 @@ module ActiveSupport method = options && options[:unless_exist] ? :add : :set value = options[:raw] ? entry.value.to_s : entry expires_in = options[:expires_in].to_i - if expires_in > 0 && !options[:raw] - # Set the memcache expire a few minutes in the future to support race condition ttls on read - expires_in += 5.minutes - end - response = @data.send(method, escape_key(key), value, expires_in, options[:raw]) - response == Response::STORED - rescue MemCache::MemCacheError => e - logger.error("MemCacheError (#{e}): #{e.message}") if logger + @data.send(method, escape_key(key), value, expires_in, options) + rescue Dalli::DalliError => e + logger.error("DalliError (#{e}): #{e.message}") if logger false end # Delete an entry from the cache. def delete_entry(key, options) # :nodoc: - response = @data.delete(escape_key(key)) - response == Response::DELETED - rescue MemCache::MemCacheError => e - logger.error("MemCacheError (#{e}): #{e.message}") if logger + @data.delete(escape_key(key)) + rescue Dalli::DalliError => e + logger.error("DalliError (#{e}): #{e.message}") if logger false end |