Sha256: 357462ab2302aa47640c524aa269fe32d37b3057f8e3552430a340f8cdac5347

Contents?: true

Size: 1.12 KB

Versions: 8

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true
module Hyrax
  module ControlledVocabularies
    ##
    # adds caching to the {#rdf_label} method.
    #
    # for systems that check the {#rdf_label}and use {#fetch} to get upstream
    # data if it is not present, this can be used to avoid making round trips
    # to an authoritative web source.
    #
    # @see Hyrax::DeepIndexingService
    module ResourceLabelCaching
      CACHE_KEY_PREFIX = "hy_label-v1-"

      ##
      # @note uses the Rails cache to avoid repeated lookups.
      # @see ActiveTriples::Resource#rdf_label
      def rdf_label
        # only cache if this rdf source is represented by a URI;
        # i.e. don't cache for blank nodes
        return super unless uri?

        Rails.cache.fetch(cache_key) { super }
      end

      ##
      # @note adds behavior to clear the cache whenever a manual fetch of data
      #   is performed.
      # @see ActiveTriples::Resource#fetch
      def fetch(*)
        Rails.cache.delete(cache_key)
        super
      end

      private

      def cache_key
        "#{CACHE_KEY_PREFIX}#{to_uri.canonicalize.pname}"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hyrax-3.6.0 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-4.0.0.rc1 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-3.5.0 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-4.0.0.beta2 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-3.4.2 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-4.0.0.beta1 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-3.4.1 lib/hyrax/controlled_vocabularies/resource_label_caching.rb
hyrax-3.4.0 lib/hyrax/controlled_vocabularies/resource_label_caching.rb