Fixing performance of DataObject::custom_database_fields()

On sites with lots of modules, and pages with plenty of database
queries, DataObject::custom_database_fields() can be called
thousands of times, and slow down page render times. This fixes
it so the fields are cached by class in a static variable, and
are cleared when reset() is called on the DataObject.
This commit is contained in:
Sean Harvey 2012-11-07 17:23:36 +13:00
parent 50d6296a7d
commit fdcd7a2e60

View File

@ -148,6 +148,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
protected static $_cache_get_one;
protected static $_cache_get_class_ancestry;
protected static $_cache_composite_fields = array();
protected static $_cache_custom_database_fields = array();
protected static $_cache_field_labels = array();
/**
@ -226,6 +227,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @return array Map of fieldname to specification, similiar to {@link DataObject::$db}.
*/
public static function custom_database_fields($class) {
if(isset(self::$_cache_custom_database_fields[$class])) {
return self::$_cache_custom_database_fields[$class];
}
$fields = Config::inst()->get($class, 'db', Config::UNINHERITED);
foreach(self::composite_fields($class, false) as $fieldName => $fieldClass) {
@ -245,7 +250,11 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$fields[$field . 'ID'] = 'ForeignKey';
}
return (array)$fields;
$output = (array) $fields;
self::$_cache_custom_database_fields[$class] = $output;
return $output;
}
/**
@ -2899,6 +2908,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
DataObject::$cache_has_own_table_field = array();
DataObject::$_cache_get_one = array();
DataObject::$_cache_composite_fields = array();
DataObject::$_cache_custom_database_fields = array();
DataObject::$_cache_get_class_ancestry = array();
DataObject::$_cache_field_labels = array();
}