4

What's the best way to clear the EE cache periodically without having to do it manually on sites?

I know there's the Cron Clean Cache plugin but was looking for something more like a system URL that we could hit on a CURL request in cron.

Or is setting up a template with this plugin the only route?

  • could I ask what your end solution was? – paulcripps Apr 18 '16 at 9:48
5

Like Bhashkar said, that code should work in either a PHP-enabled template or put in a plugin and then put in a template. Then call the template to clear cache.

A more advanced way, and probably the proper way to do it, is to create a simple module, which can use an Action URL. Stephen Lewis wrote a blog post about what Actions are. Basically, an action maps a url like http://domain.com/index.php?ACT=123 (where 123 is the action ID) to any method in your module. You can use it to trigger the cache clearing.

Say you add this method to your mod.my_addon.php file:

public function clear_cache()
{
    $this->EE->functions->clear_caching('all', '', TRUE);
    die('Cache cleared');
}

Then in your upd.my_addon.php file, inside the install() method, you need to add the action. Something like this:

$this->EE->db->insert('actions', array(
    'class'  => 'My_addon',
    'method' => 'clear_cache'
));

Where My_addon is the name of the class in the mod.my_addon.php file and clear_cache refers to the method you want to execute.

You could even add a check in your own clear_cache method for something like a secret key, so URL-hackers can't accidentally (or deliberately) trigger it.

Sounds like something one could learn in a certain workshop. ;)

| improve this answer | |
  • dude - I'm gutted to be missing the workshop! I'd have definitely been there if not for the impending arrival. The action route is what I had in mind given we have other crons to hit other Action URLs. – Steven Grant Mar 15 '13 at 10:16
0

Steven,

You can have the following code within a template (PHP allowed):

$this->EE =& get_instance();
$cache_type = 'all'; // It can be 'page' OR 'tag' OR 'db' OR 'sql' OR 'relationships'
$this->EE->functions->clear_caching($cache_type, '', TRUE);

and schedule template URL call with CRON job. I think, this would be the simplest solution for you.

For EE v2.+

| improve this answer | |
  • meh, I don't like enabling PHP in templates when I don't have to. I'd be as well using the plugin and calling the same template without having PHP enabled. – Steven Grant Mar 15 '13 at 8:14
  • Yes, it would be fine to develop plugin having the code for clearing cache and call within template. – Bhashkar Yadav Mar 15 '13 at 8:22
0

You could use this module to trigger the cache clearing using Action ID https://devot-ee.com/add-ons/beanstalk-web-hook enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.