[*] Classes: refactoring of cache classes

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9191 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2011-10-10 14:45:27 +00:00
parent 31815febc5
commit ce914bd435
5 changed files with 512 additions and 321 deletions
+89 -89
View File
@@ -1,10 +1,10 @@
<?php return array (
'AbstractLoggerCore' => 'classes/AbstractLogger.php',
'AbstractLogger' => 'override/classes/AbstractLogger.php',
'AddressFormatCore' => 'classes/AddressFormat.php',
'AddressFormat' => 'override/classes/AddressFormat.php',
'AddressCore' => 'classes/Address.php',
'Address' => 'override/classes/Address.php',
'AddressFormatCore' => 'classes/AddressFormat.php',
'AddressFormat' => 'override/classes/AddressFormat.php',
'AdminControllerCore' => 'classes/AdminController.php',
'AdminController' => 'override/classes/AdminController.php',
'AdminTabCore' => 'classes/AdminTab.php',
@@ -13,35 +13,29 @@
'Alias' => 'override/classes/Alias.php',
'AttachmentCore' => 'classes/Attachment.php',
'Attachment' => 'override/classes/Attachment.php',
'AttributeGroupCore' => 'classes/AttributeGroup.php',
'AttributeGroup' => 'override/classes/AttributeGroup.php',
'AttributeCore' => 'classes/Attribute.php',
'Attribute' => 'override/classes/Attribute.php',
'AttributeGroupCore' => 'classes/AttributeGroup.php',
'AttributeGroup' => 'override/classes/AttributeGroup.php',
'Autoload' => 'classes/Autoload.php',
'BackupCore' => 'classes/Backup.php',
'Backup' => 'override/classes/Backup.php',
'BlowfishCore' => 'classes/Blowfish.php',
'Blowfish' => 'override/classes/Blowfish.php',
'CacheFsCore' => 'classes/cache/CacheFs.php',
'CacheFs' => 'override/classes/cache/CacheFs.php',
'CacheMemcacheCore' => 'classes/cache/CacheMemcache.php',
'CacheMemcache' => 'override/classes/cache/CacheMemcache.php',
'CacheCore' => 'classes/cache/Cache.php',
'Cache' => 'override/classes/cache/Cache.php',
'CarrierModuleCore' => 'classes/CarrierModule.php',
'CarrierModule' => 'override/classes/CarrierModule.php',
'CMSCore' => 'classes/CMS.php',
'CMS' => 'override/classes/CMS.php',
'CMSCategoryCore' => 'classes/CMSCategory.php',
'CMSCategory' => 'override/classes/CMSCategory.php',
'CarrierCore' => 'classes/Carrier.php',
'Carrier' => 'override/classes/Carrier.php',
'CarrierModuleCore' => 'classes/CarrierModule.php',
'CarrierModule' => 'override/classes/CarrierModule.php',
'CartCore' => 'classes/Cart.php',
'Cart' => 'override/classes/Cart.php',
'CategoryCore' => 'classes/Category.php',
'Category' => 'override/classes/Category.php',
'ChartCore' => 'classes/Chart.php',
'Chart' => 'override/classes/Chart.php',
'CMSCategoryCore' => 'classes/CMSCategory.php',
'CMSCategory' => 'override/classes/CMSCategory.php',
'CMSCore' => 'classes/CMS.php',
'CMS' => 'override/classes/CMS.php',
'CombinationCore' => 'classes/Combination.php',
'Combination' => 'override/classes/Combination.php',
'CompareProductCore' => 'classes/CompareProduct.php',
@@ -58,10 +52,10 @@
'Contact' => 'override/classes/Contact.php',
'ContextCore' => 'classes/Context.php',
'Context' => 'override/classes/Context.php',
'ControllerFactoryCore' => 'classes/ControllerFactory.php',
'ControllerFactory' => 'override/classes/ControllerFactory.php',
'ControllerCore' => 'classes/Controller.php',
'Controller' => 'override/classes/Controller.php',
'ControllerFactoryCore' => 'classes/ControllerFactory.php',
'ControllerFactory' => 'override/classes/ControllerFactory.php',
'CookieCore' => 'classes/Cookie.php',
'Cookie' => 'override/classes/Cookie.php',
'CountryCore' => 'classes/Country.php',
@@ -70,26 +64,16 @@
'County' => 'override/classes/County.php',
'CurrencyCore' => 'classes/Currency.php',
'Currency' => 'override/classes/Currency.php',
'CustomerMessageCore' => 'classes/CustomerMessage.php',
'CustomerMessage' => 'override/classes/CustomerMessage.php',
'CustomerCore' => 'classes/Customer.php',
'Customer' => 'override/classes/Customer.php',
'CustomerMessageCore' => 'classes/CustomerMessage.php',
'CustomerMessage' => 'override/classes/CustomerMessage.php',
'CustomerThreadCore' => 'classes/CustomerThread.php',
'CustomerThread' => 'override/classes/CustomerThread.php',
'CustomizationCore' => 'classes/Customization.php',
'Customization' => 'override/classes/Customization.php',
'DateRangeCore' => 'classes/DateRange.php',
'DateRange' => 'override/classes/DateRange.php',
'DbMySQLiCore' => 'classes/db/DbMySQLi.php',
'DbMySQLi' => 'override/classes/db/DbMySQLi.php',
'DbPDOCore' => 'classes/db/DbPDO.php',
'DbPDO' => 'override/classes/db/DbPDO.php',
'DbCore' => 'classes/db/Db.php',
'Db' => 'override/classes/db/Db.php',
'DbQueryCore' => 'classes/db/DbQuery.php',
'DbQuery' => 'override/classes/db/DbQuery.php',
'MySQLCore' => 'classes/db/MySQL.php',
'MySQL' => 'override/classes/db/MySQL.php',
'DeliveryCore' => 'classes/Delivery.php',
'Delivery' => 'override/classes/Delivery.php',
'DiscountCore' => 'classes/Discount.php',
@@ -98,14 +82,6 @@
'Dispatcher' => 'override/classes/Dispatcher.php',
'EmployeeCore' => 'classes/Employee.php',
'Employee' => 'override/classes/Employee.php',
'PrestashopDatabaseExceptionCore' => 'classes/exception/PrestashopDatabaseException.php',
'PrestashopDatabaseException' => 'override/classes/exception/PrestashopDatabaseException.php',
'PrestashopExceptionCore' => 'classes/exception/PrestashopException.php',
'PrestashopException' => 'override/classes/exception/PrestashopException.php',
'PrestashopModuleExceptionCore' => 'classes/exception/PrestashopModuleException.php',
'PrestashopModuleException' => 'override/classes/exception/PrestashopModuleException.php',
'PrestashopPaymentExceptionCore' => 'classes/exception/PrestashopPaymentException.php',
'PrestashopPaymentException' => 'override/classes/exception/PrestashopPaymentException.php',
'FeatureCore' => 'classes/Feature.php',
'Feature' => 'override/classes/Feature.php',
'FeatureValueCore' => 'classes/FeatureValue.php',
@@ -126,10 +102,10 @@
'Guest' => 'override/classes/Guest.php',
'HelpAccessCore' => 'classes/HelpAccess.php',
'HelpAccess' => 'override/classes/HelpAccess.php',
'HelperListCore' => 'classes/HelperList.php',
'HelperList' => '',
'HelperCore' => 'classes/Helper.php',
'Helper' => 'override/classes/Helper.php',
'HelperListCore' => 'classes/HelperList.php',
'HelperList' => '',
'HookCore' => 'classes/Hook.php',
'Hook' => 'override/classes/Hook.php',
'ImageCore' => 'classes/Image.php',
@@ -156,20 +132,22 @@
'Message' => 'override/classes/Message.php',
'MetaCore' => 'classes/Meta.php',
'Meta' => 'override/classes/Meta.php',
'ModuleGraphEngineCore' => 'classes/ModuleGraphEngine.php',
'ModuleGraphEngine' => 'override/classes/ModuleGraphEngine.php',
'ModuleGraphCore' => 'classes/ModuleGraph.php',
'ModuleGraph' => 'override/classes/ModuleGraph.php',
'ModuleGridEngineCore' => 'classes/ModuleGridEngine.php',
'ModuleGridEngine' => 'override/classes/ModuleGridEngine.php',
'ModuleGridCore' => 'classes/ModuleGrid.php',
'ModuleGrid' => 'override/classes/ModuleGrid.php',
'ModuleCore' => 'classes/Module.php',
'Module' => 'override/classes/Module.php',
'ModuleGraphCore' => 'classes/ModuleGraph.php',
'ModuleGraph' => 'override/classes/ModuleGraph.php',
'ModuleGraphEngineCore' => 'classes/ModuleGraphEngine.php',
'ModuleGraphEngine' => 'override/classes/ModuleGraphEngine.php',
'ModuleGridCore' => 'classes/ModuleGrid.php',
'ModuleGrid' => 'override/classes/ModuleGrid.php',
'ModuleGridEngineCore' => 'classes/ModuleGridEngine.php',
'ModuleGridEngine' => 'override/classes/ModuleGridEngine.php',
'NotificationCore' => 'classes/Notification.php',
'Notification' => 'override/classes/Notification.php',
'ObjectModelCore' => 'classes/ObjectModel.php',
'ObjectModel' => 'override/classes/ObjectModel.php',
'OrderCore' => 'classes/Order.php',
'Order' => 'override/classes/Order.php',
'OrderDetailCore' => 'classes/OrderDetail.php',
'OrderDetail' => 'override/classes/OrderDetail.php',
'OrderDiscountCore' => 'classes/OrderDiscount.php',
@@ -178,8 +156,6 @@
'OrderHistory' => 'override/classes/OrderHistory.php',
'OrderMessageCore' => 'classes/OrderMessage.php',
'OrderMessage' => 'override/classes/OrderMessage.php',
'OrderCore' => 'classes/Order.php',
'Order' => 'override/classes/Order.php',
'OrderReturnCore' => 'classes/OrderReturn.php',
'OrderReturn' => 'override/classes/OrderReturn.php',
'OrderReturnStateCore' => 'classes/OrderReturnState.php',
@@ -188,6 +164,8 @@
'OrderSlip' => 'override/classes/OrderSlip.php',
'OrderStateCore' => 'classes/OrderState.php',
'OrderState' => 'override/classes/OrderState.php',
'PDFCore' => 'classes/PDF.php',
'PDF' => 'override/classes/PDF.php',
'PackCore' => 'classes/Pack.php',
'Pack' => 'override/classes/Pack.php',
'PageCore' => 'classes/Page.php',
@@ -196,12 +174,10 @@
'PaymentCC' => 'override/classes/PaymentCC.php',
'PaymentModuleCore' => 'classes/PaymentModule.php',
'PaymentModule' => 'override/classes/PaymentModule.php',
'PDFCore' => 'classes/PDF.php',
'PDF' => 'override/classes/PDF.php',
'ProductDownloadCore' => 'classes/ProductDownload.php',
'ProductDownload' => 'override/classes/ProductDownload.php',
'ProductCore' => 'classes/Product.php',
'Product' => 'override/classes/Product.php',
'ProductDownloadCore' => 'classes/ProductDownload.php',
'ProductDownload' => 'override/classes/ProductDownload.php',
'ProductSaleCore' => 'classes/ProductSale.php',
'ProductSale' => 'override/classes/ProductSale.php',
'ProfileCore' => 'classes/Profile.php',
@@ -220,34 +196,20 @@
'Rijndael' => 'override/classes/Rijndael.php',
'SceneCore' => 'classes/Scene.php',
'Scene' => 'override/classes/Scene.php',
'SearchEngineCore' => 'classes/SearchEngine.php',
'SearchEngine' => 'override/classes/SearchEngine.php',
'SearchCore' => 'classes/Search.php',
'Search' => 'override/classes/Search.php',
'GroupShopCore' => 'classes/shop/GroupShop.php',
'GroupShop' => 'override/classes/shop/GroupShop.php',
'ShopCore' => 'classes/shop/Shop.php',
'Shop' => 'override/classes/shop/Shop.php',
'ShopUrlCore' => 'classes/shop/ShopUrl.php',
'ShopUrl' => 'override/classes/shop/ShopUrl.php',
'SearchEngineCore' => 'classes/SearchEngine.php',
'SearchEngine' => 'override/classes/SearchEngine.php',
'SpecificPriceCore' => 'classes/SpecificPrice.php',
'SpecificPrice' => 'override/classes/SpecificPrice.php',
'StateCore' => 'classes/State.php',
'State' => 'override/classes/State.php',
'StockManagerFactoryCore' => 'classes/stock/StockManagerFactory.php',
'StockManagerFactory' => '',
'StockManagerInterface' => 'classes/stock/StockManagerInterface.php',
'StockManagerModule' => 'classes/stock/StockManagerModule.php',
'StockManagerCore' => 'classes/stock/StockManager.php',
'StockManager' => '',
'StockCore' => 'classes/Stock.php',
'Stock' => 'override/classes/Stock.php',
'StockMvtCore' => 'classes/StockMvt.php',
'StockMvt' => 'override/classes/StockMvt.php',
'StockMvtReasonCore' => 'classes/StockMvtReason.php',
'StockMvtReason' => 'override/classes/StockMvtReason.php',
'StockCore' => 'classes/Stock.php',
'Stock' => 'override/classes/Stock.php',
'StockWarehouseCore' => 'classes/stock/StockWarehouse.php',
'StockWarehouse' => '',
'StoreCore' => 'classes/Store.php',
'Store' => 'override/classes/Store.php',
'SubDomainCore' => 'classes/SubDomain.php',
@@ -258,21 +220,6 @@
'Tab' => 'override/classes/Tab.php',
'TagCore' => 'classes/Tag.php',
'Tag' => 'override/classes/Tag.php',
'TaxCalculatorCore' => 'classes/tax/TaxCalculator.php',
'TaxCalculator' => 'override/classes/tax/TaxCalculator.php',
'TaxManagerFactoryCore' => 'classes/tax/TaxManagerFactory.php',
'TaxManagerFactory' => 'override/classes/tax/TaxManagerFactory.php',
'TaxManagerInterface' => 'classes/tax/TaxManagerInterface.php',
'TaxManagerModuleCore' => 'classes/tax/TaxManagerModule.php',
'TaxManagerModule' => 'override/classes/tax/TaxManagerModule.php',
'TaxCore' => 'classes/tax/Tax.php',
'Tax' => 'override/classes/tax/Tax.php',
'TaxRuleCore' => 'classes/tax/TaxRule.php',
'TaxRule' => 'override/classes/tax/TaxRule.php',
'TaxRulesGroupCore' => 'classes/tax/TaxRulesGroup.php',
'TaxRulesGroup' => 'override/classes/tax/TaxRulesGroup.php',
'TaxRulesTaxManagerCore' => 'classes/tax/TaxRulesTaxManager.php',
'TaxRulesTaxManager' => 'override/classes/tax/TaxRulesTaxManager.php',
'ThemeCore' => 'classes/Theme.php',
'Theme' => 'override/classes/Theme.php',
'ToolsCore' => 'classes/Tools.php',
@@ -283,6 +230,61 @@
'Upgrader' => 'override/classes/Upgrader.php',
'ValidateCore' => 'classes/Validate.php',
'Validate' => 'override/classes/Validate.php',
'ZoneCore' => 'classes/Zone.php',
'Zone' => 'override/classes/Zone.php',
'CacheCore' => 'classes/cache/Cache.php',
'Cache' => 'override/classes/cache/Cache.php',
'CacheFsCore' => 'classes/cache/CacheFs.php',
'CacheFs' => 'override/classes/cache/CacheFs.php',
'CacheMemcacheCore' => 'classes/cache/CacheMemcache.php',
'CacheMemcache' => 'override/classes/cache/CacheMemcache.php',
'DbCore' => 'classes/db/Db.php',
'Db' => 'override/classes/db/Db.php',
'DbMySQLiCore' => 'classes/db/DbMySQLi.php',
'DbMySQLi' => 'override/classes/db/DbMySQLi.php',
'DbPDOCore' => 'classes/db/DbPDO.php',
'DbPDO' => 'override/classes/db/DbPDO.php',
'DbQueryCore' => 'classes/db/DbQuery.php',
'DbQuery' => 'override/classes/db/DbQuery.php',
'MySQLCore' => 'classes/db/MySQL.php',
'MySQL' => 'override/classes/db/MySQL.php',
'PrestashopDatabaseExceptionCore' => 'classes/exception/PrestashopDatabaseException.php',
'PrestashopDatabaseException' => 'override/classes/exception/PrestashopDatabaseException.php',
'PrestashopExceptionCore' => 'classes/exception/PrestashopException.php',
'PrestashopException' => 'override/classes/exception/PrestashopException.php',
'PrestashopModuleExceptionCore' => 'classes/exception/PrestashopModuleException.php',
'PrestashopModuleException' => 'override/classes/exception/PrestashopModuleException.php',
'PrestashopPaymentExceptionCore' => 'classes/exception/PrestashopPaymentException.php',
'PrestashopPaymentException' => 'override/classes/exception/PrestashopPaymentException.php',
'GroupShopCore' => 'classes/shop/GroupShop.php',
'GroupShop' => 'override/classes/shop/GroupShop.php',
'ShopCore' => 'classes/shop/Shop.php',
'Shop' => 'override/classes/shop/Shop.php',
'ShopUrlCore' => 'classes/shop/ShopUrl.php',
'ShopUrl' => 'override/classes/shop/ShopUrl.php',
'StockManagerCore' => 'classes/stock/StockManager.php',
'StockManager' => '',
'StockManagerFactoryCore' => 'classes/stock/StockManagerFactory.php',
'StockManagerFactory' => '',
'StockManagerInterface' => 'classes/stock/StockManagerInterface.php',
'StockManagerModule' => 'classes/stock/StockManagerModule.php',
'StockWarehouseCore' => 'classes/stock/StockWarehouse.php',
'StockWarehouse' => '',
'TaxCore' => 'classes/tax/Tax.php',
'Tax' => 'override/classes/tax/Tax.php',
'TaxCalculatorCore' => 'classes/tax/TaxCalculator.php',
'TaxCalculator' => 'override/classes/tax/TaxCalculator.php',
'TaxManagerFactoryCore' => 'classes/tax/TaxManagerFactory.php',
'TaxManagerFactory' => 'override/classes/tax/TaxManagerFactory.php',
'TaxManagerInterface' => 'classes/tax/TaxManagerInterface.php',
'TaxManagerModuleCore' => 'classes/tax/TaxManagerModule.php',
'TaxManagerModule' => 'override/classes/tax/TaxManagerModule.php',
'TaxRuleCore' => 'classes/tax/TaxRule.php',
'TaxRule' => 'override/classes/tax/TaxRule.php',
'TaxRulesGroupCore' => 'classes/tax/TaxRulesGroup.php',
'TaxRulesGroup' => 'override/classes/tax/TaxRulesGroup.php',
'TaxRulesTaxManagerCore' => 'classes/tax/TaxRulesTaxManager.php',
'TaxRulesTaxManager' => 'override/classes/tax/TaxRulesTaxManager.php',
'WebserviceExceptionCore' => 'classes/webservice/WebserviceException.php',
'WebserviceException' => 'override/classes/webservice/WebserviceException.php',
'WebserviceKeyCore' => 'classes/webservice/WebserviceKey.php',
@@ -299,8 +301,6 @@
'WebserviceSpecificManagementInterface' => 'classes/webservice/WebserviceSpecificManagementInterface.php',
'WebserviceSpecificManagementSearchCore' => 'classes/webservice/WebserviceSpecificManagementSearch.php',
'WebserviceSpecificManagementSearch' => 'override/classes/webservice/WebserviceSpecificManagementSearch.php',
'ZoneCore' => 'classes/Zone.php',
'Zone' => 'override/classes/Zone.php',
'FB' => 'override/classes/fb.php',
'AdminAddressesControllerCore' => 'controllers/admin/AdminAddressesController.php',
'AdminAddressesController' => '',
+262 -39
View File
@@ -1,6 +1,6 @@
<?php
/*
* 2007-2011 PrestaShop
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
@@ -27,54 +27,277 @@
abstract class CacheCore
{
/** @var Cache */
protected static $_instance;
protected $_keysCached = array();
protected $_tablesCached = array();
protected $_blackList = array('cart',
'cart_discount',
'cart_product',
'connections',
'connections_source',
'connections_page',
'customer',
'customer_group',
'customized_data',
'guest',
'pagenotfound',
'page_viewed');
/**
* Name of keys index
*/
const KEYS_NAME = '__keys__';
/**
* Name of SQL cache index
*/
const SQL_TABLES_NAME = 'tablesCached';
/**
* @var Cache
*/
protected static $instance;
/**
* @var array List all keys of cached data and their associated ttl
*/
protected $keys = array();
/**
* @var array Store list of tables and their associated keys for SQL cache (warning: this var must not be initialized here !)
*/
protected $sql_tables_cached;
/**
* @var array List of blacklisted tables for SQL cache, these tables won't be indexed
*/
protected $blacklist = array(
'cart',
'cart_discount',
'cart_product',
'connections',
'connections_source',
'connections_page',
'customer',
'customer_group',
'customized_data',
'guest',
'pagenotfound',
'page_viewed',
);
/**
* Cache a data
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
abstract protected function _set($key, $value, $ttl = 0);
/**
* Retrieve a cached data by key
*
* @param string $key
* @return mixed
*/
abstract protected function _get($key);
/**
* Check if a data is cached by key
*
* @param string $key
* @return bool
*/
abstract protected function _exists($key);
/**
* Delete a data from the cache by key
*
* @param string $key
* @return bool
*/
abstract protected function _delete($key);
/**
* Write keys index
*/
abstract protected function _writeKeys();
/**
* Clean all cached data
*/
abstract public function flush();
/**
* @return Cache
*/
public static function getInstance()
{
if(!isset(self::$_instance))
{
$caching_system = _PS_CACHING_SYSTEM_;
self::$_instance = new $caching_system();
}
return self::$_instance;
}
protected function __construct()
{
if (!self::$instance)
{
$caching_system = _PS_CACHING_SYSTEM_;
self::$instance = new $caching_system();
}
return self::$instance;
}
/**
* Store a data in cache
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl = 0)
{
if ($this->_set($key, $value))
{
if ($ttl < 0)
$ttl = 0;
$this->keys[$key] = ($ttl == 0) ? 0 : time() + $ttl;
$this->_writeKeys();
return true;
}
return false;
}
/**
* Retrieve a data from cache
*
* @param string $key
* @return mixed
*/
public function get($key)
{
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);
}
/**
* Check if a data is cached
*
* @param string $key
* @return bool
*/
public function exists($key)
{
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);
}
/**
* Delete one or several data from cache (* joker can be used)
* E.g.: delete('*'); delete('my_prefix_*'); delete('my_key_name');
*
* @param string $key
* @return array List of deleted keys
*/
public function delete($key)
{
$keys = array();
if ($key == '*')
$keys = $this->keys;
else if (strpos($key, '*') === false)
$keys = array($key);
else
{
$pattern = str_replace('\\*', '.*', preg_quote($key));
foreach ($this->keys as $k => $ttl)
if (preg_match('#^'.$pattern.'$#', $k))
$keys[] = $k;
}
d($keys);
if (!isset($this->keys[$key]))
return false;
if ($this->_delete($key))
{
unset($this->keys[$key]);
$this->_writeKeys();
return true;
}
return false;
}
/**
* Store a query in cache
*
* @param string $query
* @param array $result
*/
public function setQuery($query, $result)
{
if ($this->isBlacklist($query))
return true;
if (is_null($this->sql_tables_cached))
{
$this->sql_tables_cached = $this->get(self::SQL_TABLES_NAME);
if (!is_array($this->sql_tables_cached))
$this->sql_tables_cached = array();
}
// Store query results in cache if this query is not already cached
$key = md5($query);
if ($this->exists($key))
return true;
$this->set($key, $result);
// Get all table from the query and save them in cache
if (preg_match_all('/('._DB_PREFIX_.'[a-z_-]*)`?.*/i', $query, $res))
foreach($res[1] as $table)
if (!isset($this->sql_tables_cached[$table][$key]))
$this->sql_tables_cached[$table][$key] = true;
$this->set(self::SQL_TABLES_NAME, $this->sql_tables_cached);
}
/**
* Delete a query from cache
*
* @param string $query
*/
public function deleteQuery($query)
{
if (is_null($this->sql_tables_cached))
{
$this->sql_tables_cached = $this->get(self::SQL_TABLES_NAME);
if (!is_array($this->sql_tables_cached))
$this->sql_tables_cached = array();
}
if (preg_match_all('/('._DB_PREFIX_.'[a-z_-]*)`?.*/i', $query, $res))
foreach ($res[1] as $table)
if (isset($this->sql_tables_cached[$table]))
{
foreach (array_keys($this->sql_tables_cached[$table]) as $fs_key)
{
$this->delete($fs_key);
$this->delete($fs_key.'_nrows');
}
unset($this->sql_tables_cached[$table]);
}
$this->set(self::SQL_TABLES_NAME, $this->sql_tables_cached);
}
/**
* Check if a query contain blacklisted tables
*
* @param string $query
* @return bool
*/
protected function isBlacklist($query)
{
foreach ($this->_blackList AS $find)
foreach ($this->blacklist as $find)
if (strpos($query, $find))
return true;
return false;
}
abstract public function get($key);
abstract public function delete($key, $timeout = 0);
abstract public function set($key, $value, $expire = 0);
abstract public function flush();
abstract public function setQuery($query, $result);
abstract public function deleteQuery($query);
}
+74 -103
View File
@@ -25,144 +25,105 @@
* International Registered Trademark & Property of PrestaShop SA
*/
class CacheFsCore extends Cache {
class CacheFsCore extends Cache
{
/**
* @var int Number of subfolders to dispatch cached filenames
*/
protected $depth;
protected $_depth;
/**
* @var string Cache directory path
*/
protected $path;
protected function __construct()
{
parent::__construct();
$this->_init();
$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))
$this->keys = unserialize(file_get_contents($keys_filename));
}
protected function _init()
/**
* @see Cache::_set()
*/
protected function _set($key, $value, $ttl = 0)
{
$this->_depth = Db::getInstance()->getValue('SELECT value FROM '._DB_PREFIX_.'configuration WHERE name=\'PS_CACHEFS_DIRECTORY_DEPTH\'', false);
return $this->_setKeys();
return (@file_put_contents($this->getFilename($key), serialize($value)));
}
public function set($key, $value, $expire = 0)
/**
* @see Cache::_get()
*/
protected function _get($key)
{
$path = $this->getPath();
for ($i = 0; $i < $this->_depth; $i++)
$path .= $key[$i].'/';
if (@file_put_contents($path.$key, serialize($value)))
$filename = $this->getFilename($key);
if (!file_exists($filename))
{
$this->_keysCached[$key] = true;
return $key;
}
return false;
}
public function setNumRows($key, $value, $expire = 0)
{
return $this->set($key.'_nrows', $value, $expire);
}
public function getNumRows($key)
{
return $this->get($key.'_nrows');
}
public function get($key)
{
if (!isset($this->_keysCached[$key]))
return false;
$path = $this->getPath();
for ($i = 0; $i < $this->_depth; $i++)
$path .= $key[$i].'/';
if (!file_exists($path.$key))
{
unset($this->_keysCached[$key]);
unset($this->keys[$key]);
$this->_writeKeys();
return false;
}
$file = file_get_contents($path.$key);
$file = file_get_contents($filename);
return unserialize($file);
}
protected function _setKeys()
/**
* @see Cache::_exists()
*/
protected function _exists($key)
{
if (file_exists($this->getPath().'keysCached'))
{
$file = file_get_contents($this->getPath().'keysCached');
$this->_keysCached = unserialize($file);
}
if (file_exists($this->getPath().'tablesCached'))
{
$file = file_get_contents($this->getPath().'tablesCached');
$this->_tablesCached = unserialize($file);
}
return true;
return file_exists($this->getFilename($key));
}
public function setQuery($query, $result)
/**
* @see Cache::_delete()
*/
protected function _delete($key)
{
$md5_query = md5($query);
if ($this->isBlacklist($query))
$filename = $this->getFilename($key);
if (!file_exists($filename))
return true;
$this->_setKeys();
if (isset($this->_keysCached[$md5_query]))
return true;
$key = $this->set($md5_query, $result);
if (preg_match_all('/('._DB_PREFIX_.'[a-z_-]*)`?.*/i', $query, $res))
foreach($res[1] AS $table)
if(!isset($this->_tablesCached[$table][$key]))
$this->_tablesCached[$table][$key] = true;
$this->_writeKeys();
return unlink($filename);
}
public function delete($key, $timeout = 0)
/**
* @see Cache::_writeKeys()
*/
protected function _writeKeys()
{
$path = $this->getPath();
if (!isset($this->_keysCached[$key]))
return;
for ($i = 0; $i < $this->_depth; $i++)
$path.=$key[$i].'/';
if (!file_exists($path.$key))
return true;
if (!unlink($path.$key))
return false;
unset($this->_keysCached[$key]);
return true;
}
public function deleteQuery($query)
{
$this->_setKeys();
if (preg_match_all('/('._DB_PREFIX_.'[a-z_-]*)`?.*/i', $query, $res))
foreach ($res[1] AS $table)
if (isset($this->_tablesCached[$table]))
{
foreach (array_keys($this->_tablesCached[$table]) AS $fsKey)
{
$this->delete($fsKey);
$this->delete($fsKey.'_nrows');
}
unset($this->_tablesCached[$table]);
}
$this->_writeKeys();
@file_put_contents($this->getFilename(self::KEYS_NAME), serialize($this->keys));
}
/**
* @see Cache::flush()
*/
public function flush()
{
$this->delete('*');
}
private function _writeKeys()
{
@file_put_contents($this->getPath().'keysCached', serialize($this->_keysCached));
@file_put_contents($this->getPath().'tablesCached', serialize($this->_tablesCached));
}
/**
* Delete cache directory
*/
public static function deleteCacheDirectory()
{
Tools::deleteDirectory($this->getPath(), false);
Tools::deleteDirectory($this->path, false);
}
/**
* Create cache directory
*
* @param int $level_depth
* @param string $directory
*/
public static function createCacheDirectories($level_depth, $directory = false)
{
if (!$directory)
$directory = $this->getPath();
$directory = $this->path;
$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < strlen($chars); $i++)
{
@@ -174,8 +135,18 @@ class CacheFsCore extends Cache {
}
}
protected function getPath()
/**
* Transform a key into its absolute path
*
* @param string $key
* @return string
*/
protected function getFilename($key)
{
return (defined('_PS_CACHEFS_DIRECTORY_') ? _PS_CACHEFS_DIRECTORY_ : dirname(__FILE__).'/../cache/cachefs/');
$path = $this->path;
for ($i = 0; $i < $this->depth; $i++)
$path .= $key[$i].'/';
return $path.$key;
}
}
+85 -88
View File
@@ -27,148 +27,145 @@
class CacheMemcacheCore extends Cache
{
protected $_memcacheObj;
protected $_isConnected = false;
/**
* @var Memcache
*/
protected $memcache;
protected function __construct()
/**
* @var bool Connection status
*/
protected $is_connected = false;
public function __construct()
{
parent::__construct();
$this->connect();
$this->keys = $this->memcache->get(self::KEYS_NAME);
}
public function __destruct()
{
$this->close();
}
/**
* Connect to memcache server
*/
public function connect()
{
$this->_memcacheObj = new Memcache();
$this->memcache = new Memcache();
$servers = self::getMemcachedServers();
if (!$servers)
return false;
foreach ($servers AS $server)
$this->_memcacheObj->addServer($server['ip'], $server['port'], $server['weight']);
$this->memcache->addServer($server['ip'], $server['port'], $server['weight']);
$this->_isConnected = true;
return $this->_setKeys();
$this->is_connected = true;
}
public function set($key, $value, $expire = 0)
/**
* @see Cache::_set()
*/
protected function _set($key, $value, $ttl = 0)
{
if (!$this->_isConnected)
if (!$this->is_connected)
return false;
if ($this->_memcacheObj->set($key, $value, 0, $expire))
{
$this->_keysCached[$key] = true;
return $key;
}
return $this->memcache->set($key, $value, 0, $ttl);
}
public function setNumRows($key, $value, $expire = 0)
/**
* @see Cache::_get()
*/
protected function _get($key)
{
return $this->set($key.'_nrows', $value, $expire);
}
public function getNumRows($key)
{
return $this->get($key.'_nrows');
}
public function get($key)
{
if (!isset($this->_keysCached[$key]))
if (!$this->is_connected)
return false;
return $this->_memcacheObj->get($key);
return $this->memcache->get($key);
}
protected function _setKeys()
/**
* @see Cache::_exists()
*/
protected function _exists($key)
{
if (!$this->_isConnected)
if (!$this->is_connected)
return false;
$this->_keysCached = $this->_memcacheObj->get('keysCached');
$this->_tablesCached = $this->_memcacheObj->get('tablesCached');
return true;
return isset($this->keys[$key]);
}
public function setQuery($query, $result)
/**
* @see Cache::_delete()
*/
protected function _delete($key)
{
if (!$this->_isConnected)
if (!$this->is_connected)
return false;
if ($this->isBlacklist($query))
return true;
$md5_query = md5($query);
$this->_setKeys();
if (isset($this->_keysCached[$md5_query]))
return true;
$key = $this->set($md5_query, $result);
if(preg_match_all('/('._DB_PREFIX_.'[a-z_-]*)`?.*/i', $query, $res))
foreach($res[1] AS $table)
if(!isset($this->_tablesCached[$table][$key]))
$this->_tablesCached[$table][$key] = true;
$this->_writeKeys();
return $this->memcache->delete($key);
}
public function delete($key, $timeout = 0)
/**
* @see Cache::_writeKeys()
*/
protected function _writeKeys()
{
if (!$this->_isConnected)
if (!$this->is_connected)
return false;
if (!empty($key) AND $this->_memcacheObj->delete($key, $timeout))
unset($this->_keysCached[$key]);
}
public function deleteQuery($query)
{
if (!$this->_isConnected)
return false;
$this->_setKeys();
if (preg_match_all('/('._DB_PREFIX_.'[a-z_-]*)`?.*/i', $query, $res))
foreach ($res[1] AS $table)
if (isset($this->_tablesCached[$table]))
{
foreach ($this->_tablesCached[$table] AS $memcachedKey => $foo)
{
$this->delete($memcachedKey);
$this->delete($memcachedKey.'_nrows');
}
unset($this->_tablesCached[$table]);
}
$this->_writeKeys();
}
protected function close()
{
if (!$this->_isConnected)
return false;
return $this->_memcacheObj->close();
$this->memcache->set(self::KEYS_NAME, $this->keys, 0, 0);
}
/**
* @see Cache::flush()
*/
public function flush()
{
if(!$this->_isConnected)
if(!$this->is_connected)
return false;
if ($this->_memcacheObj->flush())
if ($this->memcache->flush())
return $this->_setKeys();
return false;
}
private function _writeKeys()
/**
* Close connection to memcache server
*
* @return bool
*/
protected function close()
{
if (!$this->_isConnected)
if (!$this->is_connected)
return false;
$this->_memcacheObj->set('keysCached', $this->_keysCached, 0, 0);
$this->_memcacheObj->set('tablesCached', $this->_tablesCached, 0, 0);
$this->close();
return $this->memcache->close();
}
/**
* Add a memcache server
*
* @param string $ip
* @param int $port
* @param int $weight
*/
public static function addServer($ip, $port, $weight)
{
return Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'memcached_servers (ip, port, weight) VALUES(\''.pSQL($ip).'\', '.(int)$port.', '.(int)$weight.')', false);
return Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'memcached_servers (ip, port, weight) VALUES(\''.pSQL($ip).'\', '.(int)$port.', '.(int)$weight.')', false);
}
/**
* Get list of memcached servers
*
* @return array
*/
public static function getMemcachedServers()
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM '._DB_PREFIX_.'memcached_servers', true, false);
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT * FROM '._DB_PREFIX_.'memcached_servers', true, false);
}
/**
* Delete a memcache server
*
* @param int $id_server
*/
public static function deleteServer($id_server)
{
return Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'memcached_servers WHERE id_memcached_server='.(int)$id_server);
return Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'memcached_servers WHERE id_memcached_server='.(int)$id_server);
}
}
+2 -2
View File
@@ -443,11 +443,11 @@ abstract class DbCore
{
$nrows = $this->_numRows($this->result);
if (_PS_CACHE_ENABLED_)
Cache::getInstance()->setNumRows(md5($this->last_query), $nrows);
Cache::getInstance()->set(md5($this->last_query).'_nrows', $nrows);
return $nrows;
}
else if (_PS_CACHE_ENABLED_ && $this->last_cached)
return Cache::getInstance()->getNumRows(md5($this->last_query));
return Cache::getInstance()->get(md5($this->last_query).'_nrows');
}
/**