Disable page cache based on user logged in?

Is it possible to serve non cached pages to users who are logged in and at the same time have caching enabled for all ‘normal’ visitors?
Changing the caching settings in system.yaml is no use since it is site wide. Temporarily setting the ‘cache_enabled’ option in the page frontmatter header is an option - once I figure out how to do that - but not a very good one since it also disables caching of that page for all other (not logged in) users/visitors.
All hints are greatly appreciated!

If I understand the source code correctly, the main cache check happens in Pages::buildPages(). So somewhere before onBuildPagesInitialized (perhaps right at onPluginsInitialized, I’m not sure when the user data becomes available) you could have a plugin check the user and then disable config.system.cache.enabled for that request. That should do it. I don’t use the login plugin, so it’s hard for me to test.

Thanks for pointing in that direction. As far as I understand the internal workings of Grav, I think that is the way to go. I’ll have a try.

As far as I can see in the Login plugin, the user is indeed loaded at the onPluginsInitialized event. So if your plugin runs at the same event at a priority <1000, you should have access to the user object and can disable the cache at that point.

Perlkönig, your suggestion was correct.
I created a new plugin using the DevTools Plugin as described in the docs. Then, in the function onPluginsInitialized()I added these lines just to test:

        // Get a variable from the plugin configuration
        $text = $this->grav['config']->get('system.cache.enabled');
        dump($text);
        $this->grav['config']->set('system.cache.enabled',false);
        $text = $this->grav['config']->get('system.cache.enabled');
        dump($text);

So, as far as I can tell the cache for the current request is disabled which was my question.
It isn’t the solution though to what I am trying to achieve. But for that I’ll post a different question. Thanks again for your help.

FYI, the next release of Grav has a method to enable/disable the cache directly without setting the config option. This will be the preferred approach going forward.

Great ! Will this method clear the cache and from that moment onwards serve non cached content? Or will it make Grav serve content which is processed by Twig, so effectively bypassing the cache?
For my purpose I need the latter and for logged in users only while non logged in users still get their content from the cache.

it’s a programatic thing, so only is for the scope of the current request. It will act as if cache has been disabled, and things are just processed at runtime. It will not impact the cache at all, so should just work as you describe.

That’s very good to hear. Looking forward to version 1.1.6 !
Thanks.

@Team Grav, can someone please explain how this new option can be used? I’ve searched the docs and tried to find a clue in the sources but no luck so far. The new cache method ‘hash’ (in v1.1.6) is not what rhukster meant, I think. Thank you.