Skip to content

Commit 8266330

Browse files
committed
Replace deprecated memcache-client gem with dalli in ActiveSupport::Cache::MemCacheStore
memcache-client was deprecated in favour of dalli in 2010.
1 parent 366eb72 commit 8266330

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

Diff for: Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ group :doc do
4040
end
4141

4242
# AS
43-
gem 'memcache-client', '>= 1.8.5'
43+
gem 'dalli'
4444

4545
# Add your own local bundler stuff
4646
local_gemfile = File.dirname(__FILE__) + "/.Gemfile"

Diff for: activesupport/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
4+
5+
*Guillermo Iguaran*
6+
37
* Add default values to all `ActiveSupport::NumberHelper` methods, to avoid
48
errors with empty locales or missing values.
59

Diff for: activesupport/lib/active_support/cache/mem_cache_store.rb

+21-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
begin
2-
require 'memcache'
2+
require 'dalli'
33
rescue LoadError => e
4-
$stderr.puts "You don't have memcache-client installed in your application. Please add it to your Gemfile and run bundle install"
4+
$stderr.puts "You don't have dalli installed in your application. Please add it to your Gemfile and run bundle install"
55
raise e
66
end
77

@@ -22,21 +22,13 @@ module Cache
2222
# MemCacheStore implements the Strategy::LocalCache strategy which implements
2323
# an in-memory cache inside of a block.
2424
class MemCacheStore < Store
25-
module Response # :nodoc:
26-
STORED = "STORED\r\n"
27-
NOT_STORED = "NOT_STORED\r\n"
28-
EXISTS = "EXISTS\r\n"
29-
NOT_FOUND = "NOT_FOUND\r\n"
30-
DELETED = "DELETED\r\n"
31-
end
32-
3325
ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
3426

3527
def self.build_mem_cache(*addresses)
3628
addresses = addresses.flatten
3729
options = addresses.extract_options!
3830
addresses = ["localhost:11211"] if addresses.empty?
39-
MemCache.new(addresses, options)
31+
Dalli::Client.new(addresses, options)
4032
end
4133

4234
# Creates a new MemCacheStore object, with the given memcached server
@@ -90,11 +82,11 @@ def read_multi(*names)
9082
# to zero.
9183
def increment(name, amount = 1, options = nil) # :nodoc:
9284
options = merged_options(options)
93-
response = instrument(:increment, name, :amount => amount) do
85+
instrument(:increment, name, :amount => amount) do
9486
@data.incr(escape_key(namespaced_key(name, options)), amount)
9587
end
96-
response == Response::NOT_FOUND ? nil : response.to_i
97-
rescue MemCache::MemCacheError
88+
rescue Dalli::DalliError
89+
logger.error("DalliError (#{e}): #{e.message}") if logger
9890
nil
9991
end
10092

@@ -104,18 +96,21 @@ def increment(name, amount = 1, options = nil) # :nodoc:
10496
# to zero.
10597
def decrement(name, amount = 1, options = nil) # :nodoc:
10698
options = merged_options(options)
107-
response = instrument(:decrement, name, :amount => amount) do
99+
instrument(:decrement, name, :amount => amount) do
108100
@data.decr(escape_key(namespaced_key(name, options)), amount)
109101
end
110-
response == Response::NOT_FOUND ? nil : response.to_i
111-
rescue MemCache::MemCacheError
102+
rescue Dalli::DalliError
103+
logger.error("DalliError (#{e}): #{e.message}") if logger
112104
nil
113105
end
114106

115107
# Clear the entire cache on all memcached servers. This method should
116108
# be used with care when shared cache is being used.
117109
def clear(options = nil)
118110
@data.flush_all
111+
rescue Dalli::DalliError => e
112+
logger.error("DalliError (#{e}): #{e.message}") if logger
113+
nil
119114
end
120115

121116
# Get the statistics from the memcached servers.
@@ -126,9 +121,9 @@ def stats
126121
protected
127122
# Read an entry from the cache.
128123
def read_entry(key, options) # :nodoc:
129-
deserialize_entry(@data.get(escape_key(key), true))
130-
rescue MemCache::MemCacheError => e
131-
logger.error("MemCacheError (#{e}): #{e.message}") if logger
124+
deserialize_entry(@data.get(escape_key(key), options))
125+
rescue Dalli::DalliError => e
126+
logger.error("DalliError (#{e}): #{e.message}") if logger
132127
nil
133128
end
134129

@@ -137,23 +132,17 @@ def write_entry(key, entry, options) # :nodoc:
137132
method = options && options[:unless_exist] ? :add : :set
138133
value = options[:raw] ? entry.value.to_s : entry
139134
expires_in = options[:expires_in].to_i
140-
if expires_in > 0 && !options[:raw]
141-
# Set the memcache expire a few minutes in the future to support race condition ttls on read
142-
expires_in += 5.minutes
143-
end
144-
response = @data.send(method, escape_key(key), value, expires_in, options[:raw])
145-
response == Response::STORED
146-
rescue MemCache::MemCacheError => e
147-
logger.error("MemCacheError (#{e}): #{e.message}") if logger
135+
@data.send(method, escape_key(key), value, expires_in, options)
136+
rescue Dalli::DalliError => e
137+
logger.error("DalliError (#{e}): #{e.message}") if logger
148138
false
149139
end
150140

151141
# Delete an entry from the cache.
152142
def delete_entry(key, options) # :nodoc:
153-
response = @data.delete(escape_key(key))
154-
response == Response::DELETED
155-
rescue MemCache::MemCacheError => e
156-
logger.error("MemCacheError (#{e}): #{e.message}") if logger
143+
@data.delete(escape_key(key))
144+
rescue Dalli::DalliError => e
145+
logger.error("DalliError (#{e}): #{e.message}") if logger
157146
false
158147
end
159148

Diff for: activesupport/test/abstract_unit.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
require 'active_support'
2323

2424
def uses_memcached(test_name)
25-
require 'memcache'
25+
require 'dalli'
2626
begin
27-
MemCache.new('localhost:11211').stats
27+
Dalli::Client.new('localhost:11211').stats
2828
yield
29-
rescue MemCache::MemCacheError
29+
rescue Dalli::DalliError
3030
$stderr.puts "Skipping #{test_name} tests. Start memcached and try again."
3131
end
3232
end

Diff for: activesupport/test/caching_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,34 @@ def test_file_fragment_cache_store
8383
end
8484

8585
def test_mem_cache_fragment_cache_store
86-
MemCache.expects(:new).with(%w[localhost], {})
86+
Dalli::Client.expects(:new).with(%w[localhost], {})
8787
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
8888
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
8989
end
9090

9191
def test_mem_cache_fragment_cache_store_with_given_mem_cache
92-
mem_cache = MemCache.new
93-
MemCache.expects(:new).never
92+
mem_cache = Dalli::Client.new
93+
Dalli::Client.expects(:new).never
9494
store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
9595
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
9696
end
9797

9898
def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object
99-
MemCache.expects(:new).never
99+
Dalli::Client.expects(:new).never
100100
memcache = Object.new
101101
def memcache.get() true end
102102
store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
103103
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
104104
end
105105

106106
def test_mem_cache_fragment_cache_store_with_multiple_servers
107-
MemCache.expects(:new).with(%w[localhost 192.168.1.1], {})
107+
Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], {})
108108
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
109109
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
110110
end
111111

112112
def test_mem_cache_fragment_cache_store_with_options
113-
MemCache.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 })
113+
Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 })
114114
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
115115
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
116116
assert_equal 'foo', store.options[:namespace]

0 commit comments

Comments
 (0)