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.
49 50 51 52 |
# File 'lib/cache_store.rb', line 49 def initialize(namespace = '') @store = Array.new @namespace = namespace end |
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
47 48 49 |
# File 'lib/cache_store.rb', line 47 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.
117 118 119 |
# File 'lib/cache_store.rb', line 117 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.
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 103 104 |
# File 'lib/cache_store.rb', line 76 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)) 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.
109 110 111 |
# File 'lib/cache_store.rb', line 109 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.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cache_store.rb', line 59 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) expires = Time.now.utc + expires_in end @store.push({ key: build_key(key), value: value, expires: expires}) end |