Purge not working with Guzzle

#1
Hi All

I have written a Plugin, or am in the process of doing so, that flushes the LS Cache for a specific tag on a Craft CMS website.

My problem is that while the headers get sent ("X-LiteSpeed-Purge":"public,tcb"), the next request still is cached.

I do this upon saving a website entry:

Code:
    /**
     * @var Entry $entry
     */
   $entry = $event->element;
   $elementUrl = $entry->getUrl();
   $purgeKey = 'tcb';

   $client = Craft::createGuzzleClient();

   try {

      $request = new Request('GET', $elementUrl, ['X-LiteSpeed-Purge' => 'public,' . $purgeKey]);
      $response = $client->send($request);
      Craft::$app->getSession()->setError(Craft::t('app', 'Page called: ' . $elementUrl . '(Status: '.$response->getStatusCode() . ')' ) );
             
   } catch (\Exception $e) {

      Craft::error('There was a problem parsing the url: ' . $e->getMessage(), __METHOD__);
      Craft::$app->getSession()->setError(Craft::t('app', 'Error ('.$elementUrl.'): ' . $e->getMessage(), __METHOD__));
               
   }
The server response code is 200, the body from the guzzle response contains the actual page content.

Anyone has an idea what I am doing wrong?
 

mistwang

LiteSpeed Staff
#2
There is no offical cache plugin for Craft CMS, so I guess you are using rewrite rule to do the cache, so the cache objects are not properly tagged with a cache tag in that. thus, purge by tag wont work.
 
#3
Thank you for your reply! The Litespeed part of my htaccess is reading:

Code:
<IfModule LiteSpeed>
  CacheLookup public on
</IfModule>
What works is this
Code:
header('X-LiteSpeed-Purge: public,*');
But not using above Guzzle Code, which tries to send header tag instead of asterisk
 

serpent_driver

Well-Known Member
#4
@Solaris

Maybe I missunderstand what your problem is, but before a cached page can be purged by using cache tag a page must be cached with this tag. If your code is correct you must see cache tag in response header if you request the non cached page.

And ofcourse you can't set a response header from a cached page. If a page is cached there is no PHP. It's pure HTML, but I think you know that?
 
#5
@Solaris

Maybe I missunderstand what your problem is, but before a cached page can be purged by using cache tag a page must be cached with this tag. If your code is correct you must see cache tag in response header if you request the non cached page.

And of course you can't set a response header from a cached page. If a page is cached there is no PHP. It's pure HTML, but I think you know that?
Yes. Thank you for your reply and your time. In fact the pages come back cached, with a cache hit header "hit" tag. So that works.

Header Tag from uncached pages: x-litespeed-tag: tcb,club

When saving this very page in the cms' backend, I send the header to flush. I used kint and the Guzzle MiddleWare to display what you can see in the screenshot.

I assume there is something wrong with either how I set up Guzzle or how Guzzle implements the request, since using the simple PHP header tag to flush everything poses no problems.
 

Attachments

serpent_driver

Well-Known Member
#6
Have you already tried to purge cache by URL? Same syntax but with URL instead:

PHP:
header('X-LiteSpeed-Purge: /aktivitaeten/fritigs-maetschli');
If this also doesn't work something is wrong how you set header in Guzzle.

Btw. Where do your host your page in Switzerland?
 
#7
I made an url based controller action and well it works using in the controller code :

Code:
$litespeedHeaderTag = 'foo';
header('X-LiteSpeed-Purge: tag=' . $litespeedHeaderTag);
So only pages with the foo cache tag would be purged. But it does not work using Guzzle.. and I dont know why :)

I am hosting at cyon.ch, located in Basel. Because they use LiteSpeed Webserver and have great support :)
 
Top