requestEvent(), and just as good for a Database Query Result Set... * * version below includes optional fields: $controller, $action, $id, $user_id, $groups * 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 icacheComponent extends Object { var $duration = '+2 days'; //may need to change later on. var $controller; var $id; var $user_id; var $groups; var $key; var $duration; function startup(&$controller) { $this->controller = &$controller; if (isset($this->controller->id)) { //This identifies the record, as in /view/46 - user_id, game_id, genre_id... //Utilizing ONLY this will result in "viewer-independent" caching - basically, public, but record-specific caching. $this->id = $this->controller->id; } if (isset($this->controller->current_user['User']['id'])) { //THIS identifies the current user's ID -- IRRELEVANT TO MOST THINGS. //"Viewer-dependent" caching. $this->user_id = $this->controller->current_user['User']['id']; } $this->groups = ''; //Empty, to be tacked onto; will be for categories of users (by access level). //"Censored" caching. //Probably needs to be tailored to your needs. if (isset($this->controller->current_user)) { $this->groups .= 'default'; //In my app, if current_user is set, then they are users and belong to the "default" group. //If current_user wasn't set, they're not logged in, and not part of any group. /*if($this->controller->current_user['Access']['groupA']=="1"){ $this->groups .= 'groupA_'; }*/ //The above line is DarkAuth specific. Uncomment it and replace groupX with any group name, and repeat for each group that is relevant. } } function cache($data=null, $controller=null, $action=null, $id=null, $user_id=null, $groups=null, $key=null, $duration=null) { // set values if (empty($controller)) { if (isset($this->params['controller'])) { $controller = $this->controller->params['controller']; } elseif (isset($this->controller->icache['controller'])) { //Don't know why you'd need this but hey it's the verbose version. $controller = $this->controller->icache['controller']; } else { $controller = 'unknown'; } } if (empty($action)) { if (isset($this->params['action'])) { $action = $this->controller->params['action']; } elseif (isset($this->controller->icache['action'])) { $action = $this->controller->icache['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); } elseif (isset($this->controller->icache['id'])) { $id = $this->controller->icache['id']; } else { $id = '0'; //In a view such as /articles/browse, there is no 'ID', so it is not cached ID-specifically. } } if (empty($user_id)) { if (isset($this->controller->user_id)) { $user_id = intval($this->controller->user_id); } elseif (isset($this->controller->icache['user_id'])){ $user_id = $this->controller->icache['user_id']; } else { $user_id = intval($this->user_id); //Note: intval($this->user_id will just yield '0' if not set already. So it's like above. } } if (empty($groups)) { if (isset($this->controller->icache['groups'])) { $groups = controller->icache['groups']; } else { $groups = '0'; //Same as above... } } if (empty($key)){ if(isset($this->controller->icache['key'])) { $key = $this->controller->icache['key']; } else { $key = '0'; //To keep up with the format of the above, the default is 0 meaning that this is insignificant. } } if (empty($duration)) { if (isset($this->controller->icache['duration'])) { $duration = $this->controller->icache['duration']; } else { $duration = $this->duration; //Not set in controller, default to config'd duration length set above. } } // Write our indentifying string $instance = $controller.'_'.$action.'_'.$id.'_'.$user_id.'_'.$groups.'_'.$key; $instance = str_replace(array('&', '~', '!', ',', ';', ':', '*', '__', '__', '__'), '_', $instance); //Check cache for data stored with this string. Duration, etc., happens automagically for Cache::read() $read = Cache::read($instance); if($read!==false && $read!==null){ //conditions will be met if data has been cached, and ISN'T 'stale'. //Therefore unserialize (look down to see why) & return data. $read = @unserialize($read); return $read; } else { if (is_array($data) || is_object($data)) { $serializedData = serialize($data); //Serialize our data, IE if it's a query, to be stored. } if($data!==null || $data !== false){ Cache::write($instance, $serializedData, $duration); } return $data; //returns the original unserialized data. } } // convenience wrapper for "clearCache" function clear() { return Cache::clearCache(); } // convenience wrapper for "icache" function c($data=null, $controller=null, $action=null, $id=null, $user_id=null, $groups=null, $key=null, $duration=null) { return $this->icache($data, $controller, $action, $id, $user_id, $groups, $key, $duration); } function delete($key = null){ if (empty($key)) { if(isset($this->controller->icache['key'])) { $key = $this->controller->icache['key']; } else { //This is our backup plan. No key set, check for $id. //(If there's no ID it's probably something like "browse" and needs no key.) if (isset($this->id)) { $key = intval($this->id); } elseif (isset($this->controller->id)) { $key = intval($this->controller->id); } elseif (isset($this->controller->icache['id'])) { $key = $this->controller->icache['id']; } else { $key = '0'; } } } if(Cache::delete($key)){ return true; } else { return false; } } //Note: take a peak at the iCache Lean Component if you want a write() function. it's not here because I didn't want to rewrite it :) } ?>