module Scrivito # This module provides advances auto-invalidating caching mechanism for storing obj data. # # To keep it up-to-date its caches and changes should be updated periodically # Its changes should be updated every time a new workspace data has been fetched. # Its caches should be updated every time a new obj data has been fetched. module ContentStateCaching class << self # How deep should a content state chain be inspected. Default depth is 20. attr_accessor :cache_lookup_depth # At which lookup depth to copy a hit found in an ancestor content state # to the current content state's cache. Default depth is 5. attr_accessor :cache_replication_depth # Updates caches with data from given workspace. # Should be called every time a new OBJ data has been fetched. def store_obj_data(content_state, index, key, data) content_state.save_obj_data(index, key, data) if content_state end # Fetches up-to-date obj data for given workspace, index and key. # Returns nil if no up-to-date data found. def find_obj_data(current_content_state , index, key) if index == 'permalink' current_content_state.find_obj_data(index, key) else visitor = ContentStateVisitor.new(current_content_state) cache_lookup_depth.times do |depth| return unless content_state = visitor.visit_next if hit = content_state.find_obj_data(index, key) visitor.visited_except_current.each { |cs| return if cs.has_changes_for?(index, key, hit) } current_content_state.save_obj_data(index, key, hit) if depth >= cache_replication_depth return hit end end nil end end def store_obj_classes_data(content_state, data) content_state.save_obj_classes_data(data) end def find_obj_classes_data(current_content_state) visitor = ContentStateVisitor.new(current_content_state) cache_lookup_depth.times do |depth| return unless content_state = visitor.visit_next if obj_classes_data = content_state.find_obj_classes_data if depth >= cache_replication_depth current_content_state.save_obj_classes_data(obj_classes_data) end return obj_classes_data end end nil end end self.cache_replication_depth = 5 self.cache_lookup_depth = 20 end end