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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/infopark_component_cache/component_cache.rb', line 36 def initialize(obj, name, params={}, guards=[]) @component = Component.new(obj, name, params) if guards.empty? @guards = [ Guards::ValuePresent.new(@component), Guards::LastChanged.new(@component), Guards::ObjCount.new(@component), Guards::ValidFrom.new(@component), Guards::ValidUntil.new(@component) ] else @guards = guards.map do |klass| klass.new(@component) end end end |
Instance Attribute Details
#component ⇒ Object (readonly)
Returns the value of attribute component.
24 25 26 |
# File 'lib/infopark_component_cache/component_cache.rb', line 24 def component @component end |
#guards ⇒ Object (readonly)
Returns the value of attribute guards.
24 25 26 |
# File 'lib/infopark_component_cache/component_cache.rb', line 24 def guards @guards end |
Instance Method Details
#cache ⇒ CacheStorage
Returns instance of CacheStorage to use.
94 95 96 |
# File 'lib/infopark_component_cache/component_cache.rb', line 94 def cache CacheStorage.instance end |
#ensure_consistency! ⇒ Object
99 100 101 |
# File 'lib/infopark_component_cache/component_cache.rb', line 99 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.
59 60 61 62 63 64 65 |
# File 'lib/infopark_component_cache/component_cache.rb', line 59 def expired? return !guards.all?(&:consistent?) rescue => exception raise exception if Rails.env.test? return 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/infopark_component_cache/component_cache.rb', line 75 def fetch(&block) if expired? value = yield begin cache.write(component.cache_key, value) ensure_consistency! return value rescue => exception raise exception if Rails.env.test? return value end else cache.read(component.cache_key) end end |