ENHANCEMENT Caching expensive CMSMain->SiteTreeHints() call on disk

This commit is contained in:
Ingo Schommer 2012-04-13 18:42:15 +02:00
parent d9c4aa8583
commit 2dc0e72c00
2 changed files with 61 additions and 51 deletions

View File

@ -244,67 +244,72 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
* @return String Serialized JSON * @return String Serialized JSON
*/ */
public function SiteTreeHints() { public function SiteTreeHints() {
$json = '';
$classes = ClassInfo::subclassesFor( $this->stat('tree_class') ); $classes = ClassInfo::subclassesFor( $this->stat('tree_class') );
$def['Root'] = array(); $cacheCanCreate = array();
$def['Root']['disallowedParents'] = array(); foreach($classes as $class) $cacheCanCreate[$class] = singleton($class)->canCreate();
foreach($classes as $class) { // Generate basic cache key. Too complex to encompass all variations
$obj = singleton($class); $cache = SS_Cache::factory('CMSMain_SiteTreeHints');
$cacheKey = md5(implode('_', array(Member::currentUserID(), implode(',', $cacheCanCreate), implode(',', $classes))));
if($this->request->getVar('flush')) $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
$json = $cache->load($cacheKey);
if(!$json) {
$def['Root'] = array();
$def['Root']['disallowedParents'] = array();
if($obj instanceof HiddenClass) continue; foreach($classes as $class) {
$obj = singleton($class);
if($obj instanceof HiddenClass) continue;
$allowedChildren = $obj->allowedChildren(); $allowedChildren = $obj->allowedChildren();
// SiteTree::allowedChildren() returns null rather than an empty array if SiteTree::allowed_chldren == 'none' // SiteTree::allowedChildren() returns null rather than an empty array if SiteTree::allowed_chldren == 'none'
if($allowedChildren == null) $allowedChildren = array(); if($allowedChildren == null) $allowedChildren = array();
// Exclude SiteTree from possible Children // Exclude SiteTree from possible Children
$possibleChildren = array_diff($allowedChildren, array("SiteTree")); $possibleChildren = array_diff($allowedChildren, array("SiteTree"));
// Find i18n - names and build allowed children array // Find i18n - names and build allowed children array
foreach($possibleChildren as $child) { foreach($possibleChildren as $child) {
$instance = singleton($child); $instance = singleton($child);
if($instance instanceof HiddenClass) continue; if($instance instanceof HiddenClass) continue;
if(!$instance->canCreate()) continue; if(!$cacheCanCreate[$child]) continue;
// skip this type if it is restricted // skip this type if it is restricted
if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue; if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;
$title = $instance->i18n_singular_name(); $title = $instance->i18n_singular_name();
$def[$class]['allowedChildren'][] = array("ssclass" => $child, "ssname" => $title); $def[$class]['allowedChildren'][] = array("ssclass" => $child, "ssname" => $title);
}
$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
$defaultChild = $obj->defaultChild();
if($defaultChild != 'Page' && $defaultChild != null)
$def[$class]['defaultChild'] = $defaultChild;
$defaultParent = $obj->defaultParent();
$parent = SiteTree::get_by_link($defaultParent);
$id = $parent ? $parent->id : null;
if ($defaultParent != 1 && $defaultParent != null) $def[$class]['defaultParent'] = $defaultParent;
if(isset($def[$class]['disallowedChildren'])) {
foreach($def[$class]['disallowedChildren'] as $disallowedChild) {
$def[$disallowedChild]['disallowedParents'][] = $class;
} }
$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
$defaultChild = $obj->defaultChild();
if($defaultChild != 'Page' && $defaultChild != null) $def[$class]['defaultChild'] = $defaultChild;
$defaultParent = $obj->defaultParent();
$parent = SiteTree::get_by_link($defaultParent);
$id = $parent ? $parent->id : null;
if ($defaultParent != 1 && $defaultParent != null) $def[$class]['defaultParent'] = $defaultParent;
if(isset($def[$class]['disallowedChildren'])) {
foreach($def[$class]['disallowedChildren'] as $disallowedChild) {
$def[$disallowedChild]['disallowedParents'][] = $class;
}
}
// Are any classes allowed to be parents of root?
$def['Root']['disallowedParents'][] = $class;
} }
// Are any classes allowed to be parents of root? $json = Convert::raw2xml(Convert::raw2json($def));
$def['Root']['disallowedParents'][] = $class; $cache->save($json, $cacheKey);
} }
return $json;
return Convert::raw2xml(Convert::raw2json($def));
} }
/** /**

View File

@ -18,12 +18,15 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* subclasses are allowed. * subclasses are allowed.
* To control allowed children on root level (no parent), use {@link $can_be_root}. * To control allowed children on root level (no parent), use {@link $can_be_root}.
* *
* Note that this setting is cached when used in the CMS, use the "flush" query parameter to clear it.
*
* @var array * @var array
*/ */
static $allowed_children = array("SiteTree"); static $allowed_children = array("SiteTree");
/** /**
* The default child class for this page. * The default child class for this page.
* Note: Value might be cached, see {@link $allowed_chilren}.
* *
* @var string * @var string
*/ */
@ -31,6 +34,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
/** /**
* The default parent class for this page. * The default parent class for this page.
* Note: Value might be cached, see {@link $allowed_chilren}.
* *
* @var string * @var string
*/ */
@ -38,14 +42,15 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
/** /**
* Controls whether a page can be in the root of the site tree. * Controls whether a page can be in the root of the site tree.
* Note: Value might be cached, see {@link $allowed_chilren}.
* *
* @var bool * @var bool
*/ */
static $can_be_root = true; static $can_be_root = true;
/** /**
* List of permission codes a user can have to allow a user to create a * List of permission codes a user can have to allow a user to create a page of this type.
* page of this type. * Note: Value might be cached, see {@link $allowed_chilren}.
* *
* @var array * @var array
*/ */