Class: SmartCore::Engine::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/engine/cache.rb

Overview

Since:

  • 0.13.0

Instance Method Summary collapse

Constructor Details

#initializevoid

Since:

  • 0.13.0



10
11
12
13
# File 'lib/smart_core/engine/cache.rb', line 10

def initialize
  @store = {}
  # TODO: thread-safety (use SmartCore::Engine::Lock)
end

Instance Method Details

#clearNilClass

Returns:

  • (NilClass)

Since:

  • 0.13.0



45
46
47
48
# File 'lib/smart_core/engine/cache.rb', line 45

def clear
  @store.clear
  nil
end

#read(key, &fallback) ⇒ Any, NilClass Also known as: []

rubocop:disable Style/NestedTernaryOperator

Parameters:

  • key (Any)
  • fallback (Block)

Returns:

  • (Any, NilClass)

Since:

  • 0.13.0



33
34
35
36
37
# File 'lib/smart_core/engine/cache.rb', line 33

def read(key, &fallback)
  # @note
  #   key?-flow is a compromise used to provide an ability to cache `nil` objects too.
  @store.key?(key) ? @store[key] : (block_given? ? write(key, yield) : nil)
end

#write(key, value) ⇒ Any Also known as: []=

Parameters:

  • key (Any)

Returns:

  • (Any)

Since:

  • 0.13.0



21
22
23
# File 'lib/smart_core/engine/cache.rb', line 21

def write(key, value)
  @store[key] = value
end