From 84083991f186f3c4021c89a495cfb63c3e954539 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Wed, 26 Sep 2018 12:15:45 +0200 Subject: [PATCH] don't cache in the serializer. datacite/datacite#491 --- app/controllers/dois_controller.rb | 4 +--- app/models/concerns/cacheable.rb | 14 +++++++++----- app/models/doi.rb | 12 +++++------- app/serializers/client_serializer.rb | 1 - app/serializers/doi_serializer.rb | 3 +-- app/serializers/provider_serializer.rb | 1 - 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 467dcda93..e089890ee 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -228,10 +228,8 @@ def update if exists if params[:data][:attributes][:mode] == "transfer" authorize! :transfer, @doi - elsif safe_params[:xml].present? || safe_params[:url].present? || safe_params[:event].present? - authorize! :update, @doi else - fail ActionController::ParameterMissing + authorize! :update, @doi end @doi.assign_attributes(safe_params.except(:doi)) diff --git a/app/models/concerns/cacheable.rb b/app/models/concerns/cacheable.rb index f98823786..e74d2f20f 100644 --- a/app/models/concerns/cacheable.rb +++ b/app/models/concerns/cacheable.rb @@ -51,8 +51,8 @@ def cached_media_count(options={}) end def fetch_cached_meta - if timestamp.present? - Rails.cache.fetch("cached_meta/#{doi}-#{timestamp}") do + if updated.present? + Rails.cache.fetch("cached_meta/#{doi}-#{updated.iso8601}") do if from.present? && string.present? send("read_" + from, string: string, sandbox: sandbox) else @@ -73,8 +73,8 @@ def fetch_cached_meta end def fetch_cached_xml - if timestamp.present? - Rails.cache.fetch("cached_xml/#{doi}-#{timestamp}", raw: true) do + if updated.present? + Rails.cache.fetch("cached_xml/#{doi}-#{updated.iso8601}", raw: true) do m = metadata.first m.present? ? m.xml : nil end @@ -85,7 +85,11 @@ def fetch_cached_xml end def fetch_cached_metadata_version - Rails.cache.fetch("cached_metadata_version/#{doi}-#{timestamp}") do + if updated.present? + Rails.cache.fetch("cached_metadata_version/#{doi}-#{updated.iso8601}") do + current_metadata ? current_metadata.metadata_version : 0 + end + else current_metadata ? current_metadata.metadata_version : 0 end end diff --git a/app/models/doi.rb b/app/models/doi.rb index 67353341b..628ca344d 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -423,10 +423,6 @@ def event=(value) self.send(value) if %w(register publish hide).include?(value) end - def timestamp - updated.utc.iso8601 if updated.present? - end - # update state for all DOIs in state "undetermined" starting from from_date def self.set_state(from_date: nil) from_date ||= Time.zone.now - 1.day @@ -477,8 +473,10 @@ def self.register_all_urls(limit: nil) # update metadata when any virtual attribute has changed def update_metadata - changed_virtual_attributes = changed & %w(author title publisher date_published additional_type resource_type_general description content_size content_format) + logger = Logger.new(STDOUT) + changed_virtual_attributes = changed & %w(author title publisher date_published additional_type resource_type_general description content_size content_format) + if changed_virtual_attributes.present? @xml = datacite_xml doc = Nokogiri::XML(xml, nil, 'UTF-8', &:noblanks) @@ -486,8 +484,8 @@ def update_metadata @schema_version = Array.wrap(ns).last || "http://datacite.org/schema/kernel-4" attribute_will_change!(:xml) end - - metadata.build(doi: self, xml: xml, namespace: schema_version) if (changed & %w(xml)).present? + logger.info xml + metadata.build(doi: self, xml: xml, namespace: schema_version) if xml.present? end def set_defaults diff --git a/app/serializers/client_serializer.rb b/app/serializers/client_serializer.rb index ea09c420f..8788e2a65 100644 --- a/app/serializers/client_serializer.rb +++ b/app/serializers/client_serializer.rb @@ -3,7 +3,6 @@ class ClientSerializer set_key_transform :dash set_type :clients set_id :uid - cache_options enabled: true, cache_length: 24.hours attributes :name, :symbol, :year, :contact_name, :contact_email, :domains, :url, :created, :updated diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index d3e0478cf..15ec9c33e 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -3,9 +3,8 @@ class DoiSerializer set_key_transform :dash set_type :dois set_id :uid - cache_options enabled: true, cache_length: 24.hours - attributes :doi, :identifier, :url, :prefix, :suffix, :author, :title, :publisher, :resource_type_subtype, :description, :version, :metadata_version, :schema_version, :reason, :source, :state, :is_active, :landing_page, :published, :created, :registered, :updated, :xml + attributes :doi, :identifier, :url, :prefix, :suffix, :author, :title, :publisher, :resource_type_subtype, :description, :version, :metadata_version, :schema_version, :reason, :source, :state, :is_active, :landing_page, :published, :created, :registered, :updated, :xml, :cache_key belongs_to :client, record_type: :clients belongs_to :resource_type, record_type: :resource_types diff --git a/app/serializers/provider_serializer.rb b/app/serializers/provider_serializer.rb index 25b77aec9..7828e9b97 100644 --- a/app/serializers/provider_serializer.rb +++ b/app/serializers/provider_serializer.rb @@ -3,7 +3,6 @@ class ProviderSerializer set_key_transform :dash set_type :providers set_id :uid - cache_options enabled: true, cache_length: 24.hours attributes :name, :symbol, :website, :contact_name, :contact_email, :phone, :description, :region, :country, :logo_url, :institution_type, :is_active, :has_password, :joined, :created, :updated