Class: MacawFramework::Cache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/macaw_framework.rb

Overview

This singleton class allows to manually cache parameters and other data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#invalidation_frequencyObject

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.

Examples:

MacawFramework::Cache.read("name") # Maria

Parameters:

  • tag (String)

Returns:

  • (String|nil)


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.

Examples:

MacawFramework::Cache.write("name", "Maria", expires_in: 7200)

Parameters:

  • tag (String)
  • value (Object)
  • expires_in (Integer) (defaults to: 3600)

    Defaults to 3600.

Returns:

  • nil



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.

Examples:

MacawFramework::Cache.read("name") # Maria

Parameters:

  • tag (String)

Returns:

  • (String|nil)


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.

Examples:

MacawFramework::Cache.write("name", "Maria", expires_in: 7200)

Parameters:

  • tag (String)
  • value (Object)
  • expires_in (Integer) (defaults to: 3600)

    Defaults to 3600.

Returns:

  • nil



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