Sha256: d6c209006497efd57d8dc5ccda719cdbace78ae83bd69e1896210f247a6ba75d

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'infopark_component_cache/consistency_guard'

module InfoparkComponentCache
  module Guards
    # @author Tomasz Przedmojski <tomasz.przedmojski@infopark.de>
    #
    # This Guard class ensures that the total count of Objs in the database
    # does not change inbetween. (It caches the number of Objs and compares
    # it to the current count)
    class ObjCount < ConsistencyGuard
      def consistent?
        count_known? && no_changes_since?
      end

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

      # @return true if obj count can be read from cache with {#cache_key}
      def count_known?
        cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Fixnum)
      end

      # @return true if no obj has been deleted or added since last {#guard!}
      def no_changes_since?
        current_count == cache.read(cache_key)
      end

      # @return [String] the cache key for storing {#current_count}
      def cache_key
        component.cache_key('obj_count')
      end

      # @return [Fixnum] the number of Objs in the database
      def current_count
        RailsConnector::Obj.count
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
infopark_component_cache-1.1.0 lib/infopark_component_cache/guards/obj_count.rb
infopark_component_cache-1.0.0 lib/infopark_component_cache/guards/obj_count.rb