Module: Hyrax::ControlledVocabularies::ResourceLabelCaching

Included in:
Location
Defined in:
lib/hyrax/controlled_vocabularies/resource_label_caching.rb

Overview

adds caching to the #rdf_label method.

for systems that check the #rdf_labeland 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.

Constant Summary collapse

CACHE_KEY_PREFIX =
"hy_label-v1-"

Instance Method Summary collapse

Instance Method Details

#fetchObject

Note:

adds behavior to clear the cache whenever a manual fetch of data is performed.

See Also:

  • ActiveTriples::Resource#fetch

30
31
32
33
# File 'lib/hyrax/controlled_vocabularies/resource_label_caching.rb', line 30

def fetch(*)
  Rails.cache.delete(cache_key)
  super
end

#rdf_labelObject

Note:

uses the Rails cache to avoid repeated lookups.

See Also:

  • ActiveTriples::Resource#rdf_label

18
19
20
21
22
23
24
# File 'lib/hyrax/controlled_vocabularies/resource_label_caching.rb', line 18

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