Welcome Guest, Not a member yet? Register   Sign In
Caching Pages - great. Now I want to delete 1 cached item.
#1

[eluser]meridimus[/eluser]
Hi,

I'm currently developing some stuff which needs the use of caching. It's great that codeignitor can cache the page automatically. My only problem is how can I fetch the name of the cached page? So, for example if I was caching an RSS feed, when someone posts a new topic I want to delete the cache for the RSS feed so that next time it's called it's forced to re-generate with the new content. Then the cache will not expire until the cache file is missing.

I think this is far more useful than just caching for a period of time, typically once the page has rendered it becomes static until a change is made in the content. When that happens it would be nice if I could locate the particalur page that's cached and only delete that one leaving other caches un-touched.

What do you think? Possible within the CodeIgnitor code? I'm going to take proper look at the code used to cache the pages now to see if there's a way to quickly add the feature.

Let me know what you think.
#2

[eluser]nmweb[/eluser]
I coded a function a while back that did the trick:
Code:
function clearPageCache($uri){
        $path = $this->config->item('cache_path');
           $cache_path = ($path == '') ? BASEPATH.'cache/' : $path;
        
      $cache_path .= md5($uri);
      if(file_exists($cache_path)){
        touch($cache_path);
        unlink($cache_path);
      }
      else{
        return false;
      }
  }
The touch is there because there are some problems with safe mode and user permissions on a file. For the rest I think it should run fine.
#3

[eluser]meridimus[/eluser]
That's great, thanks alot.
#4

[eluser]ocergyNohtna[/eluser]
i am trying to do this manually as opposed to building a function (since i'll hardly EVER have to do this again) and i'm curious if anyone knows if the URI that should be md5'd to get the cache file should be an absolute or relative URI?? furthermore, should the "http://" be included? perhaps the "www." shouldn't be included?? trailing slash at the end?? any thoughts anyone?

*EDIT*
so i've found that it does, in fact, use an absolute URI, but i still have no luck removing my cache file. i am doing a mod_rewrite to remove my index.php file so i thought maybe i should put it into the URI that gets md5'd but still no luck. anyone else have any luck using this function? any ideas as to what i may be missing??
#5

[eluser]beemr[/eluser]
[quote author="nmweb" date="1192639159"]I coded a function a while back that did the trick:[/quote]

Just what I was looking for! In case you're interested, I posted a wiki that uses a version of this to prevent forms from posting to their own cache.
Clear Cache by POST

Thanks again!
#6

[eluser]elvix[/eluser]
@ocergyNohtna: Try building your uri's with CI functions, like site_url('path_you_want'). That way, you're more likely to get the same URI as CI used to cache the page with.

Check the Output library for the exact method CI uses to calculate the URI.

Oh and the problem could be a trailing slash in your URI. Try removing it?
#7

[eluser]ocergyNohtna[/eluser]
@elvix: thanks, pointed me in the right direction. Wink problem resolved.
#8

[eluser]beemr[/eluser]
[quote author="beemr" date="1212196832"]
Just what I was looking for! In case you're interested, I posted a wiki that uses a version of this to prevent forms from posting to their own cache.
Clear Cache by POST
[/quote]

Found a better way to do that, so I removed the clear_page_cache function.

However it was still a useful addition for clearing out pages before sending flashdata, so I put it up in a new wiki at Clear Page Cache

It also includes a cache filepath function called get_cache_URI();
#9

[eluser]murphy2006[/eluser]
Hello,

How is the get_cache_URI(); function used?
I tried echo get_cache_URI('post/1'); but I only get an array.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB