Class: Nodepile::CrudeCalculationCache
- Inherits:
-
Object
- Object
- Nodepile::CrudeCalculationCache
- Defined in:
- lib/nodepile/base_structs.rb
Overview
Caches the result of named calculations and their parameters and provides a crude flushing
Constant Summary collapse
- @@global_disable_cache =
false
Class Method Summary collapse
-
.global_disable_cache(flag) ⇒ Object
Used to disable use of cache by all instances of this class.
Instance Method Summary collapse
-
#cache(sym, *parms) ⇒ Object
Will either retrieve the value matching these parameters from the cache or will do the calculation, cache the value, and return the result.
- #fetch_cache(sym, *parms) ⇒ Object
-
#flush_cache(name = nil, *params) ⇒ Object
Empties the entire cache forcing recalculation of some or all entries.
- #form_cache_key(sym, *parms) ⇒ Object
- #has_cache?(sym, *parms) ⇒ Boolean
-
#initialize(disable_cache: false) ⇒ CrudeCalculationCache
constructor
A new instance of CrudeCalculationCache.
- #stuff_cache(val, sym, *parms) ⇒ Object
Constructor Details
#initialize(disable_cache: false) ⇒ CrudeCalculationCache
Returns a new instance of CrudeCalculationCache.
16 17 18 19 |
# File 'lib/nodepile/base_structs.rb', line 16 def initialize(disable_cache: false) @h = Hash.new @disable_cache = disable_cache end |
Class Method Details
.global_disable_cache(flag) ⇒ Object
Used to disable use of cache by all instances of this class. Note that this is extremely useful during testing.
12 |
# File 'lib/nodepile/base_structs.rb', line 12 def self.global_disable_cache(flag) = @@universally_disable_cache = flag |
Instance Method Details
#cache(sym, *parms) ⇒ Object
Will either retrieve the value matching these parameters from the cache or will do the calculation, cache the value, and return the result
49 50 51 52 53 54 55 56 57 |
# File 'lib/nodepile/base_structs.rb', line 49 def cache(sym,*parms) raise "Calculation block expected" unless block_given? key = form_cache_key(sym,*parms) if @h.include?(key) && !@disable_cache && !@@global_disable_cache return @h[key] else return @h[sym] = yield(*parms) end end |
#fetch_cache(sym, *parms) ⇒ Object
41 42 43 44 45 |
# File 'lib/nodepile/base_structs.rb', line 41 def fetch_cache(sym,*parms) key = form_cache_key(sym,*parms) raise "This item is not in the cache #{key.inspect}" unless @h.include?(key) return @h[key] end |
#flush_cache(name = nil, *params) ⇒ Object
Empties the entire cache forcing recalculation of some or all entries
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/nodepile/base_structs.rb', line 25 def flush_cache(name = nil, *params) if key.nil? @h.clear elsif params.empty? @h.delete(name) # delete simple keys @h.delete{|(cname,*cparams)| cname == name } else @h.delete([key,*params]) end return nil end |
#form_cache_key(sym, *parms) ⇒ Object
37 |
# File 'lib/nodepile/base_structs.rb', line 37 def form_cache_key(sym,*parms) = parms.empty? ? sym : [sym,*parms] |
#has_cache?(sym, *parms) ⇒ Boolean
39 |
# File 'lib/nodepile/base_structs.rb', line 39 def has_cache?(sym,*parms) = @h.include?(form_cache_key(sym,*parms)) |
#stuff_cache(val, sym, *parms) ⇒ Object
38 |
# File 'lib/nodepile/base_structs.rb', line 38 def stuff_cache(val,sym,*parms) = @h[form_cache_key(sym,*parms)] = val |