lib/scrivito/cms_backend.rb in scrivito_sdk-0.66.0 vs lib/scrivito/cms_backend.rb in scrivito_sdk-0.70.0.rc1

- old
+ new

@@ -58,10 +58,11 @@ attr_accessor :die_content_service def initialize @query_counter = 0 @caching = true + @die_content_service = true end def begin_caching @caching = true end @@ -86,60 +87,68 @@ # For test purpose only. def reset_query_counter! @query_counter = 0 end - def find_workspace_data_by_id(id) + def find_workspace_data_from_cache(id) if die_content_service + cached_workspace_state = CmsDataCache.read_workspace_state(id) + cached_data_tag = cached_workspace_state.try(:second) + cached_content_state_id = cached_workspace_state.try(:first) + + if cached_data_tag && cached_content_state_id + if raw_workspace_data = fetch_cached_data_by_tag(cached_data_tag) + build_workspace_data(raw_workspace_data, cached_content_state_id) + end + end + else + WorkspaceDataFromService.find_from_cache(id) + end + end + + def find_workspace_data_by_id(id, timeout=nil) + options = timeout ? {timeout: timeout} : {} + + if die_content_service begin cached_workspace_state = CmsDataCache.read_workspace_state(id) cached_csid = cached_workspace_state.try(:first) cached_workspace_data_tag = cached_workspace_state.try(:second) - changes = CmsRestApi.get("/workspaces/#{id}/changes", from: cached_csid) + changes = CmsRestApi.get("/workspaces/#{id}/changes", {from: cached_csid}, options) update_obj_cache(id, cached_csid, changes) - workspace_data, workspace_data_tag = update_workspace_cache( - id, cached_workspace_data_tag, changes["workspace"]) + raw_workspace_data, workspace_data_tag = update_workspace_cache( + id, cached_workspace_data_tag, changes["workspace"], options) current_csid = changes["current"] current_workspace_state = [current_csid, workspace_data_tag] if current_workspace_state != cached_workspace_state CmsDataCache.write_workspace_state(id, current_workspace_state) end - return WorkspaceData.new(workspace_data.merge( - "content_state_id" => current_csid)) + return build_workspace_data(raw_workspace_data, current_csid) rescue Scrivito::ClientError => client_error if client_error.http_code == 404 return nil else raise end end end - workspace_data_from_cache = WorkspaceDataFromService.find_from_cache(id) + workspace_data_from_cache = find_workspace_data_from_cache(id) from_content_state_id = workspace_data_from_cache.try(:content_state_id) request_params = {:workspace_id => id} request_params[:content_state_id] = from_content_state_id if from_content_state_id - raw_data = if id == 'published' && workspace_data_from_cache - begin - ContentService.query('workspaces/query', request_params, timeout: 1) - rescue CommunicationError => e - warn_backend_not_available(id, from_content_state_id, e.message) - return workspace_data_from_cache - end - else - ContentService.query('workspaces/query', request_params) - end + raw_data = ContentService.query('workspaces/query', request_params, options) if raw_workspace_data = raw_data['workspace'] workspace_data = WorkspaceDataFromService.new(raw_workspace_data) if from_content_state_id != workspace_data.content_state_id workspace_data.store_in_cache_and_create_content_state @@ -198,10 +207,22 @@ store_blob_metadata_in_cache(id, blob_metadata) blob_metadata end end + def find_binary_meta_data(blob_id) + blob_id = normalize_blob_id(blob_id) + cache_key = "binary_meta_data/#{blob_id}" + if meta_data = CmsDataCache.cache.read(cache_key) + meta_data + else + meta_data = CmsRestApi.get("blobs/#{blob_id}/meta_data")['meta_data'] + CmsDataCache.cache.write(cache_key, meta_data) + meta_data + end + end + def search_objs(workspace, params) cache_index = 'search' cache_key = params.to_param if die_content_service @@ -211,11 +232,11 @@ return hit end result = request_search_result_from_backend(workspace, params) - cache.write_index(cache_index, cache_key, result) + cache.write_index_not_updatable(cache_index, cache_key, result) return result end content_state = workspace.revision.content_state @@ -229,29 +250,35 @@ end end private - def update_workspace_cache(id, cached_data_tag, changed_workspace) + def update_workspace_cache(id, cached_data_tag, changed_workspace, options) if changed_workspace workspace_data = changed_workspace else - cached_workspace_data = CmsDataCache.read_data_from_tag(cached_data_tag) - - if cached_workspace_data + if cached_workspace_data = fetch_cached_data_by_tag(cached_data_tag) workspace_data = cached_workspace_data workspace_data_tag = cached_data_tag else - workspace_data = CmsRestApi.get("/workspaces/#{id}") + workspace_data = CmsRestApi.get("/workspaces/#{id}", nil, options) end end workspace_data_tag ||= CmsDataCache.write_data_to_tag(workspace_data) [workspace_data, workspace_data_tag] end + def fetch_cached_data_by_tag(data_tag) + data_tag && CmsDataCache.read_data_from_tag(data_tag) + end + + def build_workspace_data(raw_data, content_state_id) + WorkspaceData.new(raw_data.merge('content_state_id' => content_state_id)) + end + def update_obj_cache(workspace_id, cached_csid, changes) objs = changes["objs"] if objs.present? && objs != "*" last_state = Backend::ContentStateNode.find(cached_csid) changes_index = Backend::ObjDataCache.changes_index_from(objs) @@ -300,11 +327,11 @@ cache_key << "/#{transformation_definition.to_query}" if transformation_definition cache_key end def normalize_blob_id(id) - Addressable::URI.normalize_component(id, Addressable::URI::CharacterClasses::UNRESERVED) + CmsRestApi.normalize_path_component(id) end def fetch_blob_metadata_from_cache(id) CmsDataCache.cache.read(blob_metadata_cache_key(id)) end @@ -418,18 +445,9 @@ ContentStateCaching.store_obj_data(revision.content_state, index, key, item) end def assert_valid_index_name(index) raise ArgumentError, "invalid index name '#{index}'" unless VALID_INDEX_NAMES.include?(index) - end - - def warn_backend_not_available(workspace_id, content_state_id, error_message) - message = <<-EOS - Couldn't connect to content service for workspace with id=#{workspace_id} and content_state_id=#{content_state_id}. - #{error_message} - Serving from cache. - EOS - Rails.logger.warn(message) end end end