提交 894336a2 编写于 作者: M Michael Grosser

use prepend instead of extending every instance

extending an instance with a module puts the methods on top of it,
prepend does the same but on the class level, so less work for us and more standard way of doing things
上级 cb67c819
......@@ -10,6 +10,7 @@ module Cache
# FileStore implements the Strategy::LocalCache strategy which implements
# an in-memory cache inside of a block.
class FileStore < Store
prepend Strategy::LocalCache
attr_reader :cache_path
DIR_FORMATTER = "%03X"
......@@ -20,7 +21,6 @@ class FileStore < Store
def initialize(cache_path, options = nil)
super(options)
@cache_path = cache_path.to_s
extend Strategy::LocalCache
end
# Deletes all items from the cache. In this case it deletes all the entries in the specified
......
......@@ -24,6 +24,31 @@ module Cache
# MemCacheStore implements the Strategy::LocalCache strategy which implements
# an in-memory cache inside of a block.
class MemCacheStore < Store
# Provide support for raw values in the local cache strategy.
module LocalCacheWithRaw # :nodoc:
protected
def read_entry(key, options)
entry = super
if options[:raw] && local_cache && entry
entry = deserialize_entry(entry.value)
end
entry
end
def write_entry(key, entry, options) # :nodoc:
retval = super
if options[:raw] && local_cache && retval
raw_entry = Entry.new(entry.value.to_s)
raw_entry.expires_at = entry.expires_at
local_cache.write_entry(key, raw_entry, options)
end
retval
end
end
prepend Strategy::LocalCache
prepend LocalCacheWithRaw
ESCAPE_KEY_CHARS = /[\x00-\x20%\x7F-\xFF]/n
# Creates a new Dalli::Client instance with specified addresses and options.
......@@ -63,9 +88,6 @@ def initialize(*addresses)
UNIVERSAL_OPTIONS.each{|name| mem_cache_options.delete(name)}
@data = self.class.build_mem_cache(*(addresses + [mem_cache_options]))
end
extend Strategy::LocalCache
extend LocalCacheWithRaw
end
# Reads multiple values from the cache using a single call to the
......@@ -181,28 +203,6 @@ def deserialize_entry(raw_value)
nil
end
end
# Provide support for raw values in the local cache strategy.
module LocalCacheWithRaw # :nodoc:
protected
def read_entry(key, options)
entry = super
if options[:raw] && local_cache && entry
entry = deserialize_entry(entry.value)
end
entry
end
def write_entry(key, entry, options) # :nodoc:
retval = super
if options[:raw] && local_cache && retval
raw_entry = Entry.new(entry.value.to_s)
raw_entry.expires_at = entry.expires_at
local_cache.write_entry(key, raw_entry, options)
end
retval
end
end
end
end
end
......@@ -8,10 +8,7 @@ module Cache
# be cached inside blocks that utilize this strategy. See
# ActiveSupport::Cache::Strategy::LocalCache for more details.
class NullStore < Store
def initialize(options = nil)
super(options)
extend Strategy::LocalCache
end
prepend Strategy::LocalCache
def clear(options = nil)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册