Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Run Cache Flush Command (for Varnish) on Publish


Go to End


9 Posts   2501 Views

Avatar
Bam

Community Member, 16 Posts

12 September 2013 at 3:19pm

Hi All

I am looking at setting up Varnish with Nginx for Silverstripe. Everything is working well, but I need a way to invalidate pages in the cache when they are updated in the cms. Is there a way I can trigger a local command or a url load when a page is published?

Cheers
Sam

Avatar
(deleted)

Community Member, 473 Posts

12 September 2013 at 5:10pm

You can use an onAfterWrite() hook on your Page class.

Avatar
Bam

Community Member, 16 Posts

14 September 2013 at 5:10pm

Thanks Simon

Will give it a go.

Avatar
Bam

Community Member, 16 Posts

16 September 2013 at 1:18pm

Thanks Simon

That worked great!

public function onAfterWrite() {
system('varnishadm -T :6082 "ban.url /"');
return true;
}

Clears the entire cache. Is it possible to replace the / with the current page? So it only clears it for that url? I tried:

public function onAfterWrite() {
system('varnishadm -T :6082 "ban.url $this->URLSegment"');
return true;
}

But that passed through ->URLSegment to the command line rather than the segment.

Thanks
Sam

Avatar
(deleted)

Community Member, 473 Posts

16 September 2013 at 1:21pm

Edited: 16/09/2013 1:22pm

Try:

$cmd = escapeshellarg('ban.url ' . $this->Link());
system('varnishadm -T :6082 ' . $cmd);

escapeshellarg() adds the quotes around the string so you don't need to.

Avatar
Bam

Community Member, 16 Posts

16 September 2013 at 1:37pm

Bloody legend, thanks Simon!

Avatar
Bam

Community Member, 16 Posts

16 September 2013 at 1:47pm

One last question. This works, but for it to work properly under all circumstances I will need to flush the parents as well, so changes to page titles and the like will filter upwards to the parents.

If $this->Link gives you the whole url. Is there one that will just give you the top level parent? I.e. get /staff/ instead of /staff/fred/

Avatar
(deleted)

Community Member, 473 Posts

16 September 2013 at 1:54pm

You'd want to call ->Link() on the topmost page, which you can get with something like:

if($this->ParentID) {
$top = $this->getAncestors()->Last();
} else {
$top = $this;
}

Go to Top