Class: LocalCacheStore
- Inherits:
-
Object
- Object
- LocalCacheStore
- Defined in:
- lib/cache_store.rb
Overview
This class is used to implement a local in memory cache store.
Instance Attribute Summary collapse
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
-
#exist?(key) ⇒ Boolean
This method is called to check if a value exists within this cache store for a specific key.
-
#get(key, expires_in = 0, &block) ⇒ Object
This method is called to get a value from this cache store by it’s unique key.
-
#initialize(namespace = '') ⇒ LocalCacheStore
constructor
A new instance of LocalCacheStore.
-
#remove(key) ⇒ Object
This method is called to remove a value from this cache store by it’s unique key.
-
#set(key, value, expires_in = 0) ⇒ Object
This method is called to set a value within this cache store by it’s key.
Constructor Details
#initialize(namespace = '') ⇒ LocalCacheStore
Returns a new instance of LocalCacheStore.
48 49 50 51 |
# File 'lib/cache_store.rb', line 48 def initialize(namespace = '') @store = Array.new @namespace = namespace end |
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
46 47 48 |
# File 'lib/cache_store.rb', line 46 def store @store end |
Instance Method Details
#exist?(key) ⇒ Boolean
This method is called to check if a value exists within this cache store for a specific key.
115 116 117 |
# File 'lib/cache_store.rb', line 115 def exist?(key) !@store.select { |i| i[:key] == build_key(key) }.empty? end |
#get(key, expires_in = 0, &block) ⇒ Object
This method is called to get a value from this cache store by it’s unique key.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cache_store.rb', line 74 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] <= DateTime.now) #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)) end return nil end end #return the item return item[:value] end |
#remove(key) ⇒ Object
This method is called to remove a value from this cache store by it’s unique key.
107 108 109 |
# File 'lib/cache_store.rb', line 107 def remove(key) @store.delete_if { |i| i[:key] == build_key(key) } end |
#set(key, value, expires_in = 0) ⇒ Object
This method is called to set a value within this cache store by it’s key.
58 59 60 61 62 63 64 65 66 |
# File 'lib/cache_store.rb', line 58 def set(key, value, expires_in = 0) remove(build_key(key)) expires = nil if expires_in > 0 now = DateTime.now expires = DateTime.new(now.year, now.month, now.day, 0, 0, expires_in) end @store.push({ key: build_key(key), value: value, expires: expires}) end |