1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/cache.rb

53 lines
1 KiB
Ruby
Raw Normal View History

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-26 19:23:38 -05:00
# Whether the key is in the cache
#
# @param Anything Hash can use as a key
# @return [Boolean]
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
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)
@cache.delete(key)
2011-11-19 21:53:18 -05:00
end
end
end