Simple patch that allows other module developers to purge objects from varnish's cache with a perl regex instead of just a URL.

Currently you would have to create an array containing each individual URL and pass them all to varnish_expire_cache().

This patch would allow you to do things like this:

Purge every page starting with a URL that starts with /red or /blue.

varnish_purge_pcre_matches("^/(blue|red)") 

Purge every page that has the string 'example' any where in the URL.

varnish_purge_pcre_matches("example") 

Purge every page that has a URL that ends with .pdf.

varnish_purge_pcre_matches("\.pdf$") 

Feedback welcome, particularly on the function name. Perhaps varnish_purge_regex_matches() would be better.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fabsor’s picture

Status: Needs review » Needs work

I like the idea! I do think varnish_purge_regex_matches would be a better name though. Maybe we could even just name the function varnish_purge since that is the command you execute against the varnish terminal.

Also, it might be a good thing to be able to optionally specify a host, since that would make it possible to use the module to invalidate the cache for other hosts than the one of the particular site, since this is an API function? It could look something like this:

varnish_purge($pattern, $host = NULL)

Thanks for your work!

alanmackenzie’s picture

Status: Needs work » Needs review
FileSize
599 bytes

Hi,

Everything you said makes perfect sense. I've attached version 2 that takes these changes into account.

alanmackenzie’s picture

Please ignore the last patch, I forgot to rename one of the variables.

Here is one that works :)

fabsor’s picture

Status: Needs review » Needs work

Great work! However, it needs to meet the Drupal coding standards

Here are a few pointers:

+  if ( $host == NULL )
+     $host = _varnish_get_host();

Always use curly braces even though they're technically optional. Also, there should be no spaces inside of the parenthesis.

On a more technical note: We sould probably return the stuff that's coming from _varnish_terminal_run(). It returns the status for each varnish server.

We should probably make sure the rest of the program uses the API function instead of running _varnish_terminal_run when executing a purge operation. That way, we can deal with whatever logic we need to use in there. So, everywhere we run a purge command in _varnish_terminal_run, we should use the API function instead.

alanmackenzie’s picture

My bad. In future I'll use the coder module to check my code is standards compliant before submitting anything.

MiSc’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)

As a part of a clean up of the issue queue for the varnish module I am closing tickets that had no activity for the last year, please feel free to re-open if needed.