Class: InfoparkComponentCache::ComponentCache

Inherits:
Object
  • Object
show all
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.

Examples:

Do some expensive computation on a page

InfoparkComponentCache::ComponentCache.new(@obj, 'processed_body', :page => 14) do
  expensive_process_body @obj.body
end

See Also:

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, name, params = {}, guards = []) ⇒ ComponentCache

First three parameters are used to construct the component

Parameters:

  • guards (Array<Class>, Array<Hash>) (defaults to: [])

    list of guard classes used when deciding whether cache is valid when left empty the default set is used: @see Guard::ValuePresent @see Guard::LastChanged @see Guard::ObjCount

See Also:



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

#componentObject (readonly)

Returns the value of attribute component.



15
16
17
# File 'lib/infopark_component_cache/component_cache.rb', line 15

def component
  @component
end

#guardsObject (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

#cacheCacheStorage

Returns instance of CacheStorage to use.

Returns:



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.

Returns:

  • (Boolean)

    true if cache is valid and in consistent state



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.

Yield Returns:

  • value to be used in case of cache miss

Returns:

  • cached value or the return value of the block

See Also:

  • InfoparkComponentCache::ComponentCache.{{#expired?}


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