Codeigniter caching - suggestions to refresh after new post
Asked Answered
B

2

5

I have a blog-type app built on CI 2.1.0. When I add

$this->output->cache(5);

to my controller, CI correctly caches the page, and it loads quickly.

My issue is that if someone comments on that post, the comment will not show until the cache expires.

I wonder if anyone has pointers on how to force a cache refresh everytime there is a change to that specific page, or if a post edit is made, etc.

Thanks in advance.

Boulogne answered 18/5, 2012 at 19:29 Comment(0)
S
5

From the CodeIgniter documentation:

If you no longer wish to cache a file you can remove the caching tag and it will no longer be refreshed when it expires. Note: Removing the tag will not delete the cache immediately. It will have to expire normally. If you need to remove it earlier you will need to manually delete it from your cache folder.

For programatically delete the cache file you can use

CodeIgniter Cache Helper

Here this function will delete the cache file

function delete_cache($uri_string)
    {
        $CI =& get_instance();
        $path = $CI->config->item('cache_path');
        $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

        $uri =  $CI->config->item('base_url').
            $CI->config->item('index_page').
            $uri_string;

        $cache_path .= md5($uri);

        if (file_exists($cache_path))
        {
            return unlink($cache_path);
        }
        else
        {
            return TRUE;
        }
    }
Superscribe answered 18/5, 2012 at 19:36 Comment(1)
of course i do not want to manually delete, as i'm not sure even which of the cached files points to the page that was updated - how would one selectively delete that file on update programmatically?Boulogne
S
4

You can extend the output class to enable clear page cache. Take a look here : https://github.com/EllisLab/CodeIgniter/wiki/Clear-Page-Cache

Sag answered 18/5, 2012 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.