mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Improve performance of DataObject::db() with caching
In a usual CMS request, DataObject::db() is called potentially thousands of times, calling Config::get() constantly for the same uninherited statics, which is slow. This improves performance by caching those into DataObject::$_cache_db
This commit is contained in:
parent
edb4ecd4d9
commit
f69c2b0495
@ -145,6 +145,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
*/
|
*/
|
||||||
public static $cache_has_own_table = array();
|
public static $cache_has_own_table = array();
|
||||||
public static $cache_has_own_table_field = array();
|
public static $cache_has_own_table_field = array();
|
||||||
|
protected static $_cache_db = array();
|
||||||
protected static $_cache_get_one;
|
protected static $_cache_get_one;
|
||||||
protected static $_cache_get_class_ancestry;
|
protected static $_cache_get_class_ancestry;
|
||||||
protected static $_cache_composite_fields = array();
|
protected static $_cache_composite_fields = array();
|
||||||
@ -1578,23 +1579,28 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(isset(self::$_cache_db[$class])) {
|
||||||
|
$dbItems = self::$_cache_db[$class];
|
||||||
|
} else {
|
||||||
|
$dbItems = (array) Config::inst()->get($class, 'db', Config::UNINHERITED);
|
||||||
|
self::$_cache_db[$class] = $dbItems;
|
||||||
|
}
|
||||||
|
|
||||||
if($fieldName) {
|
if($fieldName) {
|
||||||
$db = Config::inst()->get($class, 'db', Config::UNINHERITED);
|
if(isset($dbItems[$fieldName])) {
|
||||||
|
return $dbItems[$fieldName];
|
||||||
if(isset($db[$fieldName])) {
|
|
||||||
return $db[$fieldName];
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$newItems = (array)Config::inst()->get($class, 'db', Config::UNINHERITED);
|
|
||||||
// Validate the data
|
// Validate the data
|
||||||
foreach($newItems as $k => $v) {
|
foreach($dbItems as $k => $v) {
|
||||||
if(!is_string($k) || is_numeric($k) || !is_string($v)) {
|
if(!is_string($k) || is_numeric($k) || !is_string($v)) {
|
||||||
user_error("$class::\$db has a bad entry: "
|
user_error("$class::\$db has a bad entry: "
|
||||||
. var_export($k,true). " => " . var_export($v,true) . ". Each map key should be a"
|
. var_export($k,true). " => " . var_export($v,true) . ". Each map key should be a"
|
||||||
. " property name, and the map value should be the property type.", E_USER_ERROR);
|
. " property name, and the map value should be the property type.", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$items = isset($items) ? array_merge((array)$items, $newItems) : $newItems;
|
|
||||||
|
$items = isset($items) ? array_merge((array) $items, $dbItems) : $dbItems;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2906,6 +2912,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
public static function reset() {
|
public static function reset() {
|
||||||
DataObject::$cache_has_own_table = array();
|
DataObject::$cache_has_own_table = array();
|
||||||
DataObject::$cache_has_own_table_field = array();
|
DataObject::$cache_has_own_table_field = array();
|
||||||
|
DataObject::$_cache_db = array();
|
||||||
DataObject::$_cache_get_one = array();
|
DataObject::$_cache_get_one = array();
|
||||||
DataObject::$_cache_composite_fields = array();
|
DataObject::$_cache_composite_fields = array();
|
||||||
DataObject::$_cache_custom_database_fields = array();
|
DataObject::$_cache_custom_database_fields = array();
|
||||||
|
Loading…
Reference in New Issue
Block a user