Purge the cache by URL

#1
Hi, I want to ask how to use purge the cache by URL?
What I want to achieve is the following: to cache everything for 8 hours, and the home page to purge every hour (using cronjobs).
I use openlitespeed and cyberpanel.

What I have managed to do so far is:
1. To cache everything for eight hours (Enable LSCache through rewrite rules when LiteSpeed Cache Plugin not available) adding code to .htaccess:

PHP:
########## Begin - Litespeed cache
<IfModule LiteSpeed>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
  RewriteRule .* - [E=Cache-Control:max-age=28800]
</IfModule>
########## End - Litespeed cache
2. I create the the file under /usr/local/lsws/admin/misc called purge_cache_byurl.php with code:

PHP:
<?php

/****
* purge_cache_byurl
*
* Example: /usr/local/lsws/admin/fcgi-bin/admin_php /usr/local/lsws/admin/misc/purge_cache_byurl.php -r mywebsite.com /index.php
*/

if ($argc < 4 || $argc > 6) {
echo "Invalid arguments!\n";
echo "Usage: php $argv[0] -(r|p) domain url [server_ip] [port]
-r method option: Refresh cache (use stale cache while updating cache)
-p method option: Purge cache (delete cache entry)
domain: required parameter for domain name
url: required parameter for url
server_ip: optional parameter, default is 127.0.0.1
server_port: optional parameter, default is 80
";
exit;
}
if ( $argv[1] == '-p' )
$method = "PURGE";
else if ($argv[1] == '-r' )
$method = "REFRESH";
else
{
echo "ERROR: unknown or missing method option";
exit;
}
$domain = $argv[2];
$url = $argv[3];
$server_ip = ($argc >= 5) ? $argv[4] : '127.0.0.1';
$port = ($argc == 6) ? $argv[5] : 80;

$fp = fsockopen($server_ip, $port, $errno, $errstr, 2);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
$out = "$method $url HTTP/1.0\r\n"
. "Host: $domain\r\n"
. "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

?>
2. I change code from purge_cache_byurl.php (I hide my original IP in this post with: 11.11.11.11 , and domain name with: mywebsite.com ) to folowing:

PHP:
<?php

/****
* purge_cache_byurl
*
* Example: /usr/local/lsws/admin/fcgi-bin/admin_php /usr/local/lsws/admin/misc/purge_cache_byurl.php -r www.mywebsite.com /index.php
*/

if ($argc < 4 || $argc > 6) {
echo "Invalid arguments!\n";
echo "Usage: php $argv[0] -(r|p) mywebsite.com [11.11.11.11] [80]
-r method option: Refresh cache (use stale cache while updating cache)
-p method option: Purge cache (delete cache entry)
domain: mywebsite.com
url: https://www.mywebsite.com
server_ip: 11.11.11.11
server_port: 80
";
exit;
}
if ( $argv[1] == '-p' )
$method = "PURGE";
else if ($argv[1] == '-r' )
$method = "REFRESH";
else
{
echo "ERROR: unknown or missing method option";
exit;
}
$domain = $argv[2];
$url = $argv[3];
$server_ip = ($argc >= 5) ? $argv[4] : '127.0.0.1';
$port = ($argc == 6) ? $argv[5] : 80;

$fp = fsockopen($server_ip, $port, $errno, $errstr, 2);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
$out = "$method $url HTTP/1.0\r\n"
. "Host: $domain\r\n"
. "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

?>
3. When I log to VPS using putty and run comand:
Code:
/usr/local/lsws/admin/fcgi-bin/admin_php /usr/local/lsws/admin/misc/purge_cache_byurl.php -p www.mywebsite.com /
I get:

PHP:
HTTP/1.0 200 OK
Content-Length: 44
Date: Mon, 05 Apr 2021 12:58:41 GMT
Connection: close
4. As far as I can see, cache is purged for homepage but when I go to my site and see home page, old cached home page is still there (cache is not purged).

I hope you can help me. Thanks
 
Last edited:
Top