Class: Vedeu::Repositories::Cache Private
- Inherits:
-
Object
- Object
- Vedeu::Repositories::Cache
- Defined in:
- lib/vedeu/repositories/cache.rb
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.
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.
12 13 14 15 |
# File 'lib/vedeu/repositories/cache.rb', line 12 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.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/vedeu/repositories/cache.rb', line 25 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.
40 41 42 |
# File 'lib/vedeu/repositories/cache.rb', line 40 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.
48 49 50 51 52 53 54 55 |
# File 'lib/vedeu/repositories/cache.rb', line 48 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.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vedeu/repositories/cache.rb', line 61 def remove(resource) if @cache.key?(resource) @cache.delete(resource) true else false end end |