Skip to content
This repository
Browse code

Merge pull request #6903 from guilleiguaran/dalli-memcache-store

Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
  • Loading branch information...
commit 9f8852128f2ad968e15b53728bb5c9fb3ba52e3b 2 parents 366eb72 + bd680e3
Santiago Pastorino spastorino authored
2  Gemfile
@@ -40,7 +40,7 @@ group :doc do
40 40 end
41 41
42 42 # AS
43   -gem 'memcache-client', '>= 1.8.5'
  43 +gem 'dalli'
44 44
45 45 # Add your own local bundler stuff
46 46 local_gemfile = File.dirname(__FILE__) + "/.Gemfile"
4 activesupport/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,9 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* Replace deprecated `memcache-client` gem with `dalli` in ActiveSupport::Cache::MemCacheStore
  4 +
  5 + *Guillermo Iguaran*
  6 +
3 7 * Add default values to all `ActiveSupport::NumberHelper` methods, to avoid
4 8 errors with empty locales or missing values.
5 9
53 activesupport/lib/active_support/cache/mem_cache_store.rb
... ... @@ -1,7 +1,7 @@
1 1 begin
2   - require 'memcache'
  2 + require 'dalli'
3 3 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"
5 5 raise e
6 6 end
7 7
@@ -22,21 +22,13 @@ module Cache
22 22 # MemCacheStore implements the Strategy::LocalCache strategy which implements
23 23 # an in-memory cache inside of a block.
24 24 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   -
33 25 ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
34 26
35 27 def self.build_mem_cache(*addresses)
36 28 addresses = addresses.flatten
37 29 options = addresses.extract_options!
38 30 addresses = ["localhost:11211"] if addresses.empty?
39   - MemCache.new(addresses, options)
  31 + Dalli::Client.new(addresses, options)
40 32 end
41 33
42 34 # Creates a new MemCacheStore object, with the given memcached server
@@ -90,11 +82,11 @@ def read_multi(*names)
90 82 # to zero.
91 83 def increment(name, amount = 1, options = nil) # :nodoc:
92 84 options = merged_options(options)
93   - response = instrument(:increment, name, :amount => amount) do
  85 + instrument(:increment, name, :amount => amount) do
94 86 @data.incr(escape_key(namespaced_key(name, options)), amount)
95 87 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
98 90 nil
99 91 end
100 92
@@ -104,11 +96,11 @@ def increment(name, amount = 1, options = nil) # :nodoc:
104 96 # to zero.
105 97 def decrement(name, amount = 1, options = nil) # :nodoc:
106 98 options = merged_options(options)
107   - response = instrument(:decrement, name, :amount => amount) do
  99 + instrument(:decrement, name, :amount => amount) do
108 100 @data.decr(escape_key(namespaced_key(name, options)), amount)
109 101 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
112 104 nil
113 105 end
114 106
@@ -116,6 +108,9 @@ def decrement(name, amount = 1, options = nil) # :nodoc:
116 108 # be used with care when shared cache is being used.
117 109 def clear(options = nil)
118 110 @data.flush_all
  111 + rescue Dalli::DalliError => e
  112 + logger.error("DalliError (#{e}): #{e.message}") if logger
  113 + nil
119 114 end
120 115
121 116 # Get the statistics from the memcached servers.
@@ -126,9 +121,9 @@ def stats
126 121 protected
127 122 # Read an entry from the cache.
128 123 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
132 127 nil
133 128 end
134 129
@@ -137,23 +132,17 @@ def write_entry(key, entry, options) # :nodoc:
137 132 method = options && options[:unless_exist] ? :add : :set
138 133 value = options[:raw] ? entry.value.to_s : entry
139 134 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
148 138 false
149 139 end
150 140
151 141 # Delete an entry from the cache.
152 142 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
157 146 false
158 147 end
159 148
6 activesupport/test/abstract_unit.rb
@@ -22,11 +22,11 @@
22 22 require 'active_support'
23 23
24 24 def uses_memcached(test_name)
25   - require 'memcache'
  25 + require 'dalli'
26 26 begin
27   - MemCache.new('localhost:11211').stats
  27 + Dalli::Client.new('localhost:11211').stats
28 28 yield
29   - rescue MemCache::MemCacheError
  29 + rescue Dalli::DalliError
30 30 $stderr.puts "Skipping #{test_name} tests. Start memcached and try again."
31 31 end
32 32 end
14 activesupport/test/caching_test.rb
@@ -83,20 +83,20 @@ def test_file_fragment_cache_store
83 83 end
84 84
85 85 def test_mem_cache_fragment_cache_store
86   - MemCache.expects(:new).with(%w[localhost], {})
  86 + Dalli::Client.expects(:new).with(%w[localhost], {})
87 87 store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
88 88 assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
89 89 end
90 90
91 91 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
94 94 store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
95 95 assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
96 96 end
97 97
98 98 def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object
99   - MemCache.expects(:new).never
  99 + Dalli::Client.expects(:new).never
100 100 memcache = Object.new
101 101 def memcache.get() true end
102 102 store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
@@ -104,13 +104,13 @@ def memcache.get() true end
104 104 end
105 105
106 106 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], {})
108 108 store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
109 109 assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
110 110 end
111 111
112 112 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 })
114 114 store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
115 115 assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
116 116 assert_equal 'foo', store.options[:namespace]
@@ -447,6 +447,7 @@ def test_increment
447 447 assert_equal 2, @cache.read('foo').to_i
448 448 assert_equal 3, @cache.increment('foo')
449 449 assert_equal 3, @cache.read('foo').to_i
  450 + assert_nil @cache.increment('bar')
450 451 end
451 452
452 453 def test_decrement
@@ -456,6 +457,7 @@ def test_decrement
456 457 assert_equal 2, @cache.read('foo').to_i
457 458 assert_equal 1, @cache.decrement('foo')
458 459 assert_equal 1, @cache.read('foo').to_i
  460 + assert_nil @cache.decrement('bar')
459 461 end
460 462 end
461 463

0 comments on commit 9f88521

Please sign in to comment.
Something went wrong with that request. Please try again.