Class: InfoparkComponentCache::Guards::LastChanged

Inherits:
CmsStateGuard show all
Defined in:
lib/infopark_component_cache/guards/last_changed.rb

Overview

This Guard class ensures that the objects in database do not change inbetween. (It caches the newest timestamp and compares it to the current value)

Author:

Direct Known Subclasses

DelayedLastChanged

Instance Attribute Summary

Attributes inherited from ConsistencyGuard

#component, #options

Instance Method Summary collapse

Methods inherited from ConsistencyGuard

#cache, #initialize

Constructor Details

This class inherits a constructor from InfoparkComponentCache::ConsistencyGuard

Instance Method Details

#cache_key ⇒ String

Returns the cache key for storing #current_last_changed.

Returns:



29
30
31
# File 'lib/infopark_component_cache/guards/last_changed.rb', line 29

def cache_key
  component.cache_key('last_changed')
end

#consistent? ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/infopark_component_cache/guards/last_changed.rb', line 10

def consistent?
  last_changed_known? && no_changes_since?
end

#current_last_changed ⇒ Time

Returns the timestamp of the most recent change to any Obj.

Returns:

  • (Time) —

    the timestamp of the most recent change to any Obj



34
35
36
# File 'lib/infopark_component_cache/guards/last_changed.rb', line 34

def current_last_changed
  RailsConnector::DateAttribute.parse(scoped_relation.maximum(:last_changed))
end

#guard! ⇒ Object



14
15
16
# File 'lib/infopark_component_cache/guards/last_changed.rb', line 14

def guard!
  cache.write(cache_key, current_last_changed)
end

#last_changed_known? ⇒ Boolean

Returns true if a timestamp can be read from cache with #cache_key.

Returns:

  • (Boolean) —

    true if a timestamp can be read from cache with #cache_key



19
20
21
# File 'lib/infopark_component_cache/guards/last_changed.rb', line 19

def last_changed_known?
  cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Time)
end

#no_changes_since? ⇒ Boolean

Returns true if no obj has been changed since last #guard!.

Returns:

  • (Boolean) —

    true if no obj has been changed since last #guard!



24
25
26
# File 'lib/infopark_component_cache/guards/last_changed.rb', line 24

def no_changes_since?
  current_last_changed <= cache.read(cache_key)
end