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.

pepe
  • 9,799
  • 25
  • 110
  • 188

2 Answers2

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;
        }
    }
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
  • 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? – pepe May 18 '12 at 19:39
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

sakibmoon
  • 2,026
  • 3
  • 22
  • 32
Tania Petsouka
  • 1,388
  • 1
  • 9
  • 20