diff --git a/admin-dev/tabs/AdminPerformance.php b/admin-dev/tabs/AdminPerformance.php index 5ae311970..00741d66d 100644 --- a/admin-dev/tabs/AdminPerformance.php +++ b/admin-dev/tabs/AdminPerformance.php @@ -42,9 +42,11 @@ class AdminPerformance extends AdminTab $this->_errors[] = Tools::displayError('Caching system is missing'); else $settings = preg_replace('/define\(\'_PS_CACHING_SYSTEM_\', \'([a-z0-9=\/+-_]+)\'\);/Ui', 'define(\'_PS_CACHING_SYSTEM_\', \''.$caching_system.'\');', $settings); - if ($cache_active AND $caching_system == 'CacheMemcache' AND !extension_loaded('memcache')) + if ($cache_active && $caching_system == 'CacheMemcache' && !extension_loaded('memcache')) $this->_errors[] = Tools::displayError('To use Memcached, you must install the Memcache PECL extension on your server.').' http://www.php.net/manual/en/memcache.installation.php'; - elseif ($cache_active AND $caching_system == 'CacheFs' AND !is_writable(_PS_CACHEFS_DIRECTORY_)) + else if ($cache_active && $caching_system == 'CacheApc' && !extension_loaded('apc')) + $this->_errors[] = Tools::displayError('To use APC cache, you must install the APC PECL extension on your server.').' http://fr.php.net/manual/fr/apc.installation.php'; + else if ($cache_active && $caching_system == 'CacheFs' && !is_writable(_PS_CACHEFS_DIRECTORY_)) $this->_errors[] = Tools::displayError('To use CacheFS the directory').' '.realpath(_PS_CACHEFS_DIRECTORY_).' '.Tools::displayError('must be writable'); if ($caching_system == 'CacheFs') @@ -220,7 +222,9 @@ class AdminPerformance extends AdminTab $warnings = array(); if (!extension_loaded('memcache')) $warnings[] = $this->l('To use Memcached, you must install the Memcache PECL extension on your server.').' http://www.php.net/manual/en/memcache.installation.php'; - if(!is_writable(_PS_CACHEFS_DIRECTORY_)) + if (!extension_loaded('apc')) + $warnings[] = $this->l('To use APC, you must install the APC PECL extension on your server.').' http://fr.php.net/manual/fr/apc.installation.php'; + if (!is_writable(_PS_CACHEFS_DIRECTORY_)) $warnings[] = $this->l('To use CacheFS the directory').' '.realpath(_PS_CACHEFS_DIRECTORY_).' '.$this->l('must be writable'); if ($warnings) @@ -442,6 +446,7 @@ class AdminPerformance extends AdminTab
diff --git a/cache/class_index.php b/cache/class_index.php index 7a6b8ece7..03a8f2a0a 100644 --- a/cache/class_index.php +++ b/cache/class_index.php @@ -234,6 +234,8 @@ 'Zone' => 'override/classes/Zone.php', 'CacheCore' => 'classes/cache/Cache.php', 'Cache' => 'override/classes/cache/Cache.php', + 'CacheApcCore' => 'classes/cache/CacheApc.php', + 'CacheApc' => '', 'CacheFsCore' => 'classes/cache/CacheFs.php', 'CacheFs' => 'override/classes/cache/CacheFs.php', 'CacheMemcacheCore' => 'classes/cache/CacheMemcache.php', diff --git a/classes/cache/Cache.php b/classes/cache/Cache.php index 281cd8715..8f49184d4 100755 --- a/classes/cache/Cache.php +++ b/classes/cache/Cache.php @@ -111,6 +111,8 @@ abstract class CacheCore /** * Clean all cached data + * + * @return bool */ abstract public function flush(); @@ -161,12 +163,6 @@ abstract class CacheCore if (!isset($this->keys[$key])) return false; - if ($this->keys[$key] > 0 && $this->keys[$key] < time()) - { - $this->delete($key); - return false; - } - return $this->_get($key); } @@ -181,12 +177,6 @@ abstract class CacheCore if (!isset($this->keys[$key])) return false; - if ($this->keys[$key] > 0 && $this->keys[$key] < time()) - { - $this->delete($key); - return false; - } - return $this->_exists($key); } @@ -199,6 +189,7 @@ abstract class CacheCore */ public function delete($key) { + // Get list of keys to delete $keys = array(); if ($key == '*') $keys = $this->keys; @@ -213,18 +204,18 @@ abstract class CacheCore } - d($keys); - - if (!isset($this->keys[$key])) - return false; - - if ($this->_delete($key)) + // Delete keys + foreach ($keys as $key) { - unset($this->keys[$key]); - $this->_writeKeys(); - return true; + if (!isset($this->keys[$key])) + continue; + + if ($this->_delete($key)) + unset($this->keys[$key]); } - return false; + + $this->_writeKeys(); + return $keys; } /** diff --git a/classes/cache/CacheApc.php b/classes/cache/CacheApc.php new file mode 100644 index 000000000..eb9d59954 --- /dev/null +++ b/classes/cache/CacheApc.php @@ -0,0 +1,89 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +/** + * This class require PECL APC extension + * + * @since 1.5.0 + */ +class CacheApcCore extends Cache +{ + public function __construct() + { + $this->keys = array(); + $cache_info = apc_cache_info('user'); + foreach ($cache_info['cache_list'] as $entry) + $this->keys[$entry['info']] = $entry['ttl']; + } + + /** + * @see Cache::_set() + */ + protected function _set($key, $value, $ttl = 0) + { + return apc_store($key, $value, $ttl); + } + + /** + * @see Cache::_get() + */ + protected function _get($key) + { + return apc_fetch($key); + } + + /** + * @see Cache::_exists() + */ + protected function _exists($key) + { + return isset($this->keys[$key]); + } + + /** + * @see Cache::_delete() + */ + protected function _delete($key) + { + return apc_delete($key); + } + + /** + * @see Cache::_writeKeys() + */ + protected function _writeKeys() + { + } + + /** + * @see Cache::flush() + */ + public function flush() + { + return apc_clear_cache(); + } +} diff --git a/classes/cache/CacheFs.php b/classes/cache/CacheFs.php index 2360412ee..adc37d45e 100755 --- a/classes/cache/CacheFs.php +++ b/classes/cache/CacheFs.php @@ -32,15 +32,9 @@ class CacheFsCore extends Cache */ protected $depth; - /** - * @var string Cache directory path - */ - protected $path; - protected function __construct() { $this->depth = Db::getInstance()->getValue('SELECT value FROM '._DB_PREFIX_.'configuration WHERE name= \'PS_CACHEFS_DIRECTORY_DEPTH\'', false); - $this->path = _PS_CACHEFS_DIRECTORY_; $keys_filename = $this->getFilename(self::KEYS_NAME); if (file_exists($keys_filename)) @@ -60,6 +54,12 @@ class CacheFsCore extends Cache */ protected function _get($key) { + if ($this->keys[$key] > 0 && $this->keys[$key] < time()) + { + $this->delete($key); + return false; + } + $filename = $this->getFilename($key); if (!file_exists($filename)) { @@ -76,7 +76,13 @@ class CacheFsCore extends Cache */ protected function _exists($key) { - return file_exists($this->getFilename($key)); + if ($this->keys[$key] > 0 && $this->keys[$key] < time()) + { + $this->delete($key); + return false; + } + + return isset($this->keys[$key]) && file_exists($this->getFilename($key)); } /** @@ -104,6 +110,7 @@ class CacheFsCore extends Cache public function flush() { $this->delete('*'); + return true; } /** @@ -111,7 +118,7 @@ class CacheFsCore extends Cache */ public static function deleteCacheDirectory() { - Tools::deleteDirectory($this->path, false); + Tools::deleteDirectory(_PS_CACHEFS_DIRECTORY_, false); } /** @@ -123,7 +130,7 @@ class CacheFsCore extends Cache public static function createCacheDirectories($level_depth, $directory = false) { if (!$directory) - $directory = $this->path; + $directory = _PS_CACHEFS_DIRECTORY_; $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; for ($i = 0; $i < strlen($chars); $i++) { @@ -143,7 +150,7 @@ class CacheFsCore extends Cache */ protected function getFilename($key) { - $path = $this->path; + $path = _PS_CACHEFS_DIRECTORY_; for ($i = 0; $i < $this->depth; $i++) $path .= $key[$i].'/'; diff --git a/classes/cache/CacheMemcache.php b/classes/cache/CacheMemcache.php index f0c76cb2e..e86ddaec9 100755 --- a/classes/cache/CacheMemcache.php +++ b/classes/cache/CacheMemcache.php @@ -25,6 +25,10 @@ * International Registered Trademark & Property of PrestaShop SA */ +/** + * This class require PECL Memcache extension + * + */ class CacheMemcacheCore extends Cache { /** @@ -40,7 +44,16 @@ class CacheMemcacheCore extends Cache public function __construct() { $this->connect(); - $this->keys = $this->memcache->get(self::KEYS_NAME); + + // Get keys (this code come from Doctrine 2 project) + $this->keys = array(); + foreach ($this->memcache->getExtendedStats('slabs') as $server => $slabs) + if (is_array($slabs)) + foreach (array_keys($slabs) as $slab_id) + if ($this->memcache->getExtendedStats('cachedump', (int)$slab_id)) + foreach ($dump as $entries) + foreach ($entries as $entry) + $this->keys[$entry] = 0;; } public function __destruct() @@ -108,9 +121,6 @@ class CacheMemcacheCore extends Cache */ protected function _writeKeys() { - if (!$this->is_connected) - return false; - $this->memcache->set(self::KEYS_NAME, $this->keys, 0, 0); } /** @@ -118,11 +128,9 @@ class CacheMemcacheCore extends Cache */ public function flush() { - if(!$this->is_connected) + if (!$this->is_connected) return false; - if ($this->memcache->flush()) - return $this->_setKeys(); - return false; + return $this->memcache->flush(); } /** diff --git a/override/classes/cache/CacheApc.php b/override/classes/cache/CacheApc.php new file mode 100644 index 000000000..57cc22e5a --- /dev/null +++ b/override/classes/cache/CacheApc.php @@ -0,0 +1,7 @@ +