Class: InfoparkComponentCache::ComponentCache
- Inherits:
-
Object
- Object
- InfoparkComponentCache::ComponentCache
- Defined in:
- lib/infopark_component_cache/component_cache.rb
Overview
This class provides user-level access to component cache.
By default ComponentCache comes with a set of Guards, i.e. classes that check the consistency of cache and invalidate it, should it be neccesary.
Instance Attribute Summary collapse
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#guards ⇒ Object
readonly
Returns the value of attribute guards.
Instance Method Summary collapse
-
#cache ⇒ CacheStorage
Instance of CacheStorage to use.
- #ensure_consistency! ⇒ Object
-
#expired? ⇒ Boolean
Checks if cache is valid (in consistent state).
-
#fetch(&_block) ⇒ Object
Checks if the cache is in consistent state and cached value can be returned.
-
#initialize(obj, name, params = {}, guards = []) ⇒ ComponentCache
constructor
First three parameters are used to construct the component.
Constructor Details
#initialize(obj, name, params = {}, guards = []) ⇒ ComponentCache
First three parameters are used to construct the component
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/infopark_component_cache/component_cache.rb', line 27 def initialize(obj, name, params = {}, guards = []) @component = Component.new(obj, name, params) @guards = if guards.empty? [ Guards::ValuePresent.new(@component), Guards::LastChanged.new(@component), Guards::ObjCount.new(@component), Guards::ValidFrom.new(@component), Guards::ValidUntil.new(@component) ] else guards.map do |klass_or_hash| if klass_or_hash.kind_of?(Hash) klass = klass_or_hash.delete(:guard) klass.new(@component, klass_or_hash) else klass = klass_or_hash klass.new(@component) end end end end |
Instance Attribute Details
#component ⇒ Object (readonly)
Returns the value of attribute component.
15 16 17 |
# File 'lib/infopark_component_cache/component_cache.rb', line 15 def component @component end |
#guards ⇒ Object (readonly)
Returns the value of attribute guards.
15 16 17 |
# File 'lib/infopark_component_cache/component_cache.rb', line 15 def guards @guards end |
Instance Method Details
#cache ⇒ CacheStorage
Returns instance of CacheStorage to use.
95 96 97 |
# File 'lib/infopark_component_cache/component_cache.rb', line 95 def cache CacheStorage.instance end |
#ensure_consistency! ⇒ Object
100 101 102 |
# File 'lib/infopark_component_cache/component_cache.rb', line 100 def ensure_consistency! guards.each(&:guard!) end |
#expired? ⇒ Boolean
Checks if cache is valid (in consistent state). It delegates work to specified #guards
For any unexpected case it returns true.
56 57 58 59 60 61 62 63 64 |
# File 'lib/infopark_component_cache/component_cache.rb', line 56 def expired? return true unless cache.enabled? !guards.all?(&:consistent?) rescue StandardError => e raise e if Rails.env.test? true end |
#fetch(&_block) ⇒ Object
Checks if the cache is in consistent state and cached value can be returned. In such case it returns cached value. Otherwise it evaluates passed block and updates the cache.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/infopark_component_cache/component_cache.rb', line 74 def fetch(&_block) if expired? value = yield begin if cache.enabled? cache.write(component.cache_key, value) ensure_consistency! end value rescue StandardError => e raise e if Rails.env.test? value end else cache.read(component.cache_key) end end |