/** * Custom find types, as per Matt Curry's method * * @param string $type * @param array $options * @return mixed array|integer|boolean * @access public * @author Matt Curry * @link http://github.com/mcurry/find */ function find($type, $options = array()) { $method = null; if (is_string($type)) { $method = sprintf('__find%s', Inflector::camelize($type)); } if ($method && method_exists($this, $method)) { $return = $this->{$method}($options); if ($this->query != null) { unset($this->query['paginate']); $query = $this->query; $this->query = null; return $query; } return $return; } if (!empty($options['blacklist'])) { $options['blacklist'] = (array) $options['blacklist']; $options['fields'] = isset($options['fields']) ? $options['fields'] : array_keys($this->schema()); $options['fields'] = array_diff($options['fields'], $options['blacklist']); unset($options['blacklist']); } if (!empty($options['cache'])) { if (!class_exists('MiCache')) { App::import('Vendor', 'mi_cache'); } if (is_int($options['cache'])) { MiCache::config(array('duration' => $options['cache'])); } unset($options['cache']); return MiCache::data($this->alias, 'find', $type, $options); } if (!in_array($type, array_keys($this->_findMethods))) { diebug(array($type, $options)); $calledFrom = debug_backtrace(); CakeLog::write('error', "Unknown method {$this->alias}->{$method} in " . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . ' on line ' . $calledFrom[0]['line']); return false; } $args = func_get_args(); return call_user_func_array(array('parent', 'find'), $args); }
/** * Write directly to the configured cache * * @param mixed $cacheKey * @param mixed $data * @return void * @access public */ public static function write($cacheKey, $data, $setting = null) { if (MiCache::$setting === null) { MiCache::config(); } if (!$setting || !isset(MiCache::$settings[$setting])) { $setting = MiCache::$setting; } $settings = MiCache::$settings[$setting]; $path = dirname($settings['path'] . $settings['prefix'] . $cacheKey); if (MiCache::_createDir($path)) { return Cache::write($cacheKey, serialize($data), $setting); } return false; }
/** * 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' => ''); } }
/** * testObjectCall method * * Check what happens with arbritary object calls * * @return void * @access public */ public function testObjectCall() { Configure::write('Cache.disable', false); App::import('Core', 'HttpSocket'); $Socket = new HttpSocket(); MiCache::delete($Socket, 'get', 'http://ad7six.com'); $key1 = MiCache::key($Socket, 'get', 'http://ad7six.com'); $return = MiCache::data($Socket, 'get', 'http://ad7six.com'); $this->assertTrue(strpos($return, 'AD7six')); $key2 = MiCache::key($Socket, 'get', 'http://ad7six.com'); $this->assertIdentical($key1, $key2, 'Object state is affecting cache key'); $Socket->reset(); $return = MiCache::data($Socket, 'get', 'http://ad7six.com'); $this->assertTrue(strpos($return, 'AD7six')); $this->assertFalse($Socket->response['status']['code'], 'Result not cached, HttpSocket class still made a request'); $config = MiCache::config(); if ($config['mi_cache']['engine'] === 'MiFile') { $this->assertTrue(file_exists($config['mi_cache']['path'] . $key1)); } MiCache::delete($Socket, 'get', 'http://ad7six.com'); if ($config['mi_cache']['engine'] === 'MiFile') { $this->assertFalse(file_exists($config['mi_cache']['path'] . $key1)); } }