Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter caching - suggestions to refresh after new post

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.

like image 210
pepe Avatar asked May 18 '12 19:05

pepe


1 Answers

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;
        }
    }
like image 120
Moyed Ansari Avatar answered Sep 26 '22 00:09

Moyed Ansari