Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[*] CORE : Improve configuration load & get performances #5106

Merged
merged 5 commits into from Apr 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 58 additions & 41 deletions classes/Configuration.php
Expand Up @@ -60,8 +60,11 @@ class ConfigurationCore extends ObjectModel
),
);

/** @var array Configuration cache */
protected static $_cache = array();
/** @var array Configuration cache (kept for backward compat) */
protected static $_cache = null;

/** @var array Configuration cache with optimised key order */
protected static $_new_cache = null;

/** @var array Vars types */
protected static $types = array();
Expand Down Expand Up @@ -116,7 +119,7 @@ public static function configurationIsLoaded()
return $loaded;
}

if (isset(self::$_cache) && isset(self::$_cache[self::$definition['table']]) && count(self::$_cache[self::$definition['table']])) {
if (self::$_new_cache !== null) {
$loaded = true;
return $loaded;
}
Expand All @@ -131,7 +134,8 @@ public static function configurationIsLoaded()
*/
public static function clearConfigurationCacheForTesting()
{
self::$_cache = array();
self::$_cache = null;
self::$_new_cache = null;
}

/**
Expand All @@ -140,29 +144,41 @@ public static function clearConfigurationCacheForTesting()
public static function loadConfiguration()
{
self::$_cache[self::$definition['table']] = array();
self::$_new_cache = array();

$sql = 'SELECT c.`name`, cl.`id_lang`, IF(cl.`id_lang` IS NULL, c.`value`, cl.`value`) AS value, c.id_shop_group, c.id_shop
FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'` c
LEFT JOIN `'._DB_PREFIX_.bqSQL(self::$definition['table']).'_lang` cl ON (c.`'.bqSQL(self::$definition['primary']).'` = cl.`'.bqSQL(self::$definition['primary']).'`)';
$db = Db::getInstance();
$result = $db->executeS($sql, false);
while ($row = $db->nextRow($result)) {
$lang = ($row['id_lang']) ? $row['id_lang'] : 0;
self::$types[$row['name']] = ($lang) ? 'lang' : 'normal';
if (!isset(self::$_cache[self::$definition['table']][$lang])) {
self::$_cache[self::$definition['table']][$lang] = array(
'global' => array(),
'group' => array(),
'shop' => array(),
);
}
$results = $db->executeS($sql);
if ($results) {
foreach ($results as $row) {
$lang = ($row['id_lang']) ? $row['id_lang'] : 0;
self::$types[$row['name']] = ($lang) ? 'lang' : 'normal';

if (!isset(self::$_cache[self::$definition['table']][$lang])) {
self::$_cache[self::$definition['table']][$lang] = array(
'global' => array(),
'group' => array(),
'shop' => array(),
);
}

if ($row['id_shop']) {
self::$_cache[self::$definition['table']][$lang]['shop'][$row['id_shop']][$row['name']] = $row['value'];
} elseif ($row['id_shop_group']) {
self::$_cache[self::$definition['table']][$lang]['group'][$row['id_shop_group']][$row['name']] = $row['value'];
} else {
self::$_cache[self::$definition['table']][$lang]['global'][$row['name']] = $row['value'];
if ($row['value'] === null) {
$row['value'] = '';
}


if ($row['id_shop']) {
self::$_cache[self::$definition['table']][$lang]['shop'][$row['id_shop']][$row['name']] = $row['value'];
self::$_new_cache[$row['name']][$lang]['shop'][$row['id_shop']] = $row['value'];
} elseif ($row['id_shop_group']) {
self::$_cache[self::$definition['table']][$lang]['group'][$row['id_shop_group']][$row['name']] = $row['value'];
self::$_new_cache[$row['name']][$lang]['group'][$row['id_shop_group']] = $row['value'];
} else {
self::$_cache[self::$definition['table']][$lang]['global'][$row['name']] = $row['value'];
self::$_new_cache[$row['name']][$lang]['global'] = $row['value'];
}
}
}
}
Expand All @@ -181,30 +197,32 @@ public static function get($key, $id_lang = null, $id_shop_group = null, $id_sho
}

// If conf if not initialized, try manual query
if (!isset(self::$_cache[self::$definition['table']])) {
if (self::$_new_cache === null) {
Configuration::loadConfiguration();
if (!self::$_cache[self::$definition['table']]) {
if (self::$_new_cache === array()) {
return Db::getInstance()->getValue('SELECT `value` FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'` WHERE `name` = "'.pSQL($key).'"');
}
}
$id_lang = (int)$id_lang;

if (!isset(self::$_new_cache[$key][$id_lang])) {
$id_lang = 0;
}

if ($id_shop === null || !Shop::isFeatureActive()) {
$id_shop = Shop::getContextShopID(true);
}

if ($id_shop_group === null || !Shop::isFeatureActive()) {
$id_shop_group = Shop::getContextShopGroupID(true);
}

if (!isset(self::$_cache[self::$definition['table']][$id_lang])) {
$id_lang = 0;
}

if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop)) {
return self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop][$key];
return self::$_new_cache[$key][$id_lang]['shop'][$id_shop];
} elseif ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group)) {
return self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group][$key];
return self::$_new_cache[$key][$id_lang]['group'][$id_shop_group];
} elseif (Configuration::hasKey($key, $id_lang)) {
return self::$_cache[self::$definition['table']][$id_lang]['global'][$key];
return self::$_new_cache[$key][$id_lang]['global'];
}
return $default;
}
Expand Down Expand Up @@ -299,18 +317,12 @@ public static function hasKey($key, $id_lang = null, $id_shop_group = null, $id_
$id_lang = (int)$id_lang;

if ($id_shop) {
return isset(self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop])
&& (isset(self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop][$key])
|| array_key_exists($key, self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop]));
return isset(self::$_new_cache[$key][$id_lang]['shop'][$id_shop]);
} elseif ($id_shop_group) {
return isset(self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group])
&& (isset(self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group][$key])
|| array_key_exists($key, self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group]));
return isset(self::$_new_cache[$key][$id_lang]['group'][$id_shop_group]);
}

return isset(self::$_cache[self::$definition['table']][$id_lang]['global'])
&& (isset(self::$_cache[self::$definition['table']][$id_lang]['global'][$key])
|| array_key_exists($key, self::$_cache[self::$definition['table']][$id_lang]['global']));
return isset(self::$_new_cache[$key][$id_lang]['global']);
}

/**
Expand Down Expand Up @@ -340,10 +352,13 @@ public static function set($key, $values, $id_shop_group = null, $id_shop = null

foreach ($values as $lang => $value) {
if ($id_shop) {
self::$_new_cache[$key][$lang]['shop'][$id_shop] = $value;
self::$_cache[self::$definition['table']][$lang]['shop'][$id_shop][$key] = $value;
} elseif ($id_shop_group) {
self::$_new_cache[$key][$lang]['group'][$id_shop_group] = $value;
self::$_cache[self::$definition['table']][$lang]['group'][$id_shop_group][$key] = $value;
} else {
self::$_new_cache[$key][$lang]['global'] = $value;
self::$_cache[self::$definition['table']][$lang]['global'][$key] = $value;
}
}
Expand Down Expand Up @@ -487,7 +502,8 @@ public static function deleteByName($key)
DELETE FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'`
WHERE `name` = "'.pSQL($key).'"');

self::$_cache[self::$definition['table']] = null;
self::$_cache = null;
self::$_new_cache = null;

return ($result && $result2);
}
Expand Down Expand Up @@ -517,7 +533,8 @@ public static function deleteFromContext($key)
DELETE FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'_lang`
WHERE `'.bqSQL(self::$definition['primary']).'` = '.(int)$id);

self::$_cache[self::$definition['table']] = null;
self::$_cache = null;
self::$_new_cache = null;
}

/**
Expand Down
39 changes: 9 additions & 30 deletions tests/Integration/classes/ConfigurationCoreTest.php
Expand Up @@ -24,7 +24,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Tests\Integration\Classes;
namespace PrestaShop\PrestaShop\tests\Integration\classes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch


use PrestaShop\PrestaShop\Tests\Helper\ReflexionHelper;
use PrestaShop\PrestaShop\Tests\TestCase\IntegrationTestCase;
Expand All @@ -36,53 +36,32 @@ class ConfigurationCoreTest extends IntegrationTestCase

protected function setUp()
{
$configuration = array();
$id_shops = array(1, 2);
$id_shop_groups = array(1, 2);
$id_langs = array(0, 1, 2);
foreach ($id_langs as $id_lang) {
$configuration['configuration'][$id_lang] = array(
'global' => array(),
'group' => array(),
'shop' => array()
);

foreach ($id_shop_groups as $id_group) {
$configuration['configuration'][$id_lang]['group'][$id_group] = array();
}
foreach ($id_shops as $id_shop) {
$configuration['configuration'][$id_lang]['shop'][$id_shop] = array();
}
}

$configuration['configuration'][0]['global']['PS_TEST_NOT_OVERRIDDEN'] = 'RESULT_NOT_OVERRIDDEN';
Configuration::set('PS_TEST_NOT_OVERRIDDEN', 'RESULT_NOT_OVERRIDDEN', 0, 0);
Configuration::set('PS_TEST_GROUP_OVERRIDDEN', 'RESULT_GROUP_OVERRIDDEN', 0, 0);

$configuration['configuration'][0]['global']['PS_TEST_GROUP_OVERRIDDEN'] = 'RESULT_GROUP_OVERRIDDEN';
foreach ($id_shop_groups as $id_group) {
$configuration['configuration'][0]['group'][$id_group]['PS_TEST_GROUP_OVERRIDDEN'] = 'RESULT_GROUP_OVERRIDDEN_'.$id_group;
Configuration::set('PS_TEST_GROUP_OVERRIDDEN', 'RESULT_GROUP_OVERRIDDEN_'.$id_group, $id_group, 0);
}

$configuration['configuration'][0]['global']['PS_TEST_SHOP_OVERRIDDEN'] = 'RESULT_SHOP_OVERRIDDEN';
Configuration::updateGlobalValue('PS_TEST_SHOP_OVERRIDDEN', 'RESULT_SHOP_OVERRIDDEN');
foreach ($id_shops as $id_shop) {
$configuration['configuration'][0]['shop'][$id_shop]['PS_TEST_SHOP_OVERRIDDEN'] = 'RESULT_SHOP_OVERRIDDEN_'.$id_shop;
Configuration::set('PS_TEST_SHOP_OVERRIDDEN', 'RESULT_SHOP_OVERRIDDEN_'.$id_shop, 0, $id_shop);
}

$configuration['configuration'][0]['global']['PS_TEST_GROUP_SHOP_OVERRIDDEN'] = 'RESULT_GROUP_SHOP_OVERRIDDEN';
Configuration::updateGlobalValue('PS_TEST_GROUP_SHOP_OVERRIDDEN', 'RESULT_GROUP_SHOP_OVERRIDDEN');
foreach ($id_shop_groups as $id_group) {
$configuration['configuration'][0]['group'][$id_group]['PS_TEST_GROUP_SHOP_OVERRIDDEN'] = 'RESULT_GROUP_SHOP_OVERRIDDEN_GROUP_'.$id_group;
Configuration::set('PS_TEST_GROUP_SHOP_OVERRIDDEN', 'RESULT_GROUP_SHOP_OVERRIDDEN_GROUP_'.$id_group, $id_group, 0);
}
foreach ($id_shops as $id_shop) {
$configuration['configuration'][0]['shop'][$id_shop]['PS_TEST_GROUP_SHOP_OVERRIDDEN'] = 'RESULT_GROUP_SHOP_OVERRIDDEN_SHOP_'.$id_shop;
Configuration::set('PS_TEST_GROUP_SHOP_OVERRIDDEN', 'RESULT_GROUP_SHOP_OVERRIDDEN_SHOP_'.$id_shop, 0, $id_shop);
}


$this->default = ReflexionHelper::getProperty(new Configuration(), '_cache');
ReflexionHelper::setProperty(new Configuration(), '_cache', $configuration);
}

public function teardown()
{
ReflexionHelper::setProperty(new Configuration(), '_cache', $this->default);
}

public function testGetGlobalValue()
Expand Down