Skip to content

Commit

Permalink
don't cache in the serializer. datacite/datacite#491
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Sep 26, 2018
1 parent 5aa72b4 commit 8408399
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
4 changes: 1 addition & 3 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 9 additions & 5 deletions app/models/concerns/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -477,17 +473,19 @@ 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)
ns = doc.collect_namespaces.find { |k, v| v.start_with?("http://datacite.org/schema/kernel") }
@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
Expand Down
1 change: 0 additions & 1 deletion app/serializers/client_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions app/serializers/doi_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/serializers/provider_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8408399

Please sign in to comment.