Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

LiteSpeed Cache - Purge

Solved by Marcin View solution

Started by Marcin 4 years ago · 5 replies · 786 views
4 years ago

Hi,

I am running GRAV on hosting with LiteSpeed Cache. Caching is working great, but I have to manually clear cache each time I made change on pages as I couldn't find any plugin for GRAV (use command curl -i -X PURGE https://page-url).

What I am trying to achieve is plugin that will purge page from cache after admin save.

Just for testing purposes I create .php file on root folder with code
<?php header('X-LiteSpeed-Purge: /page-url'); ?> and it is working fine each time I hit file.

So now I am trying to use this code in plugin with onAdminAfterSave function - but without any success.

Will be great to get any suggestions from U

4 years ago

I will reply to myself, as I find part solution...

PHP
    public function onPluginsInitialized()
    {
        if ($this->isAdmin()) {
            $this->enable([
                'onAdminAfterSave'    => ['onAdminAfterSave', 0]
            ]);
            return;
        }
    }

    public function onAdminAfterSave(Event $event)
    {
        header('X-LiteSpeed-Purge: *');
    }

this will Purge all pages from LiteSpeed Cache after pressing Save button...

Now question is how to purge (get /page-url) only for edited page?

4 years ago

@lisekbox,

Now question is how to purge (get /page-url) only for edited page?

Try something like:

PHP
public function onAdminAfterSave(Event $event)
{
    $object = $event['object'];

    if ($object instanceof PageObject) {
        $route = $object->route();
        header("X-LiteSpeed-Purge: $route");
    }
}
👍 1
4 years ago Solution

Thanks!
It didn't work but it it was so close!
I made only small change in your code (PageObject -> PageInterface)

PHP
public function onAdminAfterSave(Event $event)
    {
        $object = $event['object'];
        if ($object instanceof PageInterface) {
            $route = $object->route();
            header("X-LiteSpeed-Purge: $route");
        }
    }
last edited 11/28/22 by Marcin
4 years ago

@lisekbox, According the debugger the $event['object'] should be a PageObject, which also implements interface PageInterface...

Just to nitpick, $object should be used in the if-statement. ;-)

👍 1
4 years ago

ahh.. I was testing many versions that's why I miss that... Thanks - I just made correction :crazy_face:

when PageObject is in if-statement page headers didn't change after saving (x-litespeed-cache: hit), so I remove if-statement and it worked - I get cache: miss , but leaving code like that will cause error when saving on other places - for example when making updates on plugin settings, so I tried PageInterface and it works

Once again Thanks a lot!

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 42 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 71 2 months ago