2011-11-19 21:53:18 -05:00
|
|
|
module Middleman
|
2011-11-26 19:23:38 -05:00
|
|
|
# Simple shared cache implementation
|
2011-11-19 21:53:18 -05:00
|
|
|
class Cache
|
2011-11-26 19:23:38 -05:00
|
|
|
|
|
|
|
# Initialize
|
2011-11-19 21:53:18 -05:00
|
|
|
def initialize
|
2011-11-26 19:23:38 -05:00
|
|
|
self.clear
|
2011-11-19 21:53:18 -05:00
|
|
|
end
|
|
|
|
|
2011-11-26 19:23:38 -05:00
|
|
|
# Either get the cached key or save the contents of the block
|
|
|
|
#
|
|
|
|
# @param Anything Hash can use as a key
|
|
|
|
# @return Cached value
|
2011-11-19 21:53:18 -05:00
|
|
|
def fetch(*key)
|
|
|
|
@cache[key] ||= yield
|
|
|
|
end
|
2011-11-25 00:52:23 -05:00
|
|
|
|
2011-11-26 19:23:38 -05:00
|
|
|
# Whether the key is in the cache
|
|
|
|
#
|
|
|
|
# @param Anything Hash can use as a key
|
|
|
|
# @return [Boolean]
|
2011-11-25 00:52:23 -05:00
|
|
|
def has_key?(key)
|
|
|
|
@cache.has_key?(key)
|
|
|
|
end
|
|
|
|
|
2011-11-26 19:23:38 -05:00
|
|
|
# Get a specific key
|
|
|
|
#
|
|
|
|
# @param Anything Hash can use as a key
|
|
|
|
# @return Cached value
|
2011-11-25 00:52:23 -05:00
|
|
|
def get(key)
|
|
|
|
@cache[key]
|
|
|
|
end
|
2011-11-26 19:23:38 -05:00
|
|
|
|
|
|
|
# Clear the entire cache
|
2011-11-19 21:53:18 -05:00
|
|
|
def clear
|
|
|
|
@cache = {}
|
|
|
|
end
|
|
|
|
|
2011-11-26 19:23:38 -05:00
|
|
|
# Set a specific key
|
|
|
|
#
|
|
|
|
# @param Anything Hash can use as a key
|
|
|
|
# @param Cached value
|
2011-11-19 21:53:18 -05:00
|
|
|
def set(key, value)
|
|
|
|
@cache[key] = value
|
|
|
|
end
|
|
|
|
|
2011-11-26 19:23:38 -05:00
|
|
|
# Remove a specific key
|
|
|
|
# @param Anything Hash can use as a key
|
2011-11-19 21:53:18 -05:00
|
|
|
def remove(*key)
|
2011-11-26 21:12:15 -05:00
|
|
|
@cache.delete(key)
|
2011-11-19 21:53:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|