Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.
This repository was archived by the owner on May 8, 2024. It is now read-only.

Requires remote web requests even with full cache usage #11

Open
@zrisher

Description

@zrisher

I'm a little confused as to how to best make use of the existing caching support.

I see in lib/contentful_rails/caching/timestamps.rb that this maintains a timestamp_cache_key for ContentfulModels that gets invalidated automatically if you tell Contentful to post back to your webhook. This also sets updated_at to cache based on this key. So, from my understanding, this allows us to do things like:

  <% Product.all.each do |p| %>
    <% cache(p) do %>
      <%= link_to p.name, product_url(p) %>
    <% end %>
  <% end %>

where a ContentfulModel named Product can be provided to Rails's cache method to provide the key. Very cool.

But getting the full list of Products is expensive too - it will take at least several hundred milliseconds and at worst many seconds.

With a database, the Rails examples deal with this by using simpler, faster queries to determine a cache key, and then checking against that to see if they need to query the whole set:

def cache_key_for_products
  count          = Product.count
  max_updated_at = Product.maximum(:updated_at).try(:utc).try(:to_s, :number)
  "products/all-#{count}-#{max_updated_at}"
end

...

<% cache(cache_key_for_products) do %>
  All available products:
  <% Product.all.each do |p| %>
    <% cache(p) do %>
      <%= link_to p.name, product_url(p) %>
    <% end %>
  <% end %>
<% end %>

But we can't do that, because no matter how simple the call to Contentful, it's still a remote web request and it's going to take forever in relation to other initial page load blockers.

So it seems like we need to maintain cache keys for each ContentfulModel class, which it then checks before refreshing an internally cached result set. These keys should be invalidated by a webhook POST for that Content Type.

These seems like it might but probably doesn't fit under this item listed on the Readme's todos:

Implement a method on ContentfulModel to simulate a parent-child relationship, so we can invalidate caches for parent items

Can you confirm my thinking here? Is a solution like that planned? How have other people solved this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions