• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selectively delete cache helper (too easy?)

#1
[eluser]Boztown[/eluser]
Hey everybody,

This is my first post here. I've been toying with CodeIgniter for a few months and I love it. I'm working on a site that's extremely dynamic for logged in users, but quite static for the average browser. Caching is important to me and I wanted something simple, but CI's caching was a little too simple. I basically just wanted a function I could manually call in my code to wipe a specific cache file for a specific view. So for example, if a user comments on a post I would want to clear the cache for the post they commented on so it would show up right away.

I've seen this vaguely discussed, but I'm surprised this isn't more important to more people (maybe everyone's using more complex third party caching systems?). Anyways, I just made a helper that I can pass the URI string into and have it delete to appropriate cache file. This seems too easy, so I'm posting this so you can poke holes in it. Maybe it's a really dangerous thing to do (?).

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('delete_cache'))
{
    function delete_cache($uri_string)
    {
        $CI =& get_instance();
        
        $filename =    md5($CI->config->item('base_url') . $CI->config->item('index_page') . $uri_string);
        
        $CI->firephp->log('Deleting: ' . md5($filename));
        
        $server_path = $CI->config->item('cache_server_dir');

        unlink($server_path . $filename);
    }
}

I forgot to mention, with this helper loaded I could delete the cache likes this:

Code:
delete_cache('post/32');

Of course that '32' wouldn't be hard-coded, but you get the point...

#2
[eluser]helmutbjorg[/eluser]
Seems ok to me... I would suggest putting a check to make sure the file exists before deleting it so you don't get any php warnings.

Change
Code:
unlink($server_path . $filename);

To
Code:
if(file_exists($server_path . $filename)) unlink($server_path . $filename);

#3
[eluser]Boztown[/eluser]
Thanks helmutbjorg. I did see that mentioned in the comments for unlink in the PHP manual. Good call, I should add that.

#4
[eluser]hugle[/eluser]
Hi

or maybe just:
Code:
@unlink($server_path . $filename);

#5
[eluser]helmutbjorg[/eluser]
Simply using an @ symbol stops php from displaying the error on the screen. The error is still triggered and will fill up your logs, etc so it is a better practice to check for the file and then delete it rather than suppress errors.

#6
[eluser]Jamie Rumbelow[/eluser]
Looking good!

One question though... why a helper? Shorter syntax? Helpers are generally used in the view layer only, and as this is something that ought to be called in the controller, maybe a library (extension to CI_Output) would be more appropriate?

Jamie

#7
[eluser]Boztown[/eluser]
Jamie:

That's exactly the kind of feedback I was hoping for. I never really thought of a helper as something to be reserved for use in a view, but that makes sense now that I think about it. I was trying to decide if I should use a helper, a library or if extending the output class (as you said) would make more structural sense.

I think the latter would make the most sense to me, but it almost seems excessive for such a small function.

I'm trying not to sweat things like this because I'm the only one working with this code, but I do like keeping my code clean and sensible so I'm open for suggestions.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.