Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
memcache-client was deprecated in favour of dalli in 2010.
This commit is contained in:
parent
366eb7216e
commit
82663306f4
2
Gemfile
2
Gemfile
|
@ -40,7 +40,7 @@ group :doc do
|
||||||
end
|
end
|
||||||
|
|
||||||
# AS
|
# AS
|
||||||
gem 'memcache-client', '>= 1.8.5'
|
gem 'dalli'
|
||||||
|
|
||||||
# Add your own local bundler stuff
|
# Add your own local bundler stuff
|
||||||
local_gemfile = File.dirname(__FILE__) + "/.Gemfile"
|
local_gemfile = File.dirname(__FILE__) + "/.Gemfile"
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
|
||||||
|
|
||||||
|
*Guillermo Iguaran*
|
||||||
|
|
||||||
* Add default values to all `ActiveSupport::NumberHelper` methods, to avoid
|
* Add default values to all `ActiveSupport::NumberHelper` methods, to avoid
|
||||||
errors with empty locales or missing values.
|
errors with empty locales or missing values.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
begin
|
begin
|
||||||
require 'memcache'
|
require 'dalli'
|
||||||
rescue LoadError => e
|
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
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -22,21 +22,13 @@ module ActiveSupport
|
||||||
# MemCacheStore implements the Strategy::LocalCache strategy which implements
|
# MemCacheStore implements the Strategy::LocalCache strategy which implements
|
||||||
# an in-memory cache inside of a block.
|
# an in-memory cache inside of a block.
|
||||||
class MemCacheStore < Store
|
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
|
ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
|
||||||
|
|
||||||
def self.build_mem_cache(*addresses)
|
def self.build_mem_cache(*addresses)
|
||||||
addresses = addresses.flatten
|
addresses = addresses.flatten
|
||||||
options = addresses.extract_options!
|
options = addresses.extract_options!
|
||||||
addresses = ["localhost:11211"] if addresses.empty?
|
addresses = ["localhost:11211"] if addresses.empty?
|
||||||
MemCache.new(addresses, options)
|
Dalli::Client.new(addresses, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a new MemCacheStore object, with the given memcached server
|
# Creates a new MemCacheStore object, with the given memcached server
|
||||||
|
@ -90,11 +82,11 @@ module ActiveSupport
|
||||||
# to zero.
|
# to zero.
|
||||||
def increment(name, amount = 1, options = nil) # :nodoc:
|
def increment(name, amount = 1, options = nil) # :nodoc:
|
||||||
options = merged_options(options)
|
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)
|
@data.incr(escape_key(namespaced_key(name, options)), amount)
|
||||||
end
|
end
|
||||||
response == Response::NOT_FOUND ? nil : response.to_i
|
rescue Dalli::DalliError
|
||||||
rescue MemCache::MemCacheError
|
logger.error("DalliError (#{e}): #{e.message}") if logger
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -104,11 +96,11 @@ module ActiveSupport
|
||||||
# to zero.
|
# to zero.
|
||||||
def decrement(name, amount = 1, options = nil) # :nodoc:
|
def decrement(name, amount = 1, options = nil) # :nodoc:
|
||||||
options = merged_options(options)
|
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)
|
@data.decr(escape_key(namespaced_key(name, options)), amount)
|
||||||
end
|
end
|
||||||
response == Response::NOT_FOUND ? nil : response.to_i
|
rescue Dalli::DalliError
|
||||||
rescue MemCache::MemCacheError
|
logger.error("DalliError (#{e}): #{e.message}") if logger
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -116,6 +108,9 @@ module ActiveSupport
|
||||||
# be used with care when shared cache is being used.
|
# be used with care when shared cache is being used.
|
||||||
def clear(options = nil)
|
def clear(options = nil)
|
||||||
@data.flush_all
|
@data.flush_all
|
||||||
|
rescue Dalli::DalliError => e
|
||||||
|
logger.error("DalliError (#{e}): #{e.message}") if logger
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the statistics from the memcached servers.
|
# Get the statistics from the memcached servers.
|
||||||
|
@ -126,9 +121,9 @@ module ActiveSupport
|
||||||
protected
|
protected
|
||||||
# Read an entry from the cache.
|
# Read an entry from the cache.
|
||||||
def read_entry(key, options) # :nodoc:
|
def read_entry(key, options) # :nodoc:
|
||||||
deserialize_entry(@data.get(escape_key(key), true))
|
deserialize_entry(@data.get(escape_key(key), options))
|
||||||
rescue MemCache::MemCacheError => e
|
rescue Dalli::DalliError => e
|
||||||
logger.error("MemCacheError (#{e}): #{e.message}") if logger
|
logger.error("DalliError (#{e}): #{e.message}") if logger
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -137,23 +132,17 @@ module ActiveSupport
|
||||||
method = options && options[:unless_exist] ? :add : :set
|
method = options && options[:unless_exist] ? :add : :set
|
||||||
value = options[:raw] ? entry.value.to_s : entry
|
value = options[:raw] ? entry.value.to_s : entry
|
||||||
expires_in = options[:expires_in].to_i
|
expires_in = options[:expires_in].to_i
|
||||||
if expires_in > 0 && !options[:raw]
|
@data.send(method, escape_key(key), value, expires_in, options)
|
||||||
# Set the memcache expire a few minutes in the future to support race condition ttls on read
|
rescue Dalli::DalliError => e
|
||||||
expires_in += 5.minutes
|
logger.error("DalliError (#{e}): #{e.message}") if logger
|
||||||
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
|
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
# Delete an entry from the cache.
|
# Delete an entry from the cache.
|
||||||
def delete_entry(key, options) # :nodoc:
|
def delete_entry(key, options) # :nodoc:
|
||||||
response = @data.delete(escape_key(key))
|
@data.delete(escape_key(key))
|
||||||
response == Response::DELETED
|
rescue Dalli::DalliError => e
|
||||||
rescue MemCache::MemCacheError => e
|
logger.error("DalliError (#{e}): #{e.message}") if logger
|
||||||
logger.error("MemCacheError (#{e}): #{e.message}") if logger
|
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,11 +22,11 @@ ENV['NO_RELOAD'] = '1'
|
||||||
require 'active_support'
|
require 'active_support'
|
||||||
|
|
||||||
def uses_memcached(test_name)
|
def uses_memcached(test_name)
|
||||||
require 'memcache'
|
require 'dalli'
|
||||||
begin
|
begin
|
||||||
MemCache.new('localhost:11211').stats
|
Dalli::Client.new('localhost:11211').stats
|
||||||
yield
|
yield
|
||||||
rescue MemCache::MemCacheError
|
rescue Dalli::DalliError
|
||||||
$stderr.puts "Skipping #{test_name} tests. Start memcached and try again."
|
$stderr.puts "Skipping #{test_name} tests. Start memcached and try again."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -83,20 +83,20 @@ class CacheStoreSettingTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mem_cache_fragment_cache_store
|
def test_mem_cache_fragment_cache_store
|
||||||
MemCache.expects(:new).with(%w[localhost], {})
|
Dalli::Client.expects(:new).with(%w[localhost], {})
|
||||||
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
|
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
|
||||||
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mem_cache_fragment_cache_store_with_given_mem_cache
|
def test_mem_cache_fragment_cache_store_with_given_mem_cache
|
||||||
mem_cache = MemCache.new
|
mem_cache = Dalli::Client.new
|
||||||
MemCache.expects(:new).never
|
Dalli::Client.expects(:new).never
|
||||||
store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
|
store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
|
||||||
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object
|
def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object
|
||||||
MemCache.expects(:new).never
|
Dalli::Client.expects(:new).never
|
||||||
memcache = Object.new
|
memcache = Object.new
|
||||||
def memcache.get() true end
|
def memcache.get() true end
|
||||||
store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
|
store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
|
||||||
|
@ -104,13 +104,13 @@ class CacheStoreSettingTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mem_cache_fragment_cache_store_with_multiple_servers
|
def test_mem_cache_fragment_cache_store_with_multiple_servers
|
||||||
MemCache.expects(:new).with(%w[localhost 192.168.1.1], {})
|
Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], {})
|
||||||
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
|
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
|
||||||
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mem_cache_fragment_cache_store_with_options
|
def test_mem_cache_fragment_cache_store_with_options
|
||||||
MemCache.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 })
|
Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 })
|
||||||
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
|
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
|
||||||
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
|
||||||
assert_equal 'foo', store.options[:namespace]
|
assert_equal 'foo', store.options[:namespace]
|
||||||
|
|
Loading…
Reference in New Issue