Class: WCC::Contentful::Middleware::Store::CachingMiddleware

Inherits:
Object
  • Object
show all
Includes:
Instrumentation, WCC::Contentful::Middleware::Store
Defined in:
lib/wcc/contentful/middleware/store/caching_middleware.rb

Constant Summary

Constants included from Store::Interface

Store::Interface::INTERFACE_METHODS

Instance Attribute Summary collapse

Attributes included from Instrumentation

#_instrumentation

Attributes included from WCC::Contentful::Middleware::Store

#store

Instance Method Summary collapse

Methods included from Instrumentation

#_instrumentation_event_prefix, instrument

Methods included from WCC::Contentful::Middleware::Store

#find_all, #has_select?, #resolve_includes, #resolve_link, #resolved_link?, #transform

Methods included from Store::Interface

#find_all

Constructor Details

#initialize(cache = nil) ⇒ CachingMiddleware

Returns a new instance of CachingMiddleware.



15
16
17
18
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 15

def initialize(cache = nil)
  @cache = cache || ActiveSupport::Cache::MemoryStore.new
  @expires_in = nil
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



9
10
11
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 9

def configuration
  @configuration
end

#expires_inObject

Returns the value of attribute expires_in.



9
10
11
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 9

def expires_in
  @expires_in
end

Instance Method Details

#default_localeObject



11
12
13
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 11

def default_locale
  @default_locale ||= configuration&.default_locale&.to_s || 'en-US'
end

#find(key, **options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 20

def find(key, **options)
  event = 'fresh'
  found =
    @cache.fetch(key, expires_in: expires_in) do
      event = 'miss'
      # if it's from the sync engine don't hit the API.
      next if key =~ /^sync:/

      # Store a nil object if we can't find the object on the CDN.
      (store.find(key, **options) || nil_obj(key))
    end

  return unless found
  return if %w[Nil DeletedEntry DeletedAsset].include?(found.dig('sys', 'type'))

  # If what we found in the cache is for the wrong Locale, go hit the store directly.
  # Now that the one locale is in the cache, when we index next time we'll index the
  # all-locales version and we'll be fine.
  locale = options[:locale]&.to_s || default_locale
  found_locale = found.dig('sys', 'locale')&.to_s
  if found_locale && (found_locale != locale)
    event = 'miss'
    return store.find(key, **options)
  end

  found
ensure
  _instrument(event, key: key, options: options)
end

#find_by(content_type:, filter: nil, options: nil) ⇒ Object

TODO: github.com/watermarkchurch/wcc-contentful/issues/18

figure out how to cache the results of a find_by query, ex:
`find_by('slug' => '/about')`


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 53

def find_by(content_type:, filter: nil, options: nil)
  options ||= {}
  if filter&.keys == ['sys.id'] && found = @cache.read(filter['sys.id'])
    # This is equivalent to a find, usually this is done by the resolver to
    # try to include deeper relationships.  Since we already have this object,
    # don't hit the API again.
    return if %w[Nil DeletedEntry DeletedAsset].include?(found.dig('sys', 'type'))
    return found if found.dig('sys', 'locale') == options[:locale]
  end

  store.find_by(content_type: content_type, filter: filter, options: options)
end

#index(json) ⇒ Object

#index is called whenever the sync API comes back with more data.



69
70
71
72
73
74
75
76
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 69

def index(json)
  delegated_result = store.index(json) if store.index?
  caching_result = _index(json)
  # _index returns nil if we don't already have it cached - so use the store result.
  # store result is nil if it doesn't index, so use the caching result if we have it.
  # They ought to be the same thing if it's cached and the store also indexes.
  caching_result || delegated_result
end

#index?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 78

def index?
  true
end