debugCache = $this->Application->isDebugMode() && constOn('DBG_CACHE'); $this->_storage = new CacheStorage(); } /** * Adds new value to cache $cache_name and identified by key $key * * @param string $cache_name cache name * @param int $key key name to add to cache * @param mixed $value value of chached record */ function setCache($cache_name, $key, $value) { $this->_storage->set($cache_name, $key, $value); } /** * Returns cached $key value from cache named $cache_name * * @param string $cache_name cache name * @param int $key key name from cache * @return mixed */ function getCache($cache_name, $key) { $ret = $this->_storage->get($cache_name, $key); $this->setStatistics($cache_name, $key, $ret); return $ret; } function setStatistics($cache_name, $key, $found) { if (!$this->debugCache) { return true; } if (!array_key_exists($cache_name, $this->statistics)) { $this->statistics[$cache_name] = Array (); } if (!array_key_exists($key, $this->statistics[$cache_name])) { $this->statistics[$cache_name][$key] = Array (); } $status_key = $found ? 'found' : 'not_found'; if (!isset($this->statistics[$cache_name][$key][$status_key])) { $this->statistics[$cache_name][$key][$status_key] = 0; } $this->statistics[$cache_name][$key][$status_key]++; } function printStatistics() { $cache_size = strlen(serialize($this->_storage)); $this->Application->Debugger->appendHTML('Cache Size: ' . formatSize($cache_size) . ' (' . $cache_size . ')'); foreach ($this->statistics as $cache_name => $cache_data) { foreach ($cache_data as $key => $value) { if (!array_key_exists('found', $value) || $value['found'] == 1) { // remove cached records, that were used only 1 or 2 times unset($this->statistics[$cache_name][$key]); } } } print_pre($this->statistics, 'Cache Statistics:'); } } class CacheStorage extends Params { function set($cache_name, $key, $value) { $cache = parent::Get($cache_name, Array()); $cache[$key] = $value; parent::Set($cache_name, $cache); } function get($cache_name, $key) { $cache = parent::Get($cache_name, Array()); $ret = array_key_exists($key, $cache) ? $cache[$key] : false; return $ret; } } ?>