eZ Community » Blogs » Peter Keung » Built-in Varnish Cache...


Built-in Varnish Cache "purge-on-publish" support in eZ Publish

Friday 22 May 2015 7:28:53 pm

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

eZ Publish 5 comes with built-in Varnish Cache support. Essentially this means that when content is published in the eZ Publish back-end, it notifies Varnish so that the Varnish cache is cleared. This feature is often called "purge-on-publish" and makes it so that you can cache your pages for a very long time, but that edits refresh the cache and thus appear immediately. To get this native support, you just have to use the "new stack" in eZ Publish. However, even if your legacy site is not ready to be fully upgraded to the new stack and you are running eZ Publish 5 in "legacy mode", you can take advantage of this native support.

The core part of the "purge-on-publish" feature is the "X-Location-Id" HTTP header. When eZ Publish serves up a node / location full view, it sends the "X-Location-Id" header as part of the response. This gets cached in Varnish. When content is edited in eZ Publish, it sends an instruction to Varnish to clear its cache of items containing the "X-Location-Id" header with the node / location ID value.

On an eZ Publish 5 legacy site, if you route all requests through the Symfony kernel but in "legacy mode", eZ Publish will try to clear the Varnish cache, presuming you have also properly configured the purge_servers settings. Then all you need to do is configure the eZ Publish legacy kernel to include an "X-Location-Id" header as part of its response. To do this, simply hook into the "content/view" event listener:

1. Create a PHP function to inject the "X-Location-Id" header:

class YourFunctionCollection
      * Injects X-Location-Id HTTP header for Varnish cache clearing purposes
      * Takes advantage of content/view listener
      * Activated in [Event] block of site.ini
      * Listeners[]=content/view@YourFunctionCollection::addXLocationIdHeader
     public static function addXLocationIdHeader( int $nodeID, eZINI $siteINI )
         header( 'X-Location-Id: ' . $nodeID );
         return $nodeID;

In an override of site.ini, tell eZ Publish to run this function whenever it generates a node full view:


Now you have "purge-on-publish" support for your eZ Publish 5 legacy site!

Proudly Developed with from