requestEvent(), and just as good for a Database Query Result Set... * * version below includes optional fields: $controller, $action, $id, $userid, $repid * most of those fields inherit from the controller if empty... (left the code as simple as possible for easy reconfiguration) * -------- * Can clear at any point with cake helper function: clearCache(); */ class EcacheComponent extends Object { var $duration = '+2 hours'; var $controller, $id, $userid, $repid; // may be used later function startup(&$controller) { $this->controller = &$controller; if (isset($this->controller->id)) { $this->id = $this->controller->id; } if (isset($this->controller->Uid)) { $this->userid = $this->controller->Uid; } if (isset($this->controller->repid)) { $this->repid = $this->controller->repid; } } function ecache($data=null, $controller=null, $action=null, $id=null, $userid=null, $repid=null, $duration=null) { // set values if (empty($controller)) { if (isset($this->params['controller'])) { $controller = $this->controller->params['controller']; } else { $controller = 'unknown'; } } if (empty($action)) { if (isset($this->params['action'])) { $action = $this->controller->params['action']; } else { $action = 'unknown'; } } if (empty($id)) { if (isset($this->id)) { $id = intval($this->id); } elseif (isset($this->controller->id)) { $id = intval($this->controller->id); } else { $id = intval($this->id); } } if (empty($userid)) { if (isset($this->controller->userid)) { $userid = intval($this->controller->userid); } else { $userid = intval($this->userid); } } if (empty($repid)) { if (isset($this->controller->repid)) { $repid = intval($this->controller->repid); } else { $repid = intval($this->repid); } } if (empty($duration)) { if (isset($this->controller->ecache_duration)) { $duration = $this->controller->ecache_duration; } else { $duration = $this->duration; } } if (is_array($data) || is_object($data)) { $data = serialize($data); } // make cache path & filename $filename = $controller.'_'.$action.'_'.$id.'_'.$userid.'_'.$repid; $replace = array('&', '~', '!', ',', ';', ':', '*', '__', '__', '__'); $path_key = str_replace($replace, '_', $filename); $cachePath = 'views'.DS.$path_key.'.php'; // do cache.... if data=null, retrieve... else, write & return $re = cache($cachePath, $data, $duration); // return data (attempt unserialize) $d = @unserialize($re); if ($d!==false && $d!==null) { return $d; } else { return $re; } } // convenience wrapper for "clearCache" function clear() { return clearCache(); } // convenience wrapper for "ecache" function c($data=null, $controller=null, $action=null, $id=null, $userid=null, $repid=null, $duration=null) { return $this->ecache($data, $controller, $action, $id, $userid, $repid, $duration); } } ?>