Sha256: 000db442fa505e86eda4c65574d3955acd04c5a44aa98c0c63decb4b6b6897c3

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

require "infopark_component_cache/guards/cms_state_guard"

module InfoparkComponentCache
  module Guards
    # @author Tomasz Przedmojski <tomasz.przedmojski@infopark.de>
    #
    # 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)
    class LastChanged < CmsStateGuard
      def consistent?
        last_changed_known? && no_changes_since?
      end

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

      # @return true if a timestamp can be read from cache with {#cache_key}
      def last_changed_known?
        cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Time)
      end

      # @return true if no obj has been changed since last {#guard!}
      def no_changes_since?
        current_last_changed <= cache.read(cache_key)
      end

      # @return [String] the cache key for storing {#current_last_changed}
      def cache_key
        component.cache_key("last_changed")
      end

      # @return [Time] the timestamp of the most recent change to any Obj
      def current_last_changed
        str_value = scoped_relation.maximum(:last_changed)
        return str_value if str_value.kind_of? Time

        RailsConnector::DateAttribute.parse(str_value)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
infopark_component_cache-5.0.2 lib/infopark_component_cache/guards/last_changed.rb
infopark_component_cache-5.0.1 lib/infopark_component_cache/guards/last_changed.rb
infopark_component_cache-4.2.0 lib/infopark_component_cache/guards/last_changed.rb
infopark_component_cache-4.1.0 lib/infopark_component_cache/guards/last_changed.rb
infopark_component_cache-4.0.1 lib/infopark_component_cache/guards/last_changed.rb
infopark_component_cache-4.0.0 lib/infopark_component_cache/guards/last_changed.rb