Support » Plugin: LiteSpeed Cache » Cron job to purge cache

  • Resolved marcorroma

    (@marcorroma)


    Hi, is it possible to create a cron job that clears the page cache of a single page? I currently use a php function, but I would like to create a cronjob. Thanks

Viewing 15 replies - 1 through 15 (of 18 total)
  • Plugin Support qtwrk

    (@qtwrk)

    Hi,

    if you have something like cPanel , you can do

    curl -I -XPURGE https://domain.com/page-to-purge

    or create a php file with content like

    <?php
    require( './wp-load.php' );
    if ( defined(LSCWP_V)) {
      do_action( 'litespeed_purge_url', 'https://domain.com/page-to-purge' );
    }

    save it with name like purge.php

    then curl it with cron job , e.g. curl -I -XGET https://domain.com/purge.php

    Best regards,

    Thread Starter marcorroma

    (@marcorroma)

    Thank you!

    Thread Starter marcorroma

    (@marcorroma)

    Instead, how do I create a php function that caches a page?

    Thread Starter marcorroma

    (@marcorroma)

    Please, can you help me?

    Plugin Support qtwrk

    (@qtwrk)

    No sure what do you mean how to cache it, any page that WP generates will pass through the plugin and be cached whenever possible

    Thread Starter marcorroma

    (@marcorroma)

    I use a php function that clears the cache of some very popular pages.
    I would like to create a function that creates the page cache, without needing the user to navigate once on the page to generate the cache.
    They are very heavy pages, with many posts from various categories, and they are very up to date. So I often have slow page loads.

    If I found a function that, after editing a post, generates the page cache of some pages, it would be perfect.

    Plugin Support qtwrk

    (@qtwrk)

    Hi,

    you can set an aggressive crawler

    or if you want to pre-cache the page right after the purge , you can use PHP to call curl with full Chrome header

    https://stackoverflow.com/questions/21607274/php-curl-request-using-modified-headers

    Best regards,

    Thread Starter marcorroma

    (@marcorroma)

    Could you give me an example to cache a page with a php function?
    I currently use this to clear the cache of some pages when I perform an action. I would like it to be recreated automatically after clearing the cache, without necessarily navigating to that page manually.

    add_action( 'scp_update_menu_order', 'lscwp_purge_order' );
     function lscwp_purge_order() {
     if ( defined( 'LSCWP_V' ) ) {
     do_action('litespeed_purge_url', 'https://www.mywebsite.com/');
     } } 

    I think it is a nice feature for your plugin, and I don’t mean the crawler because I have thousands of posts, but the most visited pages are few and often they are section pages or last posts.

    Plugin Support qtwrk

    (@qtwrk)

    Hi,

    something like this

     if ( defined( 'LSCWP_V' ) ) {
     do_action('litespeed_purge_url', 'https://www.mywebsite.com/');
     }
     
    $url = 'https://www.mywebsite.com/';
    $headers = array( 
            "Host: www.mywebsite.com", 
            "Accept: */*,image/webp"
        );
    
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_GET, true);
    curl_setopt( $ch, CURLOPT_HEADER, true);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36");
    curl_setopt( $ch, CURLOPT_VERBOSE, true);
    curl_setopt( $ch, CURLOPT_ENCODING, "");
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec( $ch );

    if you have enable mobile cache and webp support , you will need to run curl 4 times with different user agent and webp support

    1. desktop + no-webp –> this one can be omitted though
    2. desktop + webp
    3. mobile + no-webp
    4. mobile + webp

    you need to copy paste the user agent and “accept” header from these browsers to mimic the access

    Best regards,

    Thread Starter marcorroma

    (@marcorroma)

    Thanks for support.
    Is it correct that the action is “litespeed_purge_url”?

    Plugin Support qtwrk

    (@qtwrk)

    Hi,

    if you want to purge one single page , yes , that’s correct one to use

    Best regards,

    Thread Starter marcorroma

    (@marcorroma)

    I don’t want to “purge” the page, I want to create the page cache when some pages are deleted.

    Plugin Support qtwrk

    (@qtwrk)

    Hi,

    but your topic title says cron job to purge/clear cache and your first question was how to use PHP to purge cache

    what exactly is it you want to do ?

    Best regards,

    Thread Starter marcorroma

    (@marcorroma)

    I used the command line you recommended to solve the purge cache issue via cronjob.
    Now I need another tip: I want the cache to be created automatically for some (most visited) pages, every time the cache is cleared.
    This increases the speed of my dynamic site, with hundreds of new posts every day, and hundreds of updates to posts.

    Plugin Support qtwrk

    (@qtwrk)

    Hi,

    sadly it can’t be done , not until there is an action from LSCWP to call it

    alternatively , you can create a cron job on your system with curl command , and execute it like every 1 or 5 minutes , then curl will keep visit the pages in few minutes

    so in worse case scenario , after purge , there will only be 5 minutes of cache miss window

    Best regards,

Viewing 15 replies - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.