From ce914bd4356daed673509988581a7399d570bb2a Mon Sep 17 00:00:00 2001 From: rMalie Date: Mon, 10 Oct 2011 14:45:27 +0000 Subject: [PATCH] [*] Classes: refactoring of cache classes git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9191 b9a71923-0436-4b27-9f14-aed3839534dd --- cache/class_index.php | 178 +++++++++---------- classes/cache/Cache.php | 301 +++++++++++++++++++++++++++----- classes/cache/CacheFs.php | 177 ++++++++----------- classes/cache/CacheMemcache.php | 173 +++++++++--------- classes/db/Db.php | 4 +- 5 files changed, 512 insertions(+), 321 deletions(-) diff --git a/cache/class_index.php b/cache/class_index.php index 84360539b..7a6b8ece7 100644 --- a/cache/class_index.php +++ b/cache/class_index.php @@ -1,10 +1,10 @@ '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' => '', diff --git a/classes/cache/Cache.php b/classes/cache/Cache.php index c1da488b7..281cd8715 100755 --- a/classes/cache/Cache.php +++ b/classes/cache/Cache.php @@ -1,6 +1,6 @@ _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); - } diff --git a/classes/cache/CacheFs.php b/classes/cache/CacheFs.php index fcb182c9e..2360412ee 100755 --- a/classes/cache/CacheFs.php +++ b/classes/cache/CacheFs.php @@ -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; } } diff --git a/classes/cache/CacheMemcache.php b/classes/cache/CacheMemcache.php index e3b98e343..f0c76cb2e 100755 --- a/classes/cache/CacheMemcache.php +++ b/classes/cache/CacheMemcache.php @@ -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); } } diff --git a/classes/db/Db.php b/classes/db/Db.php index 1278e1bef..475cb9623 100644 --- a/classes/db/Db.php +++ b/classes/db/Db.php @@ -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'); } /**