0

I have an endpoint that returns an array of Order entities, but it's caching the results so when a new order is placed the cache has to be cleared.

The code within the endpoint is:

// Tried this also.
// $this->orderManager->resetCache()
$orders = $this->orderManager->loadByProperties([
  'uid' => $this->customer->id(),
]);
$response = new ResourceResponse($orders);
// maybe this doesn't work because it's an \Drupal\Entity\commerce\Order[]
$response->addCacheableDependency($orders);

return new ResourceResponse($orders);

What am I missing?

  • Have you tried looping over the orders and adding the dependency for each individually? – Clive Sep 21 '17 at 19:19
  • Had not tried that, thanks. Combined with 4k4's answer I'm all set. – Chris Rockwell Sep 21 '17 at 21:04
2

See the comment from @Clive, you can only add single objects as dependency. But you need to add a list tag as well, because the entity query may change because of entities added or modified that are not in the cached list.

$list_tags = \Drupal::entityTypeManager()->getDefinition('entity_type')->getListCacheTags();
$response->getCacheableMetadata()->addCacheTags($list_tags);
  • Once combined with the loop this works great - thanks (again)! – Chris Rockwell Sep 21 '17 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.