[Resolved] Purge Cache via PHP

Status
Not open for further replies.
#1
Hey guys,

i want to write a php script to purge the cache on my server.

I used the following instruction:
https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:lscache:purge

I have written the script with valid informations vor ip and url.
The IP is whitelisted in server config.
The .htacces is ready.

But sometimes the scripts throws an 400 - Bad request.
Sometimes everything seems to be fine, but the cache is not purged.

I'm using an Symfony 2 application.
Can anyone tell me what if I have to do, to purge everything under
Code:
www.example.com/api/*
* should be a wildcard
 
Last edited by a moderator:

NiteWave

Administrator
#4
Can anyone tell me what if I have to do, to purge everything under
Code:
www.example.com/api/*
* should be a wildcard
I think wildcard is not supported -- you have to specify the full URL.
however you can delete all cached files by OS command, e.g.:
#rm -rf /lsws-cache-folder/*
 
Last edited by a moderator:
#5
Thank you.
If wildcards are not supported, i like to purge the whole cache.

How can I do that via PHP?
It's a system for managing content of apps - So I have to purge the cache after changes in the system to get them into the app. It's okay to purge the whole chache and not only one url.
If this is not possible i can purge every single url.
How can I do that?
I'm always getting an 400 - Bad request error.

I have created a "hello.php" - It writes only the current timestamp. This is just for testing.
But nothing happen if i purge the chache via php, just a 400 - Bad request is shown.

Is it possible to refresh the cache?
When we purge, we have to fetch the data new - So we don't need a refresh, not a purge.
If it's not possible i would purge the cache and the refresh it by myself.
The scripts looks like it is possible to refresh the cache.
 
Last edited:

NiteWave

Administrator
#6
Is it possible to refresh the cache?
yes. I did test on a litespeed server:
Code:
/usr/local/lsws/admin/misc>php purge_cache_byurl.php
Invalid arguments!
Usage: php purge_cache_byurl.php -(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
-r mean refresh

on the server,
Code:
/usr/local/lsws/admin/misc>php purge_cache_byurl.php -p www.domain.com /         
HTTP/1.0 200 Purged
Date: Wed, 03 Jun 2015 05:48:31 GMT
Server: LiteSpeed
Connection: close
replace above
Code:
www.domain.com
with one domain served by this lsws web server.

can you run above script in command line with success ?
 
Last edited by a moderator:
#7
Hi,

I can run the command and get the same response.

Code:
root@MYSERVER [/usr/local/lsws/admin/misc]# php purge_cache_byurl.php -p my.url.domain.com /
HTTP/1.0 200 Purged
Date: Wed, 03 Jun 2015 09:05:23 GMT
Server: LiteSpeed
Connection: close
But on my test-file "hello.php" which should write the current timestamp nothing happend.
I'm always getting the same timestamp.
 

NiteWave

Administrator
#8
it should work. what's the cache policy ?
and
#curl -I my.url.domain.com/hello.php
always has response header
X-LiteSpeed-Cache: hit ?
 
#9
Code:
HTTP/1.1 200 OK
Date: Wed, 03 Jun 2015 09:28:48 GMT
Server: LiteSpeed
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Transfer-Encoding: chunked
Vary: User-Agent
X-LiteSpeed-Cache: hit
X-Powered-By: PHP/5.4.40
Content-Type: text/html
This is my response-header. I always get a X-LiteSpeed-Cache: hit

I don't what you mean with "cache policy" so here are my settings:

Code:
Cache Storage Settings
Storage Path: /tmp/diskcache

Cache Policy  
Enable Cache: Yes
Max Object Size: 1024000
Cache Expire Time (seconds): 86400
Cache Stale Age (seconds): 10
Cache Request with Query String: Yes
Cache Request with Cookie: Yes
Cache Response with Cookie: Yes
Ignore Request Cache-Control: Yes
Ignore Response Cache-Control: Yes
Enable Private Cache: No
Private Cache Expire Time (seconds): Not Set
 
Last edited:

NiteWave

Administrator
#10
yes, it's the cache policy and looks ok.

so when you run at command line:
root@MYSERVER [/usr/local/lsws/admin/misc]#php purge_cache_byurl.php -p my.url.domain.com /hello.php
the output ?
 
#11
Code:
root@MYSERVER [/usr/local/lsws/admin/misc]# php purge_cache_byurl.php -p my.url.domain.com /hello.php
HTTP/1.0 200 Purged
Date: Wed, 03 Jun 2015 10:34:49 GMT
Server: LiteSpeed
Connection: close
Same thing if i want to refresh:

Code:
root@MYSERVER [/usr/local/lsws/admin/misc]# php purge_cache_byurl.php -r my.url.domain.com /hello.php
HTTP/1.0 200 Purged
Date: Wed, 03 Jun 2015 10:39:17 GMT
Server: LiteSpeed
Connection: close
 
Last edited:

NiteWave

Administrator
#18
confirmed this one doesn't really purge cache:
root@MYSERVER [/usr/local/lsws/admin/misc]# php purge_cache_byurl.php -r my.url.domain.com /hello.php
HTTP/1.0 200 Purged Date: Wed, 03 Jun 2015 10:39:17 GMT Server: LiteSpeed Connection: close
but after adding my.url.domain.com's IP, it has worked!
i.e.,
Code:
#php purge_cache_byurl.php -r my.url.domain.com /hello.php IP-of-my.url.domain.com 80
the syntax is:
Code:
/usr/local/lsws/admin/misc>php purge_cache_byurl.php
Invalid arguments!
Usage: php purge_cache_byurl.php -(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
so server_ip can not be omitted in some cases!

waiting for Klimbo's confirmation message.
 
Status
Not open for further replies.
Top