/** * data method * * Retrieve cached data with a fallback to query any object you can access via the class registry. Use in preference * to creating requestAction-only controller functions which are just pass-thrus to existing model methods. Use with * care, the intention is to enable *data caching* not convert your Cake application into a MVC-pull implementation. * * Examples (equivalents) * $recentPosts = MiCache::data('Post', 'recent', array('limit' => 5)); * $recentPosts = MiCache::data('Post', 'find', array('list', array('order' => 'created DESC', 'limit' => 5))); * * @param mixed $name * @param mixed $func * @param array $params array() * @return void * @access public */ public function data($name, $func) { if (MiCache::$setting === null) { MiCache::config(); } if (func_num_args() > 2) { $params = func_get_args(); array_shift($params); array_shift($params); } else { $params = array(); } $cacheKey = MiCache::key(func_get_args()); $return = MiCache::read($cacheKey, MiCache::$setting, false); if ($return) { return unserialize($return); } if (!MiCache::_hasDb()) { return null; } if ($func === 'find') { $params[1]['miCache'] = 'cacheRequest'; } $return = call_user_func_array(array(ClassRegistry::init($name), $func), $params); MiCache::write($cacheKey, $return, MiCache::$setting); return $return; }
/** * init method * * Setup the cache, and write a few settings * * @return void * @access public */ function init($path = null, $params = array()) { if (!$path) { $view =& ClassRegistry::getObject('view'); if ($view) { $path = $view->here; if (!empty($view->webroot)) { $this->webroot = $view->webroot; } if ($view->here == '/') { $path = 'home'; } $params = $view->params; } } if (!empty($params['site'])) { $this->site = $params['site']; } $this->_key = $this->site . '_' . strtolower(Inflector::slug($path)); $params = array_merge(array('cacheConfig' => array('name' => 'Url', 'engine' => 'MiFile', 'duration' => '+1 year', 'prefix' => '', 'path' => 'url/', 'serialize' => true)), (array) $params); MiCache::config($params['cacheConfig']); $this->_cache = MiCache::read($this->_key, 'Url'); $this->_cacheGlobal = MiCache::read('_global', 'Url'); if ($params) { $this->_extraParams = array_intersect_key((array) $params, array('controller' => '', 'plugin' => '', 'action' => '', 'prefix' => '')); } else { $this->_extraParams = array('controller' => '', 'plugin' => '', 'action' => '', 'prefix' => ''); } }