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,18 +96,21 @@ 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
115
107
# Clear the entire cache on all memcached servers. This method should
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
0 commit comments