class BasicCache::Cache
Cache
object, maintains a key/value store
Attributes
store[R]
Public Class Methods
new(params = {})
click to toggle source
Generate an empty store
# File lib/basiccache/caches/cache.rb, line 17 def initialize(params = {}) params = { store: params } unless params.is_a? Hash @store = params.fetch(:store) { BasicCache::DEFAULT_STORE.new } end
Public Instance Methods
[](key = nil)
click to toggle source
Retrieve cached value
# File lib/basiccache/caches/cache.rb, line 50 def [](key = nil) key ||= BasicCache.caller_name fail KeyError, 'Key not cached' unless include? key.to_sym @store[key.to_sym] end
cache(key = nil, &code)
click to toggle source
If the key is cached, return it. If not, run the code, cache the result, and return it
# File lib/basiccache/caches/cache.rb, line 33 def cache(key = nil, &code) key ||= BasicCache.caller_name @store[key.to_sym] ||= code.call end
clear!(key = nil)
click to toggle source
Empty out either the given key or the full store
# File lib/basiccache/caches/cache.rb, line 59 def clear!(key = nil) key = key.to_sym unless key.nil? @store.clear! key end
include?(key = nil)
click to toggle source
Check if a value is cached (just a wrapper, designed to be redefined by subclasses)
# File lib/basiccache/caches/cache.rb, line 42 def include?(key = nil) key ||= BasicCache.caller_name @store.include? key.to_sym end
prune()
click to toggle source
Prunes invalid/expired keys (a noop for the basic cache)
# File lib/basiccache/caches/cache.rb, line 67 def prune [] end
size()
click to toggle source
Return the size of the cache
# File lib/basiccache/caches/cache.rb, line 25 def size @store.size end