Class: WCC::Contentful::Store::LazyCacheStore
- Inherits:
-
Object
- Object
- WCC::Contentful::Store::LazyCacheStore
- Defined in:
- lib/wcc/contentful/store/lazy_cache_store.rb
Defined Under Namespace
Classes: Query
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #ensure_hash(val) ⇒ Object
- #find(key, **options) ⇒ Object
- #find_all(content_type:, options: nil) ⇒ 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.
-
#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.
5 6 7 8 9 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 5 def initialize(client, cache: nil) @cdn = CDNAdapter.new(client) @cache = cache || ActiveSupport::Cache::MemoryStore.new @client = client end |
Instance Method Details
#delete(key) ⇒ Object
83 84 85 86 87 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 83 def delete(key) old = @cache.read(key) @cache.delete(key) old end |
#ensure_hash(val) ⇒ Object
99 100 101 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 99 def ensure_hash(val) raise ArgumentError, 'Value must be a Hash' unless val.is_a?(Hash) end |
#find(key, **options) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 11 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 |
#find_all(content_type:, options: nil) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 44 def find_all(content_type:, options: nil) Query.new( store: self, client: @client, relation: { content_type: content_type }, cache: @cache, 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')`
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 30 def find_by(content_type:, filter: nil, options: nil) if filter.keys == ['sys.id'] # Direct ID lookup, like what we do in `WCC::Contentful::ModelMethods.resolve` # We can return just this item. Stores are not required to implement :include option. if found = @cache.read(filter['sys.id']) return found end end q = find_all(content_type: content_type, options: { limit: 1 }.merge!( || {})) q = q.apply(filter) if filter q.first end |
#index(json) ⇒ Object
#index is called whenever the sync API comes back with more data.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 55 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 ensure_hash json @cache.write(id, json) case json.dig('sys', 'type') when 'DeletedEntry', 'DeletedAsset' nil else json end end |
#nil_obj(id) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 89 def nil_obj(id) { 'sys' => { 'id' => id, 'type' => 'Nil', 'revision' => 1 } } end |
#set(key, value) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/wcc/contentful/store/lazy_cache_store.rb', line 76 def set(key, value) ensure_hash value old = @cache.read(key) @cache.write(key, value) old end |