Jump to content

Template cache: can it be temporarily globally disabled (e.g., by config setting)?


LMD
 Share

Recommended Posts

Is is possible to temporaily disable template caching on all templates without having to go through each template and manual disable it in admin?

I'd like to keep my development site and live site settings the same (it makes migrations easier), including the template cache settings, but want to disable the _actual_ caching (except for testing) on my dev site.  Is there a config or other setting I can use in just one location (preferably in config-dev.php) that will achieve this?

Also, this is more iof a secondary/wish-list issue, but it would be nice if the cache could be disabled for specific SESSION vars (when not logged-in) as well as GET/POST vars.

 

Link to comment
Share on other sites

If you're logged in you have no template cache, unless you enable it for logged in users. That's enough most of the times. There's also whitelist for POST and GET for template cache. So simply adding "nocache" to the GET field when enabling template cache would let you see non-cached version by simply adding /?nocache to a url.

The only way to disable template cache via a config, the only way would be to make a hook before Page:render() and set the "allowCache" render option to false.

Somewhere in an autoload module

public function init() {
    $this->addHookBefore('Page::render', $this, 'hookRenderPage');
}

Then the method to set the arguments for "allowCache" to false if config disableTemplateCache is true

public function hookRenderPage($event){
    if($this->config->disableTemplateCache){
        $args = $event->arguments(1);
        $args['allowCache'] = false;
        $event->setArgument(1, $args);
    }
}

Now one can add to the config

$config->disableTemplateCache = true;

 

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

Thank you Soma.

Yeah, I know about being logged-in and GET/POST vars, but that wouldn't apply here (need to view the site when logged out too).

However the code you suggested is perfect and does the job.  I modified it slightly so it will work in the ready.php context:

// Code for ready.php context instead of module context
$this->addHookBefore('Page::render', null, 'doNotCache');

function doNotCache($event) {
    if(wire('config')->disableTemplateCache) {
        $args = $event->arguments(1);
        $args['allowCache'] = false;
        $event->setArgument(1, $args);
    }
}

This could easily be expanded to look for sessions/cookies too that aren't related to being logged-in but might necessitate disabling the cache while they are present (either globally or on particular templates/pages).

Link to comment
Share on other sites

@LMD, could you explain the situations when the template cache is not automatically clearing on your dev site when you would want it to? I'm just curious because I have never really used the template cache options and have always used ProCache instead. It would be good to know what things to be aware of if using template cache.

Thinking about situations when one would want the template cache to clear automatically...

1. Page save: cache can be automatically cleared for the saved page and any other pages using options in the template settings.

cache.jpg

2. Changing page sort order: cache is not automatically cleared (according to this thread).

3. GET or POST variables present: cache can be automatically cleared using options in the template settings.

4. Adding/removing/changing fields in the template: not sure, is the cache cleared automatically?

5. Making changes to a template file: not sure, is the cache cleared automatically?

Any other situations when the cache should clear?

 

Link to comment
Share on other sites

@Robin S It has nothing to do with the default behaviour of the template cache -- it is clearing the cache when it should do. At least I've not noticed any issues when logged-in, saving pages or using GET/POST whitelist.

Adding/removing/changing fields - I don't know, but adding fields is at least usually accompanied by modifying pages (to add the new data), which would clear the cache.

However, making changes to the template files does not clear the cache.  Which is one of my issues.  My other issue is laziness ;)

If I'm logged-in (which would prevent caching), I'm not getting the normal visitor ('guest') experience/view.  I just wanted one simple place to disable the cache temporarly for multiple templates, without having to log-in, add a no-cache GET variable (and then have to add it to every URL in dev) or disable it on every template that uses it.

it would also be useful on staging sites, where you may enable caching to show clients how fast it can be (and for testing), but might want to disable it when making changes.

Or even on the live site itself -- globally disable the cache (and then dump it via the module's settings page), make the updates/upgrades while the cache is disabled (preventing visitors from triggering caching), then once the updates are made/tested, enable the cache again.

This is where Soma's solution works a charm - it creates that simple/quick config setting and and only involves a few lines of code in the ready.php file.

  • Like 1
Link to comment
Share on other sites

On 29/09/2016 at 11:01 PM, LMD said:

@Robin S It has nothing to do with the default behaviour of the template cache -- it is clearing the cache when it should do.

Thanks @LMD - I guess what I was driving at is that ideally a cache will clear automatically whenever it becomes out-of-step with the state of the live site, without any manual intervention necessary. If we identify situations where the cache is not automatically cleared when it's desirable that it does clear (changing a template or a template file, maybe other situations too) then maybe new features to deal with those situations could be added to Template Cache / ProCache.

Link to comment
Share on other sites

11 hours ago, Robin S said:

Thanks @LMD - I guess what I was driving at is that ideally a cache will clear automatically whenever it becomes out-of-step with the state of the live site, without any manual intervention necessary. If we identify situations where the cache is not automatically cleared when it's desirable that it does clear (changing a template or a template file, maybe other situations too) then maybe new features to deal with those situations could be added to Template Cache / ProCache.

I'm not sure automatically clearing when a template file is changed is worth the overhead (unless PW already does the steps necessary to achieve it).  The system would have to check the modified time of every template file to decide if it is newer than the cached version _every time_ a page is loaded/rendered.  Yet template updates shouldn't be that frequent once a site is up and running (so it would mostly be a wasted overhead), and when they do happen it would still be easier on the system to just disable the cache completely while those updates are being made.

And what about changes to files that are included into a template, which the PW system won't necessarily 'know' about in order to compare modified dates?

Link to comment
Share on other sites

Caches are simply not meant to be used in development environments (other than testing if they work) and anything else I'd consider mis-usage. I think a sane solution could be a CLI script which watches the template/pw folder for file changes and, on such an event, does trigger the cache to be cleared. 

Link to comment
Share on other sites

13 hours ago, LostKobrakai said:

Caches are simply not meant to be used in development environments (other than testing if they work) and anything else I'd consider mis-usage.

It's not so much that I'm intentionally using caching in a development environment. It's that I have many small (and small-budgeted) sites where it isn't economic, or really necessary, to maintain and sync independent dev/staging/production environments. So it's not unusual for me to make small template changes directly on the live site, where the issue is that changed CSS and JS files may be loaded before the corresponding HTML changes flush through the cache.

 

13 hours ago, LMD said:

I'm not sure automatically clearing when a template file is changed is worth the overhead

Having said the above, I do take the point that it's probably not a situation that justifies any changes to Template Cache or ProCache. It might be possible to avoid a recursive modified time check by detecting changes with an inotify script (thanks to @netcarver for the reminder), and I'm going to explore that out of curiosity, but the simplest and best solution is just to remember to manually clear the cache after making template changes.

 

On 29/09/2016 at 4:59 AM, LMD said:

However the code you suggested is perfect and does the job.  I modified it slightly so it will work in the ready.php context:


// Code for ready.php context instead of module context
$this->addHookBefore('Page::render', null, 'doNotCache');

function doNotCache($event) {
    if(wire('config')->disableTemplateCache) {
        $args = $event->arguments(1);
        $args['allowCache'] = false;
        $event->setArgument(1, $args);
    }
}

 

This by itself does not clear the template cache - you would still need to clear the cache for any pages using templates you have modified. Otherwise when you set...

$config->disableTemplateCache = false;

...after making template changes PW will serve the old cached pages that exist from before template cache was disabled.

  • Like 1
Link to comment
Share on other sites

6 hours ago, Robin S said:

It's that I have many small (and small-budgeted) sites where it isn't economic, or really necessary, to maintain and sync independent dev/staging/production environments.

Then what about installing https://github.com/somatonic/ClearCacheAdmin and using that after you finished editing templates.

Link to comment
Share on other sites

10 minutes ago, LostKobrakai said:

Then what about installing https://github.com/somatonic/ClearCacheAdmin and using that after you finished editing templates.

Yes, that would work. Even without Soma's module there is a checkbox for clearing the cache in the config of the PageRender module.

Or use a simple function/module/snippet to clear just the pages that use the particular templates you worked on, e.g.

function clearCache($tpls) {
    $p = new \ProcessWire\Page();
    $pgs = wire('pages')->find("template=$tpls");
    wire('modules')->PageRender->clearCacheFilePages($pgs, $p);
}

clearCache('basic_page|home');

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...