From 7919b491b6e41e68ba760acb376e04b3b9bfc9a1 Mon Sep 17 00:00:00 2001 From: Vincent Bray Date: Tue, 13 Feb 2018 12:37:29 +0000 Subject: [PATCH] Use a hash as the backend for the mock cache store --- cache_store_base/lib/cache_store.rb | 101 ++++++++++------------------ 1 file changed, 37 insertions(+), 64 deletions(-) diff --git a/cache_store_base/lib/cache_store.rb b/cache_store_base/lib/cache_store.rb index 6fdf0eb..f6ed97b 100644 --- a/cache_store_base/lib/cache_store.rb +++ b/cache_store_base/lib/cache_store.rb @@ -8,7 +8,7 @@ def initialize(namespace = '') @namespace = namespace end - #This method is called to set a value within this cache store by it's key. + # This method is called to set a value within this cache store by its key. # # @param [String] This is the unique key to reference the value being set within this cache store. # @param [Object] This is the value to set within this cache store. @@ -17,7 +17,7 @@ def set(key, value, expires_in = 0) end - #This method is called to get a value from this cache store by it's unique key. + # This method is called to get a value from this cache store by its unique key. # # @param [String] This is the unique key to reference the value to fetch from within this cache store. # @param [Integer] This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.) @@ -26,7 +26,7 @@ def get(key, expires_in = 0, &block) end - # This method is called to remove a value from this cache store by it's unique key. + # This method is called to remove a value from this cache store by its unique key. # # @param [String] This is the unique key to reference the value to remove from this cache store. def remove(key) @@ -41,90 +41,63 @@ def exist?(key) end end -#This class is used to implement a local in memory cache store. +# This class implements a local in-memory cache store. class LocalCacheStore - - attr_accessor :store - - def initialize(namespace = nil) - @store = Array.new - @namespace = namespace + def initialize(_namespace = nil) + @store = {} end - #This method is called to set a value within this cache store by it's key. + # Store a value in this cache store by its key. # - # @param key [String] This is the unique key to reference the value being set within this cache store. - # @param value [Object] This is the value to set within this cache store. - # @param expires_in [Integer] This is the number of seconds from the current time that this value should expire. + # @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. def set(key, value, expires_in = 0) - remove(key) - expires = nil if expires_in > 0 - expires = Time.now.utc + expires_in + expires = Time.now + expires_in end - @store.push({ key: build_key(key), value: value, expires: expires}) + @store.store(key, {value: value, expires: expires}) end - #This method is called to get a value from this cache store by it's unique key. + # This method is called to get a value from this cache store by its unique key. # - # @param key [String] This is the unique key to reference the value to fetch from within this cache store. - # @param expires_in [Integer] This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.) - # @param &block [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found. + # @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. - def get(key, expires_in = 0, &block) - - #look for the cache item in the store - items = @store.select { |i| i[:key] == build_key(key) } - item = if !items.empty? then items[0] else nil end - #check if a valid item was found in the store - if item == nil || (item[:expires] != nil && item[:expires] <= Time.now.utc) - #a valid item wasn't found so check if a hydration block was specified. - if block_given? - #create the item from the block - value = yield - #put the item in the store - set(build_key(key), value, expires_in) - return value - else - #no hydration block was specified - - #check if an expired item was found - if item != nil - #remove the expired item from the store - remove(build_key(key)) + def get(key, expires_in = 0) + 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 - return nil + 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 - - #return the item - return item[:value] end - # This method is called to remove a value from this cache store by it's unique key. + # This method is called to remove a value from this cache store by its unique key. # - # @param key [String] This is the unique key to reference the value to remove from this cache store. + # @param key [String] The unique key to remove from this cache store. def remove(key) - @store.delete_if { |i| i[:key] == build_key(key) } + @store.delete key end # This method is called to check if a value exists within this cache store for a specific key. # - # @param key [String] This is the unique key to reference the value to check for within this cache store. + # @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. def exist?(key) - !@store.select { |i| i[:key] == build_key(key) }.empty? - end - - private - - def build_key(key) - k = '' - if @namespace != nil - k = @namespace + ':' + key.to_s - elsif - k = key.to_s - end - k + @store.key? key end end