Class: MacawFramework::Cache
- Inherits:
-
Object
- Object
- MacawFramework::Cache
- Includes:
- Singleton
- Defined in:
- lib/macaw_framework/cache.rb
Overview
This singleton class allows to manually cache parameters and other data.
Instance Attribute Summary collapse
-
#invalidation_frequency ⇒ Object
Returns the value of attribute invalidation_frequency.
Class Method Summary collapse
-
.read(tag) ⇒ String|nil
Read the value with the specified tag.
-
.write(tag, value, expires_in: 3600) ⇒ Object
Write a value to Cache memory.
Instance Method Summary collapse
-
#read(tag) ⇒ String|nil
Read the value with the specified tag.
-
#write(tag, value, expires_in: 3600) ⇒ Object
Write a value to Cache memory.
Instance Attribute Details
#invalidation_frequency ⇒ Object
Returns the value of attribute invalidation_frequency.
13 14 15 |
# File 'lib/macaw_framework/cache.rb', line 13 def invalidation_frequency @invalidation_frequency end |
Class Method Details
.read(tag) ⇒ String|nil
Read the value with the specified tag. Can be called statically or from an instance.
58 |
# File 'lib/macaw_framework/cache.rb', line 58 def self.read(tag) = MacawFramework::Cache.instance.read(tag) |
.write(tag, value, expires_in: 3600) ⇒ Object
Write a value to Cache memory. Can be called statically or from an instance.
25 26 27 |
# File 'lib/macaw_framework/cache.rb', line 25 def self.write(tag, value, expires_in: 3600) MacawFramework::Cache.instance.write(tag, value, expires_in: expires_in) end |
Instance Method Details
#read(tag) ⇒ String|nil
Read the value with the specified tag. Can be called statically or from an instance.
68 |
# File 'lib/macaw_framework/cache.rb', line 68 def read(tag) = @cache.dig(tag, :value) |
#write(tag, value, expires_in: 3600) ⇒ Object
Write a value to Cache memory. Can be called statically or from an instance.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/macaw_framework/cache.rb', line 39 def write(tag, value, expires_in: 3600) if read(tag).nil? @mutex.synchronize do @cache.store(tag, { value: value, expires_in: Time.now + expires_in }) end else @cache[tag][:value] = value @cache[tag][:expires_in] = Time.now + expires_in end end |