Class: WCC::Contentful::Store::LazyCacheStore
- Inherits:
-
Object
- Object
- WCC::Contentful::Store::LazyCacheStore
- Defined in:
- lib/wcc/contentful/store/lazy_cache_store.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #find(key) ⇒ Object
-
#index(json) ⇒ Object
#index is called whenever the sync API comes back with more data.
-
#initialize(client, cache: nil) ⇒ LazyCacheStore
constructor
A new instance of LazyCacheStore.
- #nil_obj(id) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize(client, cache: nil) ⇒ LazyCacheStore
Returns a new instance of LazyCacheStore.
12 13 14 15 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 12 def initialize(client, cache: nil) @cdn = CDNAdapter.new(client) @cache = cache || ActiveSupport::Cache::MemoryStore.new end |
Instance Method Details
#delete(key) ⇒ Object
60 61 62 63 64 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 60 def delete(key) old = @cache.read(key) @cache.delete(key) old end |
#find(key) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 17 def find(key) found = @cache.fetch(key) do # if it's not a contentful ID don't hit the API. # Store a nil object if we can't find the object on the CDN. (@cdn.find(key) || nil_obj(key)) if key =~ /^\w+$/ end case found.try(:dig, 'sys', 'type') when 'Nil', 'DeletedEntry', 'DeletedAsset' nil else found end end |
#index(json) ⇒ Object
#index is called whenever the sync API comes back with more data.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 34 def index(json) id = json.dig('sys', 'id') return unless prev = @cache.read(id) if (prev_rev = prev&.dig('sys', 'revision')) && (next_rev = json.dig('sys', 'revision')) return prev if next_rev < prev_rev end # we also set deletes in the cache - no need to go hit the API when we know # this is a nil object @cache.write(id, json) case json.dig('sys', 'type') when 'DeletedEntry', 'DeletedAsset' nil else json end end |
#nil_obj(id) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 66 def nil_obj(id) { 'sys' => { 'id' => id, 'type' => 'Nil', 'revision' => 1 } } end |
#set(key, value) ⇒ Object
54 55 56 57 58 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 54 def set(key, value) old = @cache.read(key) @cache.write(key, value) old end |