[+] Classes: add support of APC cache

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9217 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2011-10-11 10:04:55 +00:00
parent 4d91a31031
commit 48d8dc379f
7 changed files with 152 additions and 43 deletions
+8 -3
View File
@@ -42,9 +42,11 @@ class AdminPerformance extends AdminTab
$this->_errors[] = Tools::displayError('Caching system is missing'); $this->_errors[] = Tools::displayError('Caching system is missing');
else else
$settings = preg_replace('/define\(\'_PS_CACHING_SYSTEM_\', \'([a-z0-9=\/+-_]+)\'\);/Ui', 'define(\'_PS_CACHING_SYSTEM_\', \''.$caching_system.'\');', $settings); $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.').' <a href="http://www.php.net/manual/en/memcache.installation.php">http://www.php.net/manual/en/memcache.installation.php</a>'; $this->_errors[] = Tools::displayError('To use Memcached, you must install the Memcache PECL extension on your server.').' <a href="http://www.php.net/manual/en/memcache.installation.php">http://www.php.net/manual/en/memcache.installation.php</a>';
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.').' <a href="http://fr.php.net/manual/fr/apc.installation.php">http://fr.php.net/manual/fr/apc.installation.php</a>';
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'); $this->_errors[] = Tools::displayError('To use CacheFS the directory').' '.realpath(_PS_CACHEFS_DIRECTORY_).' '.Tools::displayError('must be writable');
if ($caching_system == 'CacheFs') if ($caching_system == 'CacheFs')
@@ -220,7 +222,9 @@ class AdminPerformance extends AdminTab
$warnings = array(); $warnings = array();
if (!extension_loaded('memcache')) if (!extension_loaded('memcache'))
$warnings[] = $this->l('To use Memcached, you must install the Memcache PECL extension on your server.').' <a href="http://www.php.net/manual/en/memcache.installation.php">http://www.php.net/manual/en/memcache.installation.php</a>'; $warnings[] = $this->l('To use Memcached, you must install the Memcache PECL extension on your server.').' <a href="http://www.php.net/manual/en/memcache.installation.php">http://www.php.net/manual/en/memcache.installation.php</a>';
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.').' <a href="http://fr.php.net/manual/fr/apc.installation.php">http://fr.php.net/manual/fr/apc.installation.php</a>';
if (!is_writable(_PS_CACHEFS_DIRECTORY_))
$warnings[] = $this->l('To use CacheFS the directory').' '.realpath(_PS_CACHEFS_DIRECTORY_).' '.$this->l('must be writable'); $warnings[] = $this->l('To use CacheFS the directory').' '.realpath(_PS_CACHEFS_DIRECTORY_).' '.$this->l('must be writable');
if ($warnings) if ($warnings)
@@ -442,6 +446,7 @@ class AdminPerformance extends AdminTab
<div class="margin-form"> <div class="margin-form">
<select name="caching_system" id="caching_system"> <select name="caching_system" id="caching_system">
<option value="CacheMemcache" '.(_PS_CACHING_SYSTEM_ == 'CacheMemcache' ? 'selected="selected"' : '' ).'>'.$this->l('Memcached').'</option> <option value="CacheMemcache" '.(_PS_CACHING_SYSTEM_ == 'CacheMemcache' ? 'selected="selected"' : '' ).'>'.$this->l('Memcached').'</option>
<option value="CacheApc" '.(_PS_CACHING_SYSTEM_ == 'CacheApc' ? 'selected="selected"' : '' ).'>'.$this->l('APC').'</option>
<option value="CacheFs" '.(_PS_CACHING_SYSTEM_ == 'CacheFs' ? 'selected="selected"' : '' ).'>'.$this->l('File System').'</option> <option value="CacheFs" '.(_PS_CACHING_SYSTEM_ == 'CacheFs' ? 'selected="selected"' : '' ).'>'.$this->l('File System').'</option>
</select> </select>
</div> </div>
+2
View File
@@ -234,6 +234,8 @@
'Zone' => 'override/classes/Zone.php', 'Zone' => 'override/classes/Zone.php',
'CacheCore' => 'classes/cache/Cache.php', 'CacheCore' => 'classes/cache/Cache.php',
'Cache' => 'override/classes/cache/Cache.php', 'Cache' => 'override/classes/cache/Cache.php',
'CacheApcCore' => 'classes/cache/CacheApc.php',
'CacheApc' => '',
'CacheFsCore' => 'classes/cache/CacheFs.php', 'CacheFsCore' => 'classes/cache/CacheFs.php',
'CacheFs' => 'override/classes/cache/CacheFs.php', 'CacheFs' => 'override/classes/cache/CacheFs.php',
'CacheMemcacheCore' => 'classes/cache/CacheMemcache.php', 'CacheMemcacheCore' => 'classes/cache/CacheMemcache.php',
+13 -22
View File
@@ -111,6 +111,8 @@ abstract class CacheCore
/** /**
* Clean all cached data * Clean all cached data
*
* @return bool
*/ */
abstract public function flush(); abstract public function flush();
@@ -161,12 +163,6 @@ abstract class CacheCore
if (!isset($this->keys[$key])) if (!isset($this->keys[$key]))
return false; return false;
if ($this->keys[$key] > 0 && $this->keys[$key] < time())
{
$this->delete($key);
return false;
}
return $this->_get($key); return $this->_get($key);
} }
@@ -181,12 +177,6 @@ abstract class CacheCore
if (!isset($this->keys[$key])) if (!isset($this->keys[$key]))
return false; return false;
if ($this->keys[$key] > 0 && $this->keys[$key] < time())
{
$this->delete($key);
return false;
}
return $this->_exists($key); return $this->_exists($key);
} }
@@ -199,6 +189,7 @@ abstract class CacheCore
*/ */
public function delete($key) public function delete($key)
{ {
// Get list of keys to delete
$keys = array(); $keys = array();
if ($key == '*') if ($key == '*')
$keys = $this->keys; $keys = $this->keys;
@@ -213,18 +204,18 @@ abstract class CacheCore
} }
d($keys); // Delete keys
foreach ($keys as $key)
if (!isset($this->keys[$key]))
return false;
if ($this->_delete($key))
{ {
unset($this->keys[$key]); if (!isset($this->keys[$key]))
$this->_writeKeys(); continue;
return true;
if ($this->_delete($key))
unset($this->keys[$key]);
} }
return false;
$this->_writeKeys();
return $keys;
} }
/** /**
+89
View File
@@ -0,0 +1,89 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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();
}
}
+17 -10
View File
@@ -32,15 +32,9 @@ class CacheFsCore extends Cache
*/ */
protected $depth; protected $depth;
/**
* @var string Cache directory path
*/
protected $path;
protected function __construct() protected function __construct()
{ {
$this->depth = Db::getInstance()->getValue('SELECT value FROM '._DB_PREFIX_.'configuration WHERE name= \'PS_CACHEFS_DIRECTORY_DEPTH\'', false); $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); $keys_filename = $this->getFilename(self::KEYS_NAME);
if (file_exists($keys_filename)) if (file_exists($keys_filename))
@@ -60,6 +54,12 @@ class CacheFsCore extends Cache
*/ */
protected function _get($key) protected function _get($key)
{ {
if ($this->keys[$key] > 0 && $this->keys[$key] < time())
{
$this->delete($key);
return false;
}
$filename = $this->getFilename($key); $filename = $this->getFilename($key);
if (!file_exists($filename)) if (!file_exists($filename))
{ {
@@ -76,7 +76,13 @@ class CacheFsCore extends Cache
*/ */
protected function _exists($key) 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() public function flush()
{ {
$this->delete('*'); $this->delete('*');
return true;
} }
/** /**
@@ -111,7 +118,7 @@ class CacheFsCore extends Cache
*/ */
public static function deleteCacheDirectory() 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) public static function createCacheDirectories($level_depth, $directory = false)
{ {
if (!$directory) if (!$directory)
$directory = $this->path; $directory = _PS_CACHEFS_DIRECTORY_;
$chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < strlen($chars); $i++) for ($i = 0; $i < strlen($chars); $i++)
{ {
@@ -143,7 +150,7 @@ class CacheFsCore extends Cache
*/ */
protected function getFilename($key) protected function getFilename($key)
{ {
$path = $this->path; $path = _PS_CACHEFS_DIRECTORY_;
for ($i = 0; $i < $this->depth; $i++) for ($i = 0; $i < $this->depth; $i++)
$path .= $key[$i].'/'; $path .= $key[$i].'/';
+16 -8
View File
@@ -25,6 +25,10 @@
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*/ */
/**
* This class require PECL Memcache extension
*
*/
class CacheMemcacheCore extends Cache class CacheMemcacheCore extends Cache
{ {
/** /**
@@ -40,7 +44,16 @@ class CacheMemcacheCore extends Cache
public function __construct() public function __construct()
{ {
$this->connect(); $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() public function __destruct()
@@ -108,9 +121,6 @@ class CacheMemcacheCore extends Cache
*/ */
protected function _writeKeys() 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() public function flush()
{ {
if(!$this->is_connected) if (!$this->is_connected)
return false; return false;
if ($this->memcache->flush()) return $this->memcache->flush();
return $this->_setKeys();
return false;
} }
/** /**
+7
View File
@@ -0,0 +1,7 @@
<?php
class CacheApc extends CacheApcCore
{
}