Merge pull request #7489 from open-sausages/pulls/4.0/many_many_queries_for_images

BUG cache the cacheKey in TreeDropdownField, so it doesn't need to query for it multiple times in the same request
This commit is contained in:
Loz Calver 2017-10-18 09:26:51 +01:00 committed by GitHub
commit 2f4f601497

View File

@ -8,6 +8,7 @@ use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\Hierarchy\Hierarchy;
use SilverStripe\ORM\Hierarchy\MarkedSet;
use SilverStripe\View\ViewableData;
@ -202,6 +203,11 @@ class TreeDropdownField extends FormField
*/
protected $showSelectedPath = false;
/**
* @var array
*/
protected static $cacheKeyCache = [];
/**
* CAVEAT: for search to work properly $labelField must be a database field,
* or you need to setSearchFunction.
@ -858,8 +864,7 @@ class TreeDropdownField extends FormField
/** @var Hierarchy|DataObject $record */
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
// Ensure cache is keyed by last modified date of the underlying list
$data['data']['cacheKey'] = DataList::create($this->getSourceObject())->max('LastEdited');
$data['data']['cacheKey'] = $this->getCacheKey();
$data['data']['showSelectedPath'] = $this->getShowSelectedPath();
if ($record) {
$titlePath = '';
@ -883,6 +888,21 @@ class TreeDropdownField extends FormField
return $data;
}
/**
* Ensure cache is keyed by last modified datetime of the underlying list.
* Caches the key for the respective underlying list types, since it doesn't need to query again.
*
* @return DBDatetime
*/
protected function getCacheKey()
{
$target = $this->getSourceObject();
if (!isset(self::$cacheKeyCache[$target])) {
self::$cacheKeyCache[$target] = DataList::create($target)->max('LastEdited');
}
return self::$cacheKeyCache[$target];
}
public function getSchemaDataDefaults()
{
$data = parent::getSchemaDataDefaults();