11begin
2- require 'memcache '
2+ require 'dalli '
33rescue 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
66end
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
0 commit comments