Support » Plugin: Autoptimize » CONTRIBUTION: Autoptimize Cache Size Under Control By Schedule Auto-cache Purge

  • Autoptimize does not have its proper cache purging mechanism, and it does display a notice on the administration pages if the cache size surpasses the half a Gigabyte mark. But it does not automatically purge the cache.

    Here is an updated quick solution which can keep the cache size under control without adding any extra cron jobs.

    As an example, I have limited the cache upto 256MB, and you need to add this code inside functions.php file of your activated theme.

    
    # Automatically clear autoptimizeCache if it goes beyond 256MB
    if (class_exists('autoptimizeCache')) {
        $myMaxSize = 256000; # You may change this value to lower like 100000 for 100MB if you have limited server space
        $statArr=autoptimizeCache::stats(); 
        $cacheSize=round($statArr[1]/1024);
        
        if ($cacheSize>$myMaxSize){
           autoptimizeCache::clearall();
           header("Refresh:0"); # Refresh the page so that autoptimize can create new cache files and it does breaks the page after clearall.
        }
    }
    

    Note: Even if you deactivate the plugin, this code will not create any fatal error as it does check if plugin is activated by checking its class_exists

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Frank Goossens

    (@futtta)

    Nice job Rahul, thanks! 🙂

    One should however take into account that a fast growing cache is an indication of other problems, which don’t go away by purging the cache like that (which is why AO does not have an automatic cache clean mechanism).

    frank

    Hi @rahulpragma
    Thanks for this code 🙂 But when I use this on my website, the size of ao cache, will stuck on a random size. One time cache size stuck on 65 mb and didn’t increase further. Then on 80 , now on 230 mb.

    Do you know the solution? Or does the code work fine on your side?

    Thanks

    Hi Amir,

    Sorry, I am not familiar with ao cache but if you are using “w3 Total Cache” or “wp supper cache” then following updated code will help you to flush the cache.

    
    /*-------------------------------*/
    /*  # Automatically clear autoptimizeCache if it goes beyond 256MB
    /*-------------------------------*/
    
    if (class_exists('autoptimizeCache')) {
        $myMaxSize = 256000; # You may change this value to lower like 100000 for 100MB if you have limited server space
        $statArr=autoptimizeCache::stats(); 
        $cacheSize=round($statArr[1]/1024);
        
        if ($cacheSize>$myMaxSize){
           autoptimizeCache::clearall();
            
    	// Clear all W3 Total Cache
    	if( class_exists('W3_Plugin_TotalCacheAdmin') )
    	{
    	    $plugin_totalcacheadmin = & w3_instance('W3_Plugin_TotalCacheAdmin');
    	    $plugin_totalcacheadmin->flush_all();
    	   # echo __('<div class="updated"><p>All <strong>W3 Total Cache</strong> caches successfully emptied.</p></div>');
    	}
    
           header("Refresh:0"); # Refresh the page so that autoptimize can create new cache files and it does break the page after clearall.
        
        }
    }
    

    Hey Rahul, thanks man. By AO, I mean Autoptimize 🙂 Do you know the code for nginx fast cgi? (clearing ao + fast cgi)

    Thank you so much 😉

    Thank you so much for this! I’m using W3 Total Cache. I added the code, and immediately the Autoptimize warning disappeared.

    Hi,

    what about wp super cache, the snippet works fine for w3 total cache

    how to purge AO cache alongside WP Super Cache

    Wont this just create the issue mention with on the plugin page on a regular basis?

    Ie, breaking the cache other plugins have stored?

    My cache goes above 256MB in a few hours.

    Plugin Author Optimizing Matters

    (@optimizingmatters)

    My cache goes above 256MB in a few hours.

    If it were me, I would be looking for the reason for this (identifying what is busting the cache) and I would try to fix that (by disabling “also aggregate inline” CSS and/ or JS or by excluding the specific cache-busting JS or CSS from autoptimization) B-)

    Hey rahulpragma,

    What about wp super cache? The snippet you posted works fine for w3 total cache…

    But, how to purge AO cache alongside WP Super Cache? Can we get a snippet for that?

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘CONTRIBUTION: Autoptimize Cache Size Under Control By Schedule Auto-cache Purge’ is closed to new replies.