Sha256: d3922c1a223eb7ccbb0dc315ca63f54dae34c40242deec017ccbe7f454567a2c

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scrivito_sdk-0.65.2 lib/scrivito/backend/obj_data_cache.rb
scrivito_sdk-0.65.1 lib/scrivito/backend/obj_data_cache.rb
scrivito_sdk-0.65.0 lib/scrivito/backend/obj_data_cache.rb
scrivito_sdk-0.65.0.rc2 lib/scrivito/backend/obj_data_cache.rb
scrivito_sdk-0.65.0.rc1 lib/scrivito/backend/obj_data_cache.rb