1

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.

1
  • 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, 2019 at 23:01

1 Answer 1

6

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);
}
3
  • 1
    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, 2019 at 8:00
  • 1
    You can also implement hook_ENTITY_TYPE_update() to target files: my_module_file_update()
    – neuquen
    Feb 6, 2020 at 19:40
  • I am using dropsolid_purge, which seems to not do anything for 'url', only for 'tag' and 'everything'. See drupal.org/project/dropsolid_purge/issues/3104082
    – donquixote
    Oct 24 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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