Class: MacawFramework::Cache
- Inherits:
-
Object
- Object
- MacawFramework::Cache
- Includes:
- Singleton
- Defined in:
- lib/macaw_framework.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.
269 270 271 |
# File 'lib/macaw_framework.rb', line 269 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.
314 |
# File 'lib/macaw_framework.rb', line 314 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.
281 282 283 |
# File 'lib/macaw_framework.rb', line 281 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.
324 |
# File 'lib/macaw_framework.rb', line 324 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.
295 296 297 298 299 300 301 302 303 304 |
# File 'lib/macaw_framework.rb', line 295 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 |