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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Run Cache Flush Command (for Varnish) on Publish


Go to End


9 Posts   3580 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