module Scrivito module Backend class ObjDataCache < Struct.new(:workspace_id, :base) def self.view_for_revision(revision) cache = new(revision.workspace.id, revision.base) cache.view_from(revision.content_state_node) end def view_from(content_state_node) CacheView.new(cache_id, content_state_node) end private def cache_id suffix = base ? "-B" : "" "#{workspace_id}#{suffix}" end class CacheView < Struct.new(:cache_id, :viewed_state) def read_index(index, key) if hit = CmsDataCache.read_obj_data_rest(cache_id, index, key) stored_at = hit.first hit.second if viewed_state.content_state_id == stored_at end end def write_index(index, key, data) CmsDataCache.write_obj_data_rest( cache_id, index, key, [viewed_state.content_state_id, data]) end def read_obj(id) if hit = CmsDataCache.read_obj_data_rest(cache_id, "id", id) stored_at, data = hit obj_in_changes = viewed_state.obj_from_changes(id, stored_at) case obj_in_changes when ContentStateNode::CACHE_MISS # could not reconstruct all changes since `stored_at` # so we don't know if data is still valid. nil when ContentStateNode::NOT_FOUND # there were no relevant changes, so the data is still valid data else # the changes revealed a newer version, return it instead obj_in_changes end end end def write_obj(id, data) write_index("id", id, data) end end end end end