From a7869a1f06c32978def39d4ab9ff9b2eebe14344 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Mon, 11 Nov 2013 13:41:23 +0100 Subject: [PATCH] [*] CORE : Remove duplicate SQL queries --- classes/Address.php | 38 +++--- classes/Carrier.php | 67 ++++++---- classes/Cart.php | 62 ++++++--- classes/Category.php | 122 +++++++++++------- classes/Currency.php | 14 +- classes/Customer.php | 77 ++++++----- classes/Meta.php | 37 +++--- classes/ObjectModel.php | 2 +- classes/Product.php | 120 +++++++++-------- classes/State.php | 40 ++++-- classes/Tab.php | 22 ++-- classes/Zone.php | 18 ++- classes/db/Db.php | 4 +- classes/module/Module.php | 8 +- classes/order/OrderState.php | 18 ++- classes/shop/Shop.php | 21 +-- classes/tax/TaxRule.php | 10 +- controllers/front/ProductController.php | 2 +- modules/blocktopmenu/blocktopmenu.php | 4 +- modules/blockwishlist/WishList.php | 22 ++-- modules/productcomments/ProductComment.php | 74 ++++++----- .../ProductCommentCriterion.php | 47 ++++--- 22 files changed, 507 insertions(+), 322 deletions(-) diff --git a/classes/Address.php b/classes/Address.php index 10600c1cf..adb7e7d7f 100644 --- a/classes/Address.php +++ b/classes/Address.php @@ -264,13 +264,17 @@ class AddressCore extends ObjectModel if(!isset($id_address) || empty($id_address)) return false; - if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT c.`active` - FROM `'._DB_PREFIX_.'address` a - LEFT JOIN `'._DB_PREFIX_.'country` c ON c.`id_country` = a.`id_country` - WHERE a.`id_address` = '.(int)$id_address)) - return false; - return ($result['active']); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_address; + if (!Cache::isStored($cache_id)) + { + $result = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getvalue(' + SELECT c.`active` + FROM `'._DB_PREFIX_.'address` a + LEFT JOIN `'._DB_PREFIX_.'country` c ON c.`id_country` = a.`id_country` + WHERE a.`id_address` = '.(int)$id_address); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -324,12 +328,17 @@ class AddressCore extends ObjectModel { if (!$id_customer) return false; - - return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' - SELECT `id_address` - FROM `'._DB_PREFIX_.'address` - WHERE `id_customer` = '.(int)$id_customer.' AND `deleted` = 0'.($active ? ' AND `active` = 1' : '') - ); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_customer.'-'.(bool)$active; + if (!Cache::isStored($cache_id)) + { + $result = (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT `id_address` + FROM `'._DB_PREFIX_.'address` + WHERE `id_customer` = '.(int)$id_customer.' AND `deleted` = 0'.($active ? ' AND `active` = 1' : '') + ); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -379,5 +388,4 @@ class AddressCore extends ObjectModel $query->where('id_warehouse = 0'); return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); } -} - +} \ No newline at end of file diff --git a/classes/Carrier.php b/classes/Carrier.php index 6de5899aa..a38ad0692 100644 --- a/classes/Carrier.php +++ b/classes/Carrier.php @@ -277,17 +277,20 @@ class CarrierCore extends ObjectModel public function getMaxDeliveryPriceByWeight($id_zone) { - $sql = 'SELECT d.`price` - FROM `'._DB_PREFIX_.'delivery` d - INNER JOIN `'._DB_PREFIX_.'range_weight` w ON d.`id_range_weight` = w.`id_range_weight` - WHERE d.`id_zone` = '.(int)$id_zone.' - AND d.`id_carrier` = '.(int)$this->id.' - '.Carrier::sqlDeliveryRangeShop('range_weight').' - ORDER BY w.`delimiter2` DESC LIMIT 1'; - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); - if (!isset($result[0]['price'])) - return false; - return $result[0]['price']; + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id.'-'.(int)$id_zone; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT d.`price` + FROM `'._DB_PREFIX_.'delivery` d + INNER JOIN `'._DB_PREFIX_.'range_weight` w ON d.`id_range_weight` = w.`id_range_weight` + WHERE d.`id_zone` = '.(int)$id_zone.' + AND d.`id_carrier` = '.(int)$this->id.' + '.Carrier::sqlDeliveryRangeShop('range_weight').' + ORDER BY w.`delimiter2` DESC'; + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -357,17 +360,20 @@ class CarrierCore extends ObjectModel public function getMaxDeliveryPriceByPrice($id_zone) { - $sql = 'SELECT d.`price` - FROM `'._DB_PREFIX_.'delivery` d - INNER JOIN `'._DB_PREFIX_.'range_price` r ON d.`id_range_price` = r.`id_range_price` - WHERE d.`id_zone` = '.(int)$id_zone.' - AND d.`id_carrier` = '.(int)$this->id.' - '.Carrier::sqlDeliveryRangeShop('range_price').' - ORDER BY r.`delimiter2` DESC LIMIT 1'; - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); - if (!isset($result[0]['price'])) - return false; - return $result[0]['price']; + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id.'-'.(int)$id_zone; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT d.`price` + FROM `'._DB_PREFIX_.'delivery` d + INNER JOIN `'._DB_PREFIX_.'range_price` r ON d.`id_range_price` = r.`id_range_price` + WHERE d.`id_zone` = '.(int)$id_zone.' + AND d.`id_carrier` = '.(int)$this->id.' + '.Carrier::sqlDeliveryRangeShop('range_price').' + ORDER BY r.`delimiter2` DESC'; + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -449,7 +455,13 @@ class CarrierCore extends ObjectModel GROUP BY c.`id_carrier` ORDER BY c.`position` ASC'; - $carriers = Db::getInstance()->executeS($sql); + $cache_id = __CLASS__.__FUNCTION__.md5($sql); + if (!Cache::isStored($cache_id)) + { + $carriers = Db::getInstance()->executeS($sql); + Cache::store($cache_id, $carriers); + } + $carriers = Cache::retrieve($cache_id); if (is_array($carriers) && count($carriers)) { @@ -1199,7 +1211,14 @@ class CarrierCore extends ObjectModel $query->where('pc.id_product = '.(int)$product->id); $query->where('pc.id_shop = '.(int)$id_shop); - $carriers_for_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); + $cache_id = __CLASS__.__FUNCTION__.(int)$product->id.'-'.(int)$id_shop; + if (!Cache::isStored($cache_id)) + { + $carriers_for_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); + Cache::store($cache_id, $carriers_for_product); + } + $carriers_for_product = Cache::retrieve($cache_id); + $carrier_list = array(); if (!empty($carriers_for_product)) { diff --git a/classes/Cart.php b/classes/Cart.php index f38015171..238138699 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -325,7 +325,7 @@ class CartCore extends ObjectModel if (!CartRule::isFeatureActive() || !$this->id) return array(); - $cache_key = 'Cart::getCartRules'.$this->id.'-'.$filter; + $cache_key = __CLASS__.__FUNCTION__.$this->id.'-'.$filter; if (!Cache::isStored($cache_key)) { $result = Db::getInstance()->executeS(' @@ -632,14 +632,19 @@ class CartCore extends ObjectModel if (!isset($row['pai_id_image']) || $row['pai_id_image'] == 0) { - $row2 = Db::getInstance()->getRow(' - SELECT image_shop.`id_image` id_image, il.`legend` - FROM `'._DB_PREFIX_.'image` i - JOIN `'._DB_PREFIX_.'image_shop` image_shop ON (i.id_image = image_shop.id_image AND image_shop.cover=1 AND image_shop.id_shop='.(int)$row['id_shop'].') - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$this->id_lang.') - WHERE i.`id_product` = '.(int)$row['id_product'].' AND image_shop.`cover` = 1' - ); - + $cache_id = __CLASS__.__FUNCTION__.'-pai_id_image-'.(int)$row['id_product'].'-'.(int)$this->id_lang.'-'.(int)$row['id_shop']; + if (!Cache::isStored($cache_id)) + { + $row2 = Db::getInstance()->getRow(' + SELECT image_shop.`id_image` id_image, il.`legend` + FROM `'._DB_PREFIX_.'image` i + JOIN `'._DB_PREFIX_.'image_shop` image_shop ON (i.id_image = image_shop.id_image AND image_shop.cover=1 AND image_shop.id_shop='.(int)$row['id_shop'].') + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$this->id_lang.') + WHERE i.`id_product` = '.(int)$row['id_product'].' AND image_shop.`cover` = 122' + ); + Cache::store($cache_id, $row2); + } + $row2 = Cache::retrieve($cache_id); if (!$row2) $row2 = array('id_image' => false, 'legend' => false); else @@ -727,7 +732,7 @@ class CartCore extends ObjectModel * * @result integer Products quantity */ - public function nbProducts() + public function nbProducts() { if (!$this->id) return 0; @@ -1094,7 +1099,13 @@ class CartCore extends ObjectModel */ public function orderExists() { - return (bool)Db::getInstance()->getValue('SELECT count(*) FROM `'._DB_PREFIX_.'orders` WHERE `id_cart` = '.(int)$this->id); + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id; + if (!Cache::isStored($cache_id)) + { + $result = (bool)Db::getInstance()->getValue('SELECT count(*) FROM `'._DB_PREFIX_.'orders` WHERE `id_cart` = '.(int)$this->id); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -2277,18 +2288,23 @@ class CartCore extends ObjectModel public function getAddressCollection() { $collection = array(); - $result = Db::getInstance()->executeS( - 'SELECT DISTINCT `id_address_delivery` - FROM `'._DB_PREFIX_.'cart_product` - WHERE id_cart = '.(int)$this->id - ); + $cache_id = 'Cart::getAddressCollection'.(int)$this->id; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance()->executeS( + 'SELECT DISTINCT `id_address_delivery` + FROM `'._DB_PREFIX_.'cart_product` + WHERE id_cart = '.(int)$this->id + ); + Cache::store($cache_id, $result); + } + $result = Cache::retrieve($cache_id); $result[] = array('id_address_delivery' => (int)$this->id_address_delivery); foreach ($result as $row) if ((int)$row['id_address_delivery'] != 0) $collection[(int)$row['id_address_delivery']] = new Address((int)$row['id_address_delivery']); - return $collection; } @@ -3466,10 +3482,14 @@ class CartCore extends ObjectModel ) WHERE `id_cart` = '.(int)$this->id.' '.(Configuration::get('PS_ALLOW_MULTISHIPPING') ? ' AND `id_shop` = '.(int)$this->id_shop : ''); - - $result = Db::getInstance()->execute($sql); - if ($result) - $emptyCache = true; + + $cache_id = 'Cart::setNoMultishipping'.(int)$this->id.'-'.(int)$this->id_shop; + if (!Cache::isStored($cache_id)) + { + if ($result = (bool)Db::getInstance()->execute($sql)) + $emptyCache = true; + Cache::store($cache_id, $result); + } if (Customization::isFeatureActive()) Db::getInstance()->execute(' diff --git a/classes/Category.php b/classes/Category.php index f787cce56..a7da52113 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -666,9 +666,9 @@ class CategoryCore extends ObjectModel * @param boolean $active return only active categories * @return array categories */ - public static function getHomeCategories($id_lang, $active = true) + public static function getHomeCategories($id_lang, $active = true, $id_shop = false) { - return self::getChildren(Configuration::get('PS_HOME_CATEGORY'), $id_lang, $active); + return self::getChildren(Configuration::get('PS_HOME_CATEGORY'), $id_lang, $active, $id_shop); } public static function getRootCategory($id_lang = null, Shop $shop = null) @@ -704,16 +704,22 @@ class CategoryCore extends ObjectModel if (!Validate::isBool($active)) die(Tools::displayError()); - $query = 'SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, category_shop.`id_shop` - FROM `'._DB_PREFIX_.'category` c - LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').') - '.Shop::addSqlAssociation('category', 'c').' - WHERE `id_lang` = '.(int)$id_lang.' - AND c.`id_parent` = '.(int)$id_parent.' - '.($active ? 'AND `active` = 1' : '').' - GROUP BY c.`id_category` - ORDER BY category_shop.`position` ASC'; - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_parent.'-'.(int)$id_lang.'-'.(bool)$active.'-'.(int)$id_shop; + if (!Cache::isStored($cache_id)) + { + $query = 'SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, category_shop.`id_shop` + FROM `'._DB_PREFIX_.'category` c + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').') + '.Shop::addSqlAssociation('category', 'c').' + WHERE `id_lang` = '.(int)$id_lang.' + AND c.`id_parent` = '.(int)$id_parent.' + '.($active ? 'AND `active` = 1' : '').' + GROUP BY c.`id_category` + ORDER BY category_shop.`position` ASC'; + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -1008,14 +1014,20 @@ class CategoryCore extends ObjectModel public function getGroups() { $groups = array(); - $result = Db::getInstance()->executeS(' - SELECT cg.`id_group` - FROM '._DB_PREFIX_.'category_group cg - WHERE cg.`id_category` = '.(int)$this->id - ); - foreach ($result as $group) - $groups[] = $group['id_group']; - return $groups; + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance()->executeS(' + SELECT cg.`id_group` + FROM '._DB_PREFIX_.'category_group cg + WHERE cg.`id_category` = '.(int)$this->id + ); + $groups = array(); + foreach ($result as $group) + $groups[] = $group['id_group']; + Cache::store($cache_id, $groups); + } + return Cache::retrieve($cache_id); } public function addGroupsIfNoExist($id_group) @@ -1036,24 +1048,23 @@ class CategoryCore extends ObjectModel */ public function checkAccess($id_customer) { - if (!$id_customer) + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id.'-'.$id_customer.(!$id_customer ? '-'.(int)Group::getCurrent()->id : ''); + if (!Cache::isStored($cache_id)) { - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + if (!$id_customer) + $result = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT ctg.`id_group` FROM '._DB_PREFIX_.'category_group ctg - WHERE ctg.`id_category` = '.(int)$this->id.' AND ctg.`id_group` = '.(int)Group::getCurrent()->id.' - '); - } else { - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + WHERE ctg.`id_category` = '.(int)$this->id.' AND ctg.`id_group` = '.(int)Group::getCurrent()->id); + else + $result = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT ctg.`id_group` FROM '._DB_PREFIX_.'category_group ctg INNER JOIN '._DB_PREFIX_.'customer_group cg on (cg.`id_group` = ctg.`id_group` AND cg.`id_customer` = '.(int)$id_customer.') - WHERE ctg.`id_category` = '.(int)$this->id - ); + WHERE ctg.`id_category` = '.(int)$this->id); + Cache::store($cache_id, $result); } - if ($result && isset($result['id_group']) && $result['id_group']) - return true; - return false; + return Cache::retrieve($cache_id); } /** @@ -1199,12 +1210,16 @@ class CategoryCore extends ObjectModel */ public static function getInterval($id) { - $sql = 'SELECT nleft, nright, level_depth - FROM '._DB_PREFIX_.'category - WHERE id_category = '.(int)$id; - if (!$result = Db::getInstance()->getRow($sql)) - return false; - return $result; + $cache_id = __CLASS__.__FUNCTION__.(int)$id; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT nleft, nright, level_depth + FROM '._DB_PREFIX_.'category + WHERE id_category = '.(int)$id; + $result = Db::getInstance()->getRow($sql); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -1386,11 +1401,17 @@ class CategoryCore extends ObjectModel public static function getCategoriesWithoutParent() { - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT DISTINCT c.* - FROM `'._DB_PREFIX_.'category` c - LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)Context::getContext()->language->id.') - WHERE `level_depth` = 1'); + $cache_id = __CLASS__.__FUNCTION__.(int)Context::getContext()->language->id; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT DISTINCT c.* + FROM `'._DB_PREFIX_.'category` c + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)Context::getContext()->language->id.') + WHERE `level_depth` = 1'); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } public function isRootCategoryForAShop() @@ -1409,12 +1430,17 @@ class CategoryCore extends ObjectModel public static function getTopCategory($id_lang = null) { if (is_null($id_lang)) - $id_lang = Context::getContext()->language->id; - $id_category = Db::getInstance()->getValue(' - SELECT `id_category` - FROM `'._DB_PREFIX_.'category` - WHERE `id_parent` = 0'); - return new Category($id_category, $id_lang); + $id_lang = (int)Context::getContext()->language->id; + $cache_id = __CLASS__.__FUNCTION__.(int)$id_lang; + if (!Cache::isStored($cache_id)) + { + $id_category = (int)Db::getInstance()->getValue(' + SELECT `id_category` + FROM `'._DB_PREFIX_.'category` + WHERE `id_parent` = 0'); + Cache::store($cache_id, new Category($id_category, $id_lang)); + } + return Cache::retrieve($cache_id); } public function addPosition($position, $id_shop = null) diff --git a/classes/Currency.php b/classes/Currency.php index 3280ec189..f37d47bcd 100644 --- a/classes/Currency.php +++ b/classes/Currency.php @@ -296,10 +296,16 @@ class CurrencyCore extends ObjectModel */ public static function getIdByIsoCode($iso_code, $id_shop = 0) { - $query = Currency::getIdByQuery($id_shop); - $query->where('iso_code = \''.pSQL($iso_code).'\''); - - return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query->build()); + $cache_id = 'Currency::getIdByIsoCode'.pSQL($iso_code).'-'.(int)$id_shop; + if (!Cache::isStored($cache_id)) + { + $query = Currency::getIdByQuery($id_shop); + $query->where('iso_code = \''.pSQL($iso_code).'\''); + + $result = (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query->build()); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/Customer.php b/classes/Customer.php index 431d217e6..4c3c78ed3 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -362,15 +362,18 @@ class CustomerCore extends ObjectModel { if (!Validate::isUnsignedId($id_customer)) return true; - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT `id_customer` - FROM `'._DB_PREFIX_.'customer` - WHERE `id_customer` = \''.(int)$id_customer.'\' - AND active = 1 - AND `deleted` = 0'); - if (isset($result['id_customer'])) - return false; - return true; + $cache_id = __CLASS__.__FUNCTION__.(int)$id_customer; + if (!Cache::isStored($cache_id)) + { + $result = (bool)!Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + SELECT `id_customer` + FROM `'._DB_PREFIX_.'customer` + WHERE `id_customer` = \''.(int)$id_customer.'\' + AND active = 1 + AND `deleted` = 0'); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -413,7 +416,7 @@ class CustomerCore extends ObjectModel public static function customerHasAddress($id_customer, $id_address) { $key = (int)$id_customer.'-'.(int)$id_address; - if (!array_key_exists($id_address, self::$_customerHasAddress)) + if (!array_key_exists($key, self::$_customerHasAddress)) { self::$_customerHasAddress[$key] = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_address` @@ -439,15 +442,22 @@ class CustomerCore extends ObjectModel */ public function getAddresses($id_lang) { - $sql = 'SELECT DISTINCT a.*, cl.`name` AS country, s.name AS state, s.iso_code AS state_iso - FROM `'._DB_PREFIX_.'address` a - LEFT JOIN `'._DB_PREFIX_.'country` c ON (a.`id_country` = c.`id_country`) - LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`) - LEFT JOIN `'._DB_PREFIX_.'state` s ON (s.`id_state` = a.`id_state`) - '.(Context::getContext()->shop->getGroup()->share_order ? '' : Shop::addSqlAssociation('country', 'c')).' - WHERE `id_lang` = '.(int)$id_lang.' AND `id_customer` = '.(int)$this->id.' AND a.`deleted` = 0'; + $share_order = (bool)Context::getContext()->shop->getGroup()->share_order; + $cache_id = 'Customer::getAddresses'.(int)$this->id.'-'.(int)$id_lang.'-'.$share_order; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT DISTINCT a.*, cl.`name` AS country, s.name AS state, s.iso_code AS state_iso + FROM `'._DB_PREFIX_.'address` a + LEFT JOIN `'._DB_PREFIX_.'country` c ON (a.`id_country` = c.`id_country`) + LEFT JOIN `'._DB_PREFIX_.'country_lang` cl ON (c.`id_country` = cl.`id_country`) + LEFT JOIN `'._DB_PREFIX_.'state` s ON (s.`id_state` = a.`id_state`) + '.($share_order ? '' : Shop::addSqlAssociation('country', 'c')).' + WHERE `id_lang` = '.(int)$id_lang.' AND `id_customer` = '.(int)$this->id.' AND a.`deleted` = 0'; - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -476,12 +486,17 @@ class CustomerCore extends ObjectModel { if (!Validate::isUnsignedId($id_customer) || !Validate::isMd5($passwd)) die (Tools::displayError()); - - $sql = 'SELECT `id_customer` - FROM `'._DB_PREFIX_.'customer` - WHERE `id_customer` = '.$id_customer.' + $cache_id = 'Customer::checkPassword'.(int)$id_customer.'-'.$passwd; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT `id_customer` + FROM `'._DB_PREFIX_.'customer` + WHERE `id_customer` = '.$id_customer.' AND `passwd` = \''.$passwd.'\''; - return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); + $result = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -574,12 +589,16 @@ class CustomerCore extends ObjectModel public static function customerIdExistsStatic($id_customer) { - $row = Db::getInstance()->getRow(' - SELECT `id_customer` - FROM '._DB_PREFIX_.'customer c - WHERE c.`id_customer` = '.(int)$id_customer); - - return isset($row['id_customer']); + $cache_id = 'Customer::customerIdExistsStatic'.(int)$id_customer; + if (!Cache::isStored($cache_id)) + { + $result = (int)Db::getInstance()->getValue(' + SELECT `id_customer` + FROM '._DB_PREFIX_.'customer c + WHERE c.`id_customer` = '.(int)$id_customer); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/Meta.php b/classes/Meta.php index 4e911fe72..1612812b1 100644 --- a/classes/Meta.php +++ b/classes/Meta.php @@ -277,24 +277,31 @@ class MetaCore extends ObjectModel FROM `'._DB_PREFIX_.'category_lang` cl WHERE cl.`id_lang` = '.(int)$id_lang.' AND cl.`id_category` = '.(int)$id_category.Shop::addSqlRestrictionOnLang('cl'); - if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql)) + + $cache_id = 'Meta::getCategoryMetas'.(int)$id_category.'-'.(int)$id_lang; + if (!Cache::isStored($cache_id)) { - if (empty($row['meta_description'])) - $row['meta_description'] = strip_tags($row['description']); - - // Paginate title - if (!empty($row['meta_title'])) - $row['meta_title'] = $title.$row['meta_title'].(!empty($page_number) ? ' ('.$page_number.')' : '').' - '.Configuration::get('PS_SHOP_NAME'); + if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql)) + { + if (empty($row['meta_description'])) + $row['meta_description'] = strip_tags($row['description']); + + // Paginate title + if (!empty($row['meta_title'])) + $row['meta_title'] = $title.$row['meta_title'].(!empty($page_number) ? ' ('.$page_number.')' : '').' - '.Configuration::get('PS_SHOP_NAME'); + else + $row['meta_title'] = $row['name'].(!empty($page_number) ? ' ('.$page_number.')' : '').' - '.Configuration::get('PS_SHOP_NAME'); + + if (!empty($title)) + $row['meta_title'] = $title.(!empty($page_number) ? ' ('.$page_number.')' : '').' - '.Configuration::get('PS_SHOP_NAME'); + + $result = Meta::completeMetaTags($row, $row['name']); + } else - $row['meta_title'] = $row['name'].(!empty($page_number) ? ' ('.$page_number.')' : '').' - '.Configuration::get('PS_SHOP_NAME'); - - if (!empty($title)) - $row['meta_title'] = $title.(!empty($page_number) ? ' ('.$page_number.')' : '').' - '.Configuration::get('PS_SHOP_NAME'); - - return Meta::completeMetaTags($row, $row['name']); + $result = Meta::getHomeMetas($id_lang, $page_name); + Cache::store($cache_id, $result); } - - return Meta::getHomeMetas($id_lang, $page_name); + return Cache::retrieve($cache_id); } /** diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index 7683055a4..783cd6abf 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -191,7 +191,7 @@ abstract class ObjectModelCore if ($id) { // Load object from database if object id is present - $cache_id = 'objectmodel_'.$this->def['classname'].'_'.(int)$id.'_'.(int)$id_shop.'_'.(int)$id_lang; + $cache_id = 'objectmodel_'.$this->def['classname'].'_'.(int)$id.'_'.(int)$this->id_shop.'_'.(int)$id_lang; if (!Cache::isStored($cache_id)) { $sql = new DbQuery(); diff --git a/classes/Product.php b/classes/Product.php index 0d54b9752..a1fca7382 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -229,7 +229,13 @@ class ProductCore extends ObjectModel protected static $_prices = array(); protected static $_pricesLevel2 = array(); protected static $_incat = array(); + + /** + * @since 1.5.6.1 + * @var array $_cart_quantity is deprecated since 1.5.6.1 + */ protected static $_cart_quantity = array(); + protected static $_tax_rules_group = array(); protected static $_cacheFeatures = array(); protected static $_frontFeaturesCache = array(); @@ -2434,13 +2440,18 @@ class ProductCore extends ObjectModel { if (!$context) $context = Context::getContext(); - - $sql = 'SELECT image_shop.`id_image` - FROM `'._DB_PREFIX_.'image` i - '.Shop::addSqlAssociation('image', 'i').' - WHERE i.`id_product` = '.(int)$id_product.' - AND image_shop.`cover` = 1'; - return Db::getInstance()->getRow($sql); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$context->shop->id; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT image_shop.`id_image` + FROM `'._DB_PREFIX_.'image` i + '.Shop::addSqlAssociation('image', 'i').' + WHERE i.`id_product` = '.(int)$id_product.' + AND image_shop.`cover` = 1'; + $result = Db::getInstance()->getRow($sql); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -2502,15 +2513,17 @@ class ProductCore extends ObjectModel $cart_quantity = 0; if ((int)$id_cart) { - $condition = ''; - $cache_name = (int)$id_cart.'_'.(int)$id_product; - if (!isset(self::$_cart_quantity[$cache_name]) || self::$_cart_quantity[$cache_name] != (int)$quantity) - self::$_cart_quantity[$cache_name] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' - SELECT SUM(`quantity`) + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$id_cart; + if (!Cache::isStored($cache_id)) + { + $sql = 'SELECT SUM(`quantity`) FROM `'._DB_PREFIX_.'cart_product` WHERE `id_product` = '.(int)$id_product.' - AND `id_cart` = '.(int)$id_cart); - $cart_quantity = self::$_cart_quantity[$cache_name]; + AND `id_cart` = '.(int)$id_cart; + $cart_quantity = (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); + Cache::store($cache_id, $cart_quantity); + } + $cart_quantity = Cache::retrieve($cache_id); } $id_currency = (int)Validate::isLoadedObject($context->currency) ? $context->currency->id : Configuration::get('PS_CURRENCY_DEFAULT'); @@ -4300,19 +4313,25 @@ class ProductCore extends ObjectModel public function checkAccess($id_customer) { - if (!$id_customer) - return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' - SELECT ctg.`id_group` - FROM `'._DB_PREFIX_.'category_product` cp - INNER JOIN `'._DB_PREFIX_.'category_group` ctg ON (ctg.`id_category` = cp.`id_category`) - WHERE cp.`id_product` = '.(int)$this->id.' AND ctg.`id_group` ='.(int)Group::getCurrent()->id); - else - return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' - SELECT cg.`id_group` - FROM `'._DB_PREFIX_.'category_product` cp - INNER JOIN `'._DB_PREFIX_.'category_group` ctg ON (ctg.`id_category` = cp.`id_category`) - INNER JOIN `'._DB_PREFIX_.'customer_group` cg ON (cg.`id_group` = ctg.`id_group`) - WHERE cp.`id_product` = '.(int)$this->id.' AND cg.`id_customer` = '.(int)$id_customer); + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id.'-'.(int)$id_customer.(!$id_customer ? '-'.(int)Group::getCurrent()->id : ''); + if (!Cache::isStored($cache_id)) + { + if (!$id_customer) + $result = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT ctg.`id_group` + FROM `'._DB_PREFIX_.'category_product` cp + INNER JOIN `'._DB_PREFIX_.'category_group` ctg ON (ctg.`id_category` = cp.`id_category`) + WHERE cp.`id_product` = '.(int)$this->id.' AND ctg.`id_group` = '.(int)Group::getCurrent()->id); + else + $result = (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT cg.`id_group` + FROM `'._DB_PREFIX_.'category_product` cp + INNER JOIN `'._DB_PREFIX_.'category_group` ctg ON (ctg.`id_category` = cp.`id_category`) + INNER JOIN `'._DB_PREFIX_.'customer_group` cg ON (cg.`id_group` = ctg.`id_group`) + WHERE cp.`id_product` = '.(int)$this->id.' AND cg.`id_customer` = '.(int)$id_customer); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } @@ -4872,8 +4891,12 @@ class ProductCore extends ObjectModel */ public static function getAttributesParams($id_product, $id_product_attribute) { + $id_lang = (int)Context::getContext()->language->id; + $id_shop = (int)Context::getContext()->shop->id; + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$id_product_attribute.'-'.(int)$id_lang.'-'.(int)$id_shop; + // if blocklayered module is installed we check if user has set custom attribute name - if (Module::isInstalled('blocklayered')) + if (Module::isInstalled('blocklayered') && Module::isEnabled('blocklayered')) { $nb_custom_values = Db::getInstance()->executeS(' SELECT DISTINCT la.`id_attribute`, la.`url_name` as `name` @@ -4884,7 +4907,7 @@ class ProductCore extends ObjectModel ON (pac.`id_product_attribute` = pa.`id_product_attribute`) '.Shop::addSqlAssociation('product_attribute', 'pa').' LEFT JOIN `'._DB_PREFIX_.'layered_indexable_attribute_lang_value` la - ON (la.`id_attribute` = a.`id_attribute` AND la.`id_lang` = '.(int)Context::getContext()->language->id.') + ON (la.`id_attribute` = a.`id_attribute` AND la.`id_lang` = '.(int)$id_lang.') WHERE la.`url_name` IS NOT NULL AND la.`url_name` != \'\' AND pa.`id_product` = '.(int)$id_product.' AND pac.`id_product_attribute` = '.(int)$id_product_attribute); @@ -4902,7 +4925,7 @@ class ProductCore extends ObjectModel LEFT JOIN `'._DB_PREFIX_.'attribute` a ON (a.`id_attribute_group` = g.`id_attribute_group`) WHERE a.`id_attribute` = '.(int)$attribute['id_attribute'].' - AND g.`id_lang` = '.(int)Context::getContext()->language->id.' + AND g.`id_lang` = '.(int)$id_lang.' AND g.`url_name` IS NOT NULL AND g.`url_name` != \'\''); if (empty($group)) { @@ -4912,7 +4935,7 @@ class ProductCore extends ObjectModel LEFT JOIN `'._DB_PREFIX_.'attribute` a ON (a.`id_attribute_group` = g.`id_attribute_group`) WHERE a.`id_attribute` = '.(int)$attribute['id_attribute'].' - AND g.`id_lang` = '.(int)Context::getContext()->language->id.' + AND g.`id_lang` = '.(int)$id_lang.' AND g.`name` IS NOT NULL'); } $result[] = array_merge($attribute, $group[0]); @@ -4921,9 +4944,9 @@ class ProductCore extends ObjectModel SELECT DISTINCT a.`id_attribute_group`, al.`name`, agl.`name` as `group` FROM `'._DB_PREFIX_.'attribute` a LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al - ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)Context::getContext()->language->id.') + ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl - ON (a.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)Context::getContext()->language->id.') + ON (a.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (a.`id_attribute` = pac.`id_attribute`) LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa @@ -4932,45 +4955,30 @@ class ProductCore extends ObjectModel WHERE pa.`id_product` = '.(int)$id_product.' AND pac.id_product_attribute = '.(int)$id_product_attribute.' AND a.`id_attribute` NOT IN('.implode(', ', $tab_id_attribute).')'); - $result = array_merge($values_not_custom, $result); - } - else - { - $result = Db::getInstance()->executeS(' - SELECT al.`name`, agl.`name` as `group` - FROM `'._DB_PREFIX_.'attribute` a - LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al - ON (al.`id_attribute` = a.`id_attribute` AND al.`id_lang` = '.(int)Context::getContext()->language->id.') - LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac - ON (pac.`id_attribute` = a.`id_attribute`) - LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa - ON (pa.`id_product_attribute` = pac.`id_product_attribute`) - '.Shop::addSqlAssociation('product_attribute', 'pa').' - LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl - ON (a.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)Context::getContext()->language->id.') - WHERE pa.`id_product` = '.(int)$id_product.' - AND pac.`id_product_attribute` = '.(int)$id_product_attribute.' - AND agl.`id_lang` = '.(int)Context::getContext()->language->id); + return array_merge($values_not_custom, $result); } } - else + + if (!Cache::isStored($cache_id)) { $result = Db::getInstance()->executeS(' SELECT al.`name`, agl.`name` as `group` FROM `'._DB_PREFIX_.'attribute` a LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al - ON (al.`id_attribute` = a.`id_attribute` AND al.`id_lang` = '.(int)Context::getContext()->language->id.') + ON (al.`id_attribute` = a.`id_attribute` AND al.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_attribute` = a.`id_attribute`) LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product_attribute` = pac.`id_product_attribute`) '.Shop::addSqlAssociation('product_attribute', 'pa').' LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl - ON (a.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)Context::getContext()->language->id.') + ON (a.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)$id_lang.') WHERE pa.`id_product` = '.(int)$id_product.' AND pac.`id_product_attribute` = '.(int)$id_product_attribute.' - AND agl.`id_lang` = '.(int)Context::getContext()->language->id); + AND agl.`id_lang` = '.(int)$id_lang); + Cache::store($cache_id, $result); } + $result = Cache::retrieve($cache_id); return $result; } diff --git a/classes/State.php b/classes/State.php index e818c07a5..3bdeba81b 100644 --- a/classes/State.php +++ b/classes/State.php @@ -80,13 +80,19 @@ class StateCore extends ObjectModel */ public static function getNameById($id_state) { - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT `name` - FROM `'._DB_PREFIX_.'state` - WHERE `id_state` = '.(int)$id_state - ); - - return $result['name']; + if (!$id_state) + return false; + $cache_id = __CLASS__.__FUNCTION__.(int)$id_state; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT `name` + FROM `'._DB_PREFIX_.'state` + WHERE `id_state` = '.(int)$id_state + ); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -97,13 +103,19 @@ class StateCore extends ObjectModel */ public static function getIdByName($state) { - $result = Db::getInstance()->getValue(' - SELECT `id_state` - FROM `'._DB_PREFIX_.'state` - WHERE `name` LIKE \''.pSQL($state).'\' - '); - - return (int)$result; + if (empty($state)) + return false; + $cache_id = __CLASS__.__FUNCTION__.pSQL($state); + if (!Cache::isStored($cache_id)) + { + $result = (int)Db::getInstance()->getValue(' + SELECT `id_state` + FROM `'._DB_PREFIX_.'state` + WHERE `name` LIKE \''.pSQL($state).'\' + '); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/Tab.php b/classes/Tab.php index 94cb98de4..9ed6cfb51 100644 --- a/classes/Tab.php +++ b/classes/Tab.php @@ -190,14 +190,20 @@ class TabCore extends ObjectModel */ public static function getTab($id_lang, $id_tab) { - /* Tabs selection */ - return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT * - FROM `'._DB_PREFIX_.'tab` t - LEFT JOIN `'._DB_PREFIX_.'tab_lang` tl - ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '.(int)$id_lang.') - WHERE t.`id_tab` = '.(int)$id_tab - ); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_lang.'-'.(int)$id_tab; + if (!Cache::isStored($cache_id)) + { + /* Tabs selection */ + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + SELECT * + FROM `'._DB_PREFIX_.'tab` t + LEFT JOIN `'._DB_PREFIX_.'tab_lang` tl + ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '.(int)$id_lang.') + WHERE t.`id_tab` = '.(int)$id_tab + ); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/Zone.php b/classes/Zone.php index 699a90e52..7a476001a 100644 --- a/classes/Zone.php +++ b/classes/Zone.php @@ -54,12 +54,18 @@ class ZoneCore extends ObjectModel */ public static function getZones($active = false) { - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT * - FROM `'._DB_PREFIX_.'zone` - '.($active ? 'WHERE active = 1' : '').' - ORDER BY `name` ASC - '); + $cache_id = __CLASS__.__FUNCTION__.(bool)$active; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'zone` + '.($active ? 'WHERE active = 1' : '').' + ORDER BY `name` ASC + '); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/db/Db.php b/classes/db/Db.php index be9f1530a..2583396b1 100755 --- a/classes/db/Db.php +++ b/classes/db/Db.php @@ -523,13 +523,13 @@ abstract class DbCore $this->last_cached = true; return $result; } - $this->result = $this->query($sql); if (!$this->result) return false; - $this->last_cached = false; $result = $this->nextRow($this->result); + if (is_null($result)) + $result = false; if ($use_cache && $this->is_cache_enabled) Cache::getInstance()->setQuery($sql, $result); return $result; diff --git a/classes/module/Module.php b/classes/module/Module.php index b516a54b4..b5dd2ef64 100644 --- a/classes/module/Module.php +++ b/classes/module/Module.php @@ -1864,7 +1864,13 @@ abstract class ModuleCore */ public static function getModuleIdByName($name) { - return Db::getInstance()->getValue('SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = "'.pSQL($name).'"'); + $cache_id = __CLASS__.__FUNCTION__.pSQL($name); + if (!Cache::isStored($cache_id)) + { + $result = (int)Db::getInstance()->getValue('SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = "'.pSQL($name).'"'); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/order/OrderState.php b/classes/order/OrderState.php index 7cc1d5375..2829f2ca2 100644 --- a/classes/order/OrderState.php +++ b/classes/order/OrderState.php @@ -112,12 +112,18 @@ class OrderStateCore extends ObjectModel */ public static function getOrderStates($id_lang) { - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT * - FROM `'._DB_PREFIX_.'order_state` os - LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int)$id_lang.') - WHERE deleted = 0 - ORDER BY `name` ASC'); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_lang; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'order_state` os + LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int)$id_lang.') + WHERE deleted = 0 + ORDER BY `name` ASC'); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/classes/shop/Shop.php b/classes/shop/Shop.php index 98ae61c53..c8f72bef7 100644 --- a/classes/shop/Shop.php +++ b/classes/shop/Shop.php @@ -187,14 +187,19 @@ class ShopCore extends ObjectModel public function setUrl() { - $row = Db::getInstance()->getRow(' - SELECT su.physical_uri, su.virtual_uri, su.domain, su.domain_ssl, t.id_theme, t.name, t.directory - FROM '._DB_PREFIX_.'shop s - LEFT JOIN '._DB_PREFIX_.'shop_url su ON (s.id_shop = su.id_shop) - LEFT JOIN '._DB_PREFIX_.'theme t ON (t.id_theme = s.id_theme) - WHERE s.id_shop = '.(int)$this->id.' - AND s.active = 1 AND s.deleted = 0 AND su.main = 1'); - + $cache_id = __CLASS__.__FUNCTION__.(int)$this->id; + if (!Cache::isStored($cache_id)) + { + $row = Db::getInstance()->getRow(' + SELECT su.physical_uri, su.virtual_uri, su.domain, su.domain_ssl, t.id_theme, t.name, t.directory + FROM '._DB_PREFIX_.'shop s + LEFT JOIN '._DB_PREFIX_.'shop_url su ON (s.id_shop = su.id_shop) + LEFT JOIN '._DB_PREFIX_.'theme t ON (t.id_theme = s.id_theme) + WHERE s.id_shop = '.(int)$this->id.' + AND s.active = 1 AND s.deleted = 0 AND su.main = 1'); + Cache::store($cache_id, $row); + } + $row = Cache::retrieve($cache_id); if (!$row) return false; diff --git a/classes/tax/TaxRule.php b/classes/tax/TaxRule.php index e59a2ddb9..0d778d6c0 100644 --- a/classes/tax/TaxRule.php +++ b/classes/tax/TaxRule.php @@ -124,9 +124,13 @@ class TaxRuleCore extends ObjectModel */ public static function isTaxInUse($id_tax) { - return Db::getInstance()->getValue(' - SELECT COUNT(*) FROM `'._DB_PREFIX_.'tax_rule` WHERE `id_tax` = '.(int)$id_tax - ); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_tax; + if (!Cache::isStored($cache_id)) + { + $result = (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'tax_rule` WHERE `id_tax` = '.(int)$id_tax); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } diff --git a/controllers/front/ProductController.php b/controllers/front/ProductController.php index ed2454ab6..46a3a1aff 100644 --- a/controllers/front/ProductController.php +++ b/controllers/front/ProductController.php @@ -548,7 +548,7 @@ class ProductControllerCore extends FrontController $path = Tools::getPath((int)$this->context->shop->id_category, $this->product->name); $this->context->smarty->assign('path', $path); - $this->context->smarty->assign('categories', Category::getHomeCategories($this->context->language->id)); + $this->context->smarty->assign('categories', Category::getHomeCategories($this->context->language->id, true, (int)$this->context->shop->id)); $this->context->smarty->assign(array('HOOK_PRODUCT_FOOTER' => Hook::exec('displayFooterProduct', array('product' => $this->product, 'category' => $this->category)))); } diff --git a/modules/blocktopmenu/blocktopmenu.php b/modules/blocktopmenu/blocktopmenu.php index f22947251..51858836b 100644 --- a/modules/blocktopmenu/blocktopmenu.php +++ b/modules/blocktopmenu/blocktopmenu.php @@ -572,7 +572,7 @@ class Blocktopmenu extends Module switch (substr($item, 0, strlen($value[1]))) { case 'CAT': - $this->getCategory((int)$id); + $this->getCategory($id, $id_lang, $id_shop); break; case 'PRD': @@ -687,9 +687,7 @@ class Blocktopmenu extends Module if (isset($children) && count($children)) foreach ($children as $child) - { $this->getCategoryOption((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']); - } } private function getCategory($id_category, $id_lang = false, $id_shop = false) diff --git a/modules/blockwishlist/WishList.php b/modules/blockwishlist/WishList.php index 7ab58cc44..10e15e03b 100644 --- a/modules/blockwishlist/WishList.php +++ b/modules/blockwishlist/WishList.php @@ -171,6 +171,8 @@ class WishList extends ObjectModel */ public static function getByIdCustomer($id_customer) { + if (!Validate::isUnsignedId($id_customer)) + die (Tools::displayError()); if (Shop::getContextShopID()) $shop_restriction = 'AND id_shop = '.(int)Shop::getContextShopID(); elseif (Shop::getContextShopGroupID()) @@ -178,14 +180,18 @@ class WishList extends ObjectModel else $shop_restriction = ''; - if (!Validate::isUnsignedId($id_customer)) - die (Tools::displayError()); - return (Db::getInstance()->executeS(' - SELECT w.`id_wishlist`, w.`name`, w.`token`, w.`date_add`, w.`date_upd`, w.`counter` - FROM `'._DB_PREFIX_.'wishlist` w - WHERE `id_customer` = '.(int)($id_customer).' - '.$shop_restriction.' - ORDER BY w.`name` ASC')); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_customer.'-'.(int)Shop::getContextShopID().'-'.(int)Shop::getContextShopGroupID(); + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance()->executeS(' + SELECT w.`id_wishlist`, w.`name`, w.`token`, w.`date_add`, w.`date_upd`, w.`counter` + FROM `'._DB_PREFIX_.'wishlist` w + WHERE `id_customer` = '.(int)($id_customer).' + '.$shop_restriction.' + ORDER BY w.`name` ASC'); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } public static function refreshWishList($id_wishlist) diff --git a/modules/productcomments/ProductComment.php b/modules/productcomments/ProductComment.php index cb7aa8994..8518ebe0a 100644 --- a/modules/productcomments/ProductComment.php +++ b/modules/productcomments/ProductComment.php @@ -97,18 +97,24 @@ class ProductComment extends ObjectModel if ($n != null && $n <= 0) $n = 5; - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT pc.`id_product_comment`, - (SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful, - (SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '. - ((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : ''). - ((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').' - IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title - FROM `'._DB_PREFIX_.'product_comment` pc - LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer` - WHERE pc.`id_product` = '.(int)($id_product).($validate == '1' ? ' AND pc.`validate` = 1' : '').' - ORDER BY pc.`date_add` DESC - '.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : '')); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$p.'-'.(int)$n.'-'.(int)$id_customer.'-'.(bool)$validate; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT pc.`id_product_comment`, + (SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful, + (SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '. + ((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : ''). + ((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').' + IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title + FROM `'._DB_PREFIX_.'product_comment` pc + LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer` + WHERE pc.`id_product` = '.(int)($id_product).($validate == '1' ? ' AND pc.`validate` = 1' : '').' + ORDER BY pc.`date_add` DESC + '.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : '')); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** @@ -118,18 +124,24 @@ class ProductComment extends ObjectModel */ public static function getByCustomer($id_product, $id_customer, $get_last = false, $id_guest = false) { - $results = Db::getInstance()->executeS(' - SELECT * - FROM `'._DB_PREFIX_.'product_comment` pc - WHERE pc.`id_product` = '.(int)$id_product.' - AND '.(!$id_guest ? 'pc.`id_customer` = '.(int)$id_customer : 'pc.`id_guest` = '.(int)$id_guest).' - ORDER BY pc.`date_add` DESC ' - .($get_last ? 'LIMIT 1' : '') - ); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$id_customer.'-'.(bool)$get_last.'-'.(int)$id_guest; + if (!Cache::isStored($cache_id)) + { + $results = Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'product_comment` pc + WHERE pc.`id_product` = '.(int)$id_product.' + AND '.(!$id_guest ? 'pc.`id_customer` = '.(int)$id_customer : 'pc.`id_guest` = '.(int)$id_guest).' + ORDER BY pc.`date_add` DESC ' + .($get_last ? 'LIMIT 1' : '') + ); - if ($get_last) - $results = array_shift($results); - return $results; + if ($get_last && count($results)) + $results = array_shift($results); + + Cache::store($cache_id, $results); + } + return Cache::retrieve($cache_id); } /** @@ -202,12 +214,16 @@ class ProductComment extends ObjectModel if (!Validate::isUnsignedId($id_product)) die(Tools::displayError()); $validate = (int)Configuration::get('PRODUCT_COMMENTS_MODERATE'); - if (($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT COUNT(`id_product_comment`) AS "nbr" - FROM `'._DB_PREFIX_.'product_comment` pc - WHERE `id_product` = '.(int)($id_product).($validate == '1' ? ' AND `validate` = 1' : ''))) === false) - return false; - return (int)($result['nbr']); + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.$validate; + if (!Cache::isStored($cache_id)) + { + $result = (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(`id_product_comment`) AS "nbr" + FROM `'._DB_PREFIX_.'product_comment` pc + WHERE `id_product` = '.(int)($id_product).($validate == '1' ? ' AND `validate` = 1' : '')); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /** diff --git a/modules/productcomments/ProductCommentCriterion.php b/modules/productcomments/ProductCommentCriterion.php index dc8f668ba..07ae1a134 100644 --- a/modules/productcomments/ProductCommentCriterion.php +++ b/modules/productcomments/ProductCommentCriterion.php @@ -159,26 +159,33 @@ class ProductCommentCriterion extends ObjectModel $table = '_shop'; $alias = 'ps'; } - return Db::getInstance()->executeS(' - SELECT pcc.`id_product_comment_criterion`, pccl.`name` - FROM `'._DB_PREFIX_.'product_comment_criterion` pcc - LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_lang` pccl - ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion) - LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_product` pccp - ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = '.(int)$id_product.') - LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_category` pccc - ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`) - LEFT JOIN `'._DB_PREFIX_.'product'.$table.'` '.$alias.' - ON ('.$alias.'.id_category_default = pccc.id_category AND '.$alias.'.id_product = '.(int)$id_product.') - WHERE pccl.`id_lang` = '.(int)($id_lang).' - AND ( - pccp.id_product IS NOT NULL - OR ps.id_product IS NOT NULL - OR pcc.id_product_comment_criterion_type = 1 - ) - AND pcc.active = 1 - GROUP BY pcc.id_product_comment_criterion - '); + + $cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$id_lang; + if (!Cache::isStored($cache_id)) + { + $result = Db::getInstance()->executeS(' + SELECT pcc.`id_product_comment_criterion`, pccl.`name` + FROM `'._DB_PREFIX_.'product_comment_criterion` pcc + LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_lang` pccl + ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion) + LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_product` pccp + ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = '.(int)$id_product.') + LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_category` pccc + ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`) + LEFT JOIN `'._DB_PREFIX_.'product'.$table.'` '.$alias.' + ON ('.$alias.'.id_category_default = pccc.id_category AND '.$alias.'.id_product = '.(int)$id_product.') + WHERE pccl.`id_lang` = '.(int)($id_lang).' + AND ( + pccp.id_product IS NOT NULL + OR ps.id_product IS NOT NULL + OR pcc.id_product_comment_criterion_type = 1 + ) + AND pcc.active = 1 + GROUP BY pcc.id_product_comment_criterion + '); + Cache::store($cache_id, $result); + } + return Cache::retrieve($cache_id); } /**