![]() |
Selectively delete cache helper (too easy?) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Selectively delete cache helper (too easy?) (/showthread.php?tid=26391) |
Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [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'); 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... Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [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); Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [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. Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [eluser]hugle[/eluser] Hi or maybe just: Code: @unlink($server_path . $filename); Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [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. Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [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 Selectively delete cache helper (too easy?) - El Forum - 01-12-2010 [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. |