cache multiple resources from a single request

169 views Asked by At

Let's say I'm making a request for /products.json which returns a JSON array with X number of products. Each is available at /product/[id].json. Is it possible to make siesta cache that information instead of making a request for each product? Or do I have to cache my models separate from their resources?

1

There are 1 answers

0
Paul Cantrell On

There’s a short discussion of this here:

https://github.com/bustoutsolutions/siesta/issues/156

As Siesta currently exists, each URL is a separate resource with a separate cached state. However, you can manually propagate changes from an index/list/search resource to the corresponding resources for its individual results:

childResource.addObserver(self)
parentResource.addObserver(owner: self) {
  if case .newData = $1 {
    childResource.invalidate()
    // Delayed refresh prevents redundant load if multiple
    // children trigger a refresh on the same parent
    DispatchQueue.main.async {
      childResource.loadIfNeeded()
    }
  }
}

That that Github issue discussion for more background.