lib/active_support/cache/dalli_store23.rb in dalli-0.9.3 vs lib/active_support/cache/dalli_store23.rb in dalli-0.9.4
- old
+ new
@@ -20,16 +20,15 @@
EXISTS = "EXISTS\r\n"
NOT_FOUND = "NOT_FOUND\r\n"
DELETED = "DELETED\r\n"
end
- ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/
-
def self.build_mem_cache(*addresses)
addresses = addresses.flatten
options = addresses.extract_options!
addresses = ["localhost"] if addresses.empty?
+ # No need to marshal since we marshal here.
Dalli::Client.new(addresses, options)
end
# Creates a new DalliStore object, with the given memcached server
# addresses. Each address is either a host name, or a host-with-port string
@@ -39,14 +38,18 @@
#
# If no addresses are specified, then DalliStore will connect to
# localhost port 11211 (the default memcached port).
#
def initialize(*addresses)
+ addresses = addresses.flatten
+ options = addresses.extract_options!
if addresses.first.respond_to?(:get)
@data = addresses.first
else
- @data = self.class.build_mem_cache(*addresses)
+ mem_cache_options = options.dup
+ @namespace = mem_cache_options.delete(:namespace)
+ @data = self.class.build_mem_cache(*(addresses + [mem_cache_options]))
end
extend Strategy::LocalCache
end
@@ -128,12 +131,10 @@
# - +:expires_in+ - the number of seconds that this value may stay in
# the cache. See ActiveSupport::Cache::Store#write for an example.
def write(key, value, options = nil)
super
method = options && options[:unless_exist] ? :add : :set
- # memcache-client will break the connection if you send it an integer
- # in raw mode, so we convert it to a string to be sure it continues working.
value = Marshal.dump value
@data.send(method, escape_key(key), value, expires_in(options))
rescue Dalli::DalliError => e
logger.error("DalliError (#{e}): #{e.message}")
false
@@ -160,12 +161,22 @@
super
raise "Not supported by Memcache"
end
private
+
+ # Exists in 2.3.8 but not in 2.3.2 so roll our own version
+ def expires_in(options)
+ expires_in = options && options[:expires_in]
+
+ raise ":expires_in must be a number" if expires_in && !expires_in.is_a?(Numeric)
+
+ expires_in || 0
+ end
+
def escape_key(key)
- key = key.to_s.gsub(ESCAPE_KEY_CHARS){|match| "%#{match.getbyte(0).to_s(16).upcase}"}
- key = "#{key[0, 213]}:md5:#{Digest::MD5.hexdigest(key)}" if key.size > 250
+ prefix = @namespace.is_a?(Proc) ? @namespace.call : @namespace
+ key = "#{prefix}:#{key}" if prefix
key
end
end
end
end
\ No newline at end of file