• Resolved Wegwarte

    (@wegwarte)


    Hi, I’ve got a question: I want to use preload and rebuild the cache for all posts and pages automatically at a certain time, e.g. 1pm. How should I setup the plugin?

    1.) I could call “wget -O – dev/null https://xxxxxxx.xx/?action=wpfastestcache&type=preload > preload.log” via crontab at 1pm, but then it will only rebuild the cache for 4 items. So it has to be called repeatedly until the output is “Completed”. How can this be automated? Should I write a Bash script for this and call it via crontab? (The script would call the wget command in a loop until the output is “Completed”.)

    2.) When the output is “Completed”, subsequently calls will not trigger a new rebuild. How can a new rebuild be initiated? Does the cache have to be deleted first? How can this be achieved via wget?

    Or did I miss something and the plugin can to this on its own?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Emre Vona

    (@emrevona)

    you can call every minute in 1pm as below.
    1 * * *

    Thread Starter Wegwarte

    (@wegwarte)

    Thanks; but I know the crontab syntax.

    I have now come to an own preload solution: A bash script (rebuild-cache.bash) which parses the sitemap.xml and gets every post URL in a loop. This way the entire cache for all posts is rebuild within 4 minutes.

    #!/bin/bash
    
    > sitemap.xml
    wget -O- "https://mydomain.com/sitemap.xml" > sitemap.xml 2> wget.err
    
    if [ "$?" -ne 0 ]
    then
    	echo "Error getting sitemap.xml: $(<wget.err)"
    	exit 1
    fi
    
    for urlSubmap in cat sitemap.xml | grep '<loc>' | awk -F'[\<\>]' '{print $3}'
    do
    	echo "Submap: $urlSubmap"
    
    	wget -O- "$urlSubmap" > submap.xml 2> wget.err
    
    	if [ "$?" -ne 0 ]
    	then
    		echo "Error getting $urlSubmap: $(<wget.err)"
    		exit 1
    	fi
    
    	for urlPost in cat submap.xml | grep '<loc>' | awk -F'[\<\>]' '{print $3}'
    	do
    		echo "Post: $urlPost"
    
    		wget -O- "$urlPost" > post.html 2> wget.err
    
    		if [ "$?" -ne 0 ]
    		then
    			echo "Error getting $urlPost: $(<wget.err)"
    		fi
    	done
    
    done

    Another bash script (clear-cache.bash) clears the cache:

    #!/bin/bash
    
    > wget.out
    wget -O- "https://mydomain.com/?action=wpfastestcache&type=clearcacheandminified&token=xyz123" > wget.out 2> wget.err
    
    if [ "$?" -ne 0 ]
    then
    	echo "Error clearing cache: $(<wget.err)"
    	exit 1
    fi
    
    RESULT=$(<wget.out)
    
    if [ "$RESULT" != 'Done' ]
    then
    	echo "Error clearing cache: $RESULT"
    	exit 1
    fi
    
    echo "Cache cleared sucessfully."
    

    Both scripts are executed by cron:

    0 0 * * * ~/clear-cache.bash > clear-cache.log
    1 * * * * ~/rebuild-cache.bash > rebuild-cache.log
    

    At 0:00 pm the cache is cleared, and every hour (0:01, 1:01, …) it is rebuilt.

    Since I don’t have cron on the shared webserver whith the WordPress installation, the scripts are running on a Raspberry Pi (which I’m using as a “Pi-Hole”).

    So far this solution is working for me. If there’s an easier solution using built-in features of WP Fastest Cache to achieve the same, then please tell me…

    Plugin Author Emre Vona

    (@emrevona)

    very nice ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to automatically rebuild preloaded cache at 1pm?’ is closed to new replies.