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,14 +244,24 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
* @return String Serialized JSON
*/
public function SiteTreeHints() {
$json = '';
$classes = ClassInfo::subclassesFor( $this->stat('tree_class') );
$cacheCanCreate = array();
foreach($classes as $class) $cacheCanCreate[$class] = singleton($class)->canCreate();
// Generate basic cache key. Too complex to encompass all variations
$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();
foreach($classes as $class) {
$obj = singleton($class);
if($obj instanceof HiddenClass) continue;
$allowedChildren = $obj->allowedChildren();
@ -268,7 +278,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
if($instance instanceof HiddenClass) continue;
if(!$instance->canCreate()) continue;
if(!$cacheCanCreate[$child]) continue;
// skip this type if it is restricted
if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;
@ -280,20 +290,12 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$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;
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;
@ -304,7 +306,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$def['Root']['disallowedParents'][] = $class;
}
return Convert::raw2xml(Convert::raw2json($def));
$json = Convert::raw2xml(Convert::raw2json($def));
$cache->save($json, $cacheKey);
}
return $json;
}
/**

View File

@ -18,12 +18,15 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* subclasses are allowed.
* 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
*/
static $allowed_children = array("SiteTree");
/**
* The default child class for this page.
* Note: Value might be cached, see {@link $allowed_chilren}.
*
* @var string
*/
@ -31,6 +34,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
/**
* The default parent class for this page.
* Note: Value might be cached, see {@link $allowed_chilren}.
*
* @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.
* Note: Value might be cached, see {@link $allowed_chilren}.
*
* @var bool
*/
static $can_be_root = true;
/**
* List of permission codes a user can have to allow a user to create a
* page of this type.
* List of permission codes a user can have to allow a user to create a page of this type.
* Note: Value might be cached, see {@link $allowed_chilren}.
*
* @var array
*/