Welcome Guest, Not a member yet? Register   Sign In
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...


Messages In This Thread
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 02:57 PM
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 03:03 PM
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 03:04 PM
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 04:17 PM
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 04:35 PM
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 05:13 PM
Selectively delete cache helper (too easy?) - by El Forum - 01-12-2010, 05:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB