Welcome Guest, Not a member yet? Register   Sign In
Caching Templates - Is my approach to creating my own caching mechanism a good one, can anyone suggest a better approach
#1

[eluser]Freeze Dried Pop[/eluser]
Hi guys, fairly new to code igniter just registered to post for the first time, but believe me I won't be a one post wonder, i'm just lucky enough that every question has been answered by the search function up until now!

Just to skip the prelude my question is going to be... "is my approach to creating my own caching mechanism a good one, can anyone suggest a better approach?"

I know that there are already a few caching libraries out there, but none seem to be specific enough for what I want to do.

I want to be able to cache nearly every page on my site, however many pages have certain permissions attached to them present in my user authentication setup. So I can't use the CI caching system which pumps it out before going through the controller etc as i'm going to have to check if the user has permission to see the page first. After that however, I do want to save time getting stuff from the DB and building the html by just outputting a cache.

I also want to be able to cache things in different ways. I'm using different views e.g.

Code:
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
$this->load->view('footer');

Things like header, menu and footer I want to cache globally, so the same cache is used on every page. However some things like content I want to be used specific to the url that i'm on.

I want to use APC cache instead of files, so the organisation isn't that big a deal.

However the code for it is a bit more complicated, i've looked at extending the core classes but this seems like alot of hassle, so instead I thought i'd create a library called "template".

This is a pseudo code so probably isn't perfect (definitely shouldn't be copy and pasted anywhere into CI) but it should give some idea.

Code:
function load_template($viewname, $params = array(), $return = FALSE, $cache_type = FALSE, $cache_ttl = 3600)
    {
        //Cache is dependent on cache type
        if($cache_type != FALSE)
        {
            $cache_key = ($cache_type == 'global') ? md5($viewname) : md5($this->ci->uri->uri_string().$viewname);
            $template = $this->ci->cache->apc->get($cache_key);

            if(!$template) {
                //It's not in the cache better load it and cache it
                $template = $this->ci->load->view($viewname, $params, TRUE);
                $this->ci->cache->apc->save($cache_key, $template, $cache_ttl);
            }

            if($return) {
                return $template;
            } else {
                echo $template;
                return TRUE;
            }
        } else {
            //No caching being done here
            return $this->ci->load->view($viewname, $params, $return;
        }
    }

Then I can just call this function everytime I want to load a bit of my template.

It's alot simpler than most of the templating systems i've seen out there and would do exactly what I need (with some fine tuning of course).

So what do people think? Does this make sense or is there a better way of achieving what I need?
#2

[eluser]Freeze Dried Pop[/eluser]
Just putting some more thought into it, that doesn't make any sense as i'm generating and sending the paramaters to the function, so caching doesn't help anything (DOH!). At least it was just psuedo code, but i'm sure a similar sort of thing will work just as well.




Theme © iAndrew 2016 - Forum software by © MyBB