Class: Primalize::JSONAPI::Cache
- Inherits:
-
Object
- Object
- Primalize::JSONAPI::Cache
- Defined in:
- lib/primalize/jsonapi.rb
Instance Method Summary collapse
- #[](type, model) ⇒ Object
- #[]=(type, model, value) ⇒ Object
- #fetch(type, model) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
130 131 132 133 134 135 136 137 |
# File 'lib/primalize/jsonapi.rb', line 130 def initialize # Three-layer cache: metadata/serialization, class, and id @cache = Hash.new do |h, k| h[k] = Hash.new do |h, k| h[k] = {} end end end |
Instance Method Details
#[](type, model) ⇒ Object
139 140 141 142 143 |
# File 'lib/primalize/jsonapi.rb', line 139 def [] type, model return if model.nil? @cache[type][model.class][model.id] end |
#[]=(type, model, value) ⇒ Object
145 146 147 148 149 |
# File 'lib/primalize/jsonapi.rb', line 145 def []= type, model, value return if model.nil? @cache[type][model.class][model.id] = value end |
#fetch(type, model) ⇒ Object
151 152 153 154 155 |
# File 'lib/primalize/jsonapi.rb', line 151 def fetch type, model return if model.nil? @cache[type][model.class][model.id] ||= yield end |