Welcome Guest, Not a member yet? Register   Sign In
Override page cache functionality


I would like to use the page cache with 

However, it should never use the cache is the user is logged in on site. I can check this with 
if ($session->logged_in)

But it the page is already cached, it won't run any logic. So I don't know how to do a check before the cache is fetched. My first thought was to override the function cache in Common.php, but I don't think that would be enough?

Now I'm trying to override system/Cache/Handlers/FileHandler.php in get() and getItem(). It doesn't seem to work though. I followed the this guide: https://codeigniter.com/user_guide/exten...asses.html

Could be that the cache is not automatically run and I cannot override it this way.

Would it be possible to write my own cache handler?

I also tried emptying the cache on all requests when logged in, but it affects all users of course, and if multiple visitors are on site at the same times it get's messy.

Any ideas for me, on how to do conditional caching? It's ok if it does cache the page as long as the cached page is not returned when logged in.

Thanks Smile

(This post was last modified: 10-24-2020, 05:25 AM by legon614.)

I solved it by overriding cache() in App\Common.php.
If the user is logged in and the user is not under the admin section it should use the dummy handler. They will then never get cache response. The reason for checking if the first URI segment is admin is that I need to clean the cache when an admin edit a page. If it clears the dummy cache nothing happens. It will clean the whole cache which is not optimal but it's ok for me right now.

PHP Code:
if (! function_exists('cache')) {
    function cache(string $key null)
        $session Services::session();
        $useDummyCache $session->logged_in && !in_array('admin'current_url(true)->getSegments());
// Avoid cached responses if logged in
        $cacheConfig = new Cache();
        $cacheConfig->handler $useDummyCache 'dummy' $cacheConfig->handler;

        $cache Services::cache($cacheConfig);

        // No params - return cache object
        if (is_null($key)) {
            return $cache;

        // Still here? Retrieve the value.
        return $cache->get($key);


Theme © iAndrew 2016 - Forum software by © MyBB