class LocalCacheStore
Implements a local in-memory cache store.
Public Class Methods
# File lib/cache_store.rb, line 43 def initialize(_namespace = nil) @store = {} end
Public Instance Methods
Checks if a value exists within this cache store for a specific key.
@param key [String] The unique key to reference the value to check for within this cache store. @return [Boolean] True or False to specify if the key exists in the cache store.
# File lib/cache_store.rb, line 97 def exist?(key) @store.key? key end
Gets a value from this cache store by its unique key.
@param key [String] Unique key to reference the value to fetch from within this cache store. @param &block [Block] This block is provided to populate this cache store with the value for the request key when it is not found. @return [Object] The value for the specified unique key within the cache store.
# File lib/cache_store.rb, line 64 def get(key, expires_in = 0, &block) item = @store[key] if item if item[:expires] && item[:expires] < Time.now # An expired entry has been found if block_given? value = yield set(key, value, expires_in) return value else remove(key) return nil end else # An item was found which has not expired return item[:value] end elsif block_given? value = yield set(key, value, expires_in) return value end end
Removes a value from this cache store by its unique key.
@param key [String] The unique key to remove from this cache store.
# File lib/cache_store.rb, line 89 def remove(key) @store.delete key end
Stores a value in this cache store by its key.
@param key [String] The unique key to reference the value being set. @param value [Object] The value to store. @param expires_in [Integer] The number of seconds from the current time that this value should expire.
# File lib/cache_store.rb, line 52 def set(key, value, expires_in = 0) if expires_in > 0 expires = Time.now + expires_in end @store.store(key, {value: value, expires: expires}) end