Skip to content

Commit

Permalink
[*] Core: Add ObjectModel::disableCache() - useful in case of loading…
Browse files Browse the repository at this point in the history
… a lot of objects
  • Loading branch information
rGaillard committed Jan 27, 2015
1 parent 2612d92 commit 8201d4c
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions classes/ObjectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ abstract class ObjectModelCore
*/
public $force_id = false;

/**
* @var boolean cache the objects in memory
*/
protected static $cache_objects = true;

/**
* Returns object validation rules (fields validity)
*
Expand Down Expand Up @@ -208,7 +213,7 @@ public function __construct($id = null, $id_lang = null, $id_shop = null)
{
// Load object from database if object id is present
$cache_id = 'objectmodel_'.$this->def['classname'].'_'.(int)$id.'_'.(int)$this->id_shop.'_'.(int)$id_lang;
if (!Cache::isStored($cache_id))
if (!ObjectModel::$cache_objects || !Cache::isStored($cache_id))
{
$sql = new DbQuery();
$sql->from($this->def['table'], 'a');
Expand Down Expand Up @@ -245,7 +250,8 @@ public function __construct($id = null, $id_lang = null, $id_shop = null)
}
}
}
Cache::store($cache_id, $object_datas);
if (ObjectModel::$cache_objects)
Cache::store($cache_id, $object_datas);
}
}
else
Expand Down Expand Up @@ -1270,13 +1276,19 @@ public function isAssociatedToShop($id_shop = null)
$id_shop = Context::getContext()->shop->id;

$cache_id = 'objectmodel_shop_'.$this->def['classname'].'_'.(int)$this->id.'-'.(int)$id_shop;
if (!Cache::isStored($cache_id))
if (!ObjectModel::$cache_objects || !Cache::isStored($cache_id))
{
$sql = 'SELECT id_shop
FROM `'.pSQL(_DB_PREFIX_.$this->def['table']).'_shop`
WHERE `'.$this->def['primary'].'` = '.(int)$this->id.'
AND id_shop = '.(int)$id_shop;
Cache::store($cache_id, (bool)Db::getInstance()->getValue($sql));
$associated = (bool)Db::getInstance()->getValue('
SELECT id_shop
FROM `'.pSQL(_DB_PREFIX_.$this->def['table']).'_shop`
WHERE `'.$this->def['primary'].'` = '.(int)$this->id.'
AND id_shop = '.(int)$id_shop
);

if (!ObjectModel::$cache_objects)
return $associated;

Cache::store($cache_id, $associated);
}
return Cache::retrieve($cache_id);
}
Expand Down Expand Up @@ -1698,4 +1710,14 @@ public function setFieldsToUpdate(array $fields)
{
$this->update_fields = $fields;
}
}

public static function enableCache()
{
ObjectModel::$cache_objects = true;
}

public static function disableCache()
{
ObjectModel::$cache_objects = false;
}
}

0 comments on commit 8201d4c

Please sign in to comment.