Class: WCC::Contentful::Middleware::Store::CachingMiddleware
- Inherits:
-
Object
- Object
- WCC::Contentful::Middleware::Store::CachingMiddleware
- 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
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
Attributes included from Instrumentation
Attributes included from WCC::Contentful::Middleware::Store
Instance Method Summary collapse
- #default_locale ⇒ Object
- #find(key, **options) ⇒ Object
-
#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’)‘.
-
#index(json) ⇒ Object
#index is called whenever the sync API comes back with more data.
- #index? ⇒ Boolean
-
#initialize(cache = nil) ⇒ CachingMiddleware
constructor
A new instance of CachingMiddleware.
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
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
#configuration ⇒ Object
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_in ⇒ Object
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_locale ⇒ Object
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, **) 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, **) || 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 = [: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, **) end found ensure _instrument(event, key: key, 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) ||= {} 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') == [:locale] end store.find_by(content_type: content_type, filter: filter, 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
78 79 80 |
# File 'lib/wcc/contentful/middleware/store/caching_middleware.rb', line 78 def index? true end |