[eluser]Unknown[/eluser]
Hi everyone!
I'd been searching the forum how to use codeigniter's cache only when there is no session but I didn't find anything useful.
I made this code with hooks that solves my problem and I hope this can be useful for other people.
hooks/MYCache.php
Code:
<?php
class MYCache
{
function display_cache_override()
{
/*
* Do nothing at this stage
*/
}
function post_controller_constructor_cache()
{
$CI = &get;_instance();
$CFG =& load_class('Config');
$URI =& load_class('URI');
$OUT =& load_class('Output');
//Don't use cache if we have session
if(!$CI->session->userdata('current_user_id'))
{
if ($OUT->_display_cache($CFG, $URI) == TRUE)
exit;
}
}
}
config/hooks.php
Code:
$hook['post_controller_constructor'] = array(
'class' => 'MYCache',
'function' => 'post_controller_constructor_cache',
'filename' => 'MYCache.php',
'filepath' => 'hooks',
'params' => array()
);
$hook['cache_override'] = array(
'class' => 'MYCache',
'function' => 'display_cache_override',
'filename' => 'MYCache.php',
'filepath' => 'hooks',
'params' => array()
);
This hooks overrides the CI's cache calling it later, when we already have the $CI object so we can check the session among other things.
You also need to enable hooks in config.php.
With this. you can use CI's cache as usual.
Have fun!
(EDITED)
I forgot to say that you need to be careful calling $this->output->cache(); do it only if you're sure that there is no session!!