Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Run Cache Flush Command (for Varnish) on Publish

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

Page: 1 2
Go to End
Author Topic: 652 Views
  • Bam
    Avatar
    Community Member
    16 Posts

    Run Cache Flush Command (for Varnish) on Publish Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

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

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

    Thanks Simon

    Will give it a go.

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

    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.

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

    Bloody legend, thanks Simon!

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

    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/

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Run Cache Flush Command (for Varnish) on Publish Link to this post

    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;
    }

    652 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.