Class: Vedeu::Repositories::Cache Private
- Inherits:
-
Object
- Object
- Vedeu::Repositories::Cache
- Defined in:
- lib/vedeu/repositories/cache.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class is not currently used and will be used in future.
Allow the storing of values for a finite period to save repeated computation.
Instance Method Summary collapse
-
#add(resource, options = {}) ⇒ void
private
Add a new resource to the cache.
-
#clear ⇒ Hash
private
Remove all cached resources.
- #initialize ⇒ Vedeu::Repositories::Cache constructor private
-
#read(resource) ⇒ void
private
Read the cached resource if it exists.
-
#remove(resource) ⇒ Boolean
private
Remove the cached resource if it exists.
Constructor Details
#initialize ⇒ Vedeu::Repositories::Cache
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 21 |
# File 'lib/vedeu/repositories/cache.rb', line 18 def initialize @cache ||= {} @lock = Mutex.new end |
Instance Method Details
#add(resource, options = {}) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Add a new resource to the cache.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/vedeu/repositories/cache.rb', line 31 def add(resource, = {}) value = [:value] expires = .fetch(:expires, 600) @lock.synchronize do @cache[resource] = { value: value, expires_at: (Time.now + expires), } end end |
#clear ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove all cached resources.
46 47 48 |
# File 'lib/vedeu/repositories/cache.rb', line 46 def clear @cache = {} end |
#read(resource) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Read the cached resource if it exists.
54 55 56 57 58 59 60 61 |
# File 'lib/vedeu/repositories/cache.rb', line 54 def read(resource) resource = @cache[resource] return unless resource return if resource[:expires_at] < Time.now resource[:value] end |
#remove(resource) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove the cached resource if it exists.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vedeu/repositories/cache.rb', line 67 def remove(resource) if @cache.key?(resource) @cache.delete(resource) true else false end end |