0

I'm programmatically updating images and keeping the same file name. I flush the image styles for the image when I do this, but I'm running into an issue where my image styles aren't being regenerated because the old image style is being loaded from varnish when the attempt to regenerate them is made. I've read (in a couple places, including this related Drupal answer How can I clear the Varnish cache?) that I can clear the cache with a curl request:

curl -X PURGE -H "Host: domain.com" http://127.0.0.1/test.html

How can I use an existing method in the Purge or Acquia Purge modules to clear the cache for a given url, specifically an image style url?

Note: I've seen the purge_queuer_url module, but that is not a viable solution for me.

Update: I don't even think this was happening. I can't reproduce the varnish cache hits at all so I must've been wrong about that. I found a bug elsewhere with the focal_point module that was causing my image styles to not be recreated properly.

  • Image styles are served by Drupal when they are first created, but then saved on disk. So, the are served up by your webserver directly aferwards. That makes this more of a Varnish question, but I am leaving this open as it is a Drupal related quirk/issue with how image styles work and Varnish. – mpdonadio Jun 7 at 23:01
4

Here's some code that I use to clear Acquia varish via the Purge / Acquia purge modules when a pdf with the same name is uploaded. You could expand on this to clear the styles cache as well.

/**
 * Implements hook_entity_update().
 */
function my_module_entity_update(EntityInterface $entity) {
  $type = $entity->getEntityTypeId();
  if ($type != 'file') {
    return;
  }
  // \Drupal::logger('my_module')->debug("File entity found");
  $purgeInvalidationFactory = \Drupal::service('purge.invalidation.factory');
  $purgeQueuers = \Drupal::service('purge.queuers');
  $purgeQueue = \Drupal::service('purge.queue');

  $queuer = $purgeQueuers->get('coretags');
  // Invalidate cache tags so all pages that have the file get banned from varnish
  // Also invalidate the file's url.
  $invalidations = [
      $purgeInvalidationFactory->get('tag', 'file:' . $entity->id()),
      $purgeInvalidationFactory->get('url', file_create_url($entity->getFileUri())),
  ];

  $purgeQueue->add($queuer, $invalidations);
}
  • Thank you, this works. I try similar code but I wasn't sure what is the value to get the queuer. Apparently it is coretags. – Permana Jul 10 at 8:00

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.