We've got to maintain a certain level of 'street-cred'.

More Caching in CakePHP

I have a user portal that gathers information from multiple controllers. The logic belongs in those controllers. The portal obtains these viewlets via requestAction() calls. RequestAction is notoriously slow and had no chance of using the view cache - until now.

Further to my caching frenzy, I have a user portal that gathers information from multiple controllers. The logic belongs in those controllers. The portal obtains these viewlets via requestAction() calls. RequestAction is notoriously slow and had no chance of using the view cache - until now.

The modifications are in the dispatcher. A quick check to see if it is an internal call and then if caching is enabled. The code also checks if we want a cached copy but could easily changed to the negative.

The code goes immediately after the function declaration also shown here in dispatcher.php ` function dispatch($url, $additionalParams = array()) {

if (!empty($additionalParams) && !empty($additionalParams['cache'])) {
    // requestAction had no prior chance to get cached version so check here
    if (defined('CACHE_CHECK') && CACHE_CHECK === true) {
        $uri = Router::url($url);
        if (empty($uri)) {
            $uri = setUri();
        }
        if (!isset($TIME_START)) {
            $TIME_START = getMicrotime();
        }
        if (strpos($uri,'/bare/')===false) { // we want to force a bare cache
            $filename = CACHE . 'views' . DS . 'bare_' . convertSlash($uri) . '.php';
        } else {
            $filename = CACHE . 'views' . DS . convertSlash($uri) . '.php';
        }
        if (file_exists($filename)) {
            uses('controller' . DS . 'component', DS . 'view' . DS . 'view');
            $v = null;
            $view = new View($v);
            if ($view->renderCache($filename, $TIME_START, false)) {
                return;
            }
        } elseif(file_exists(CACHE . 'views' . DS . 'bare__' . convertSlash($uri) . '_index.php')) {
            uses('controller' . DS . 'component', DS . 'view' . DS . 'view');
            $v = null;
            $view = new View($v);
            if ($view->renderCache(CACHE . 'views' . DS . convertSlash($uri) . '_index.php', $TIME_START)) {
                return;
            }
        }
    }
} 
`

You will also have to modify the View base class in view.php. Change the renderCache method to accept a third argument `

function renderCache($filename, $timeStart, $dieOnSuccess = true) { 
`

and change the line in renderCache `

die(); 
`

to be `

if ($dieOnSuccess) {
    die();
} else {
    return true;
} 
`

I also added a ` return false; `

just for completeness.