Sha256: faeb8c140739e3bc5aaafd10c14975b9c93f6b06b73e05d8d113075157aa2cce
Contents?: true
Size: 1.39 KB
Versions: 5
Compression:
Stored size: 1.39 KB
Contents
## # Define the basic cache and default store objects module BasicCache ## # Set default Store type DEFAULT_STORE = BasicCache::Store ## # Cache object, maintains a key/value store class Cache attr_reader :store ## # Generate an empty store def initialize(params = {}) params = { store: params } unless params.is_a? Hash @store = params.fetch(:store) { BasicCache::DEFAULT_STORE.new } end ## # Return the size of the cache def size @store.size end ## # If the key is cached, return it. # If not, run the code, cache the result, and return it def cache(key = nil, &code) key ||= BasicCache.caller_name @store[key.to_sym] ||= code.call end ## # Check if a value is cached # (just a wrapper, designed to be redefined by subclasses) def include?(key = nil) key ||= BasicCache.caller_name @store.include? key.to_sym end ## # Retrieve cached value def [](key = nil) key ||= BasicCache.caller_name fail KeyError, 'Key not cached' unless include? key.to_sym @store[key.to_sym] end ## # Empty out either the given key or the full store def clear!(key = nil) key = key.to_sym unless key.nil? @store.clear! key end ## # Prunes invalid/expired keys (a noop for the basic cache) def prune [] end end end
Version data entries
5 entries across 5 versions & 1 rubygems