mirror of
https://github.com/silverstripe/silverstripe-reports
synced 2024-10-22 11:05:53 +02:00
ENHANCEMENT Caching expensive CMSMain->SiteTreeHints() call on disk
This commit is contained in:
parent
d9c4aa8583
commit
2dc0e72c00
@ -244,14 +244,24 @@ 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') );
|
||||||
|
|
||||||
|
$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'] = array();
|
||||||
$def['Root']['disallowedParents'] = array();
|
$def['Root']['disallowedParents'] = array();
|
||||||
|
|
||||||
foreach($classes as $class) {
|
foreach($classes as $class) {
|
||||||
$obj = singleton($class);
|
$obj = singleton($class);
|
||||||
|
|
||||||
if($obj instanceof HiddenClass) continue;
|
if($obj instanceof HiddenClass) continue;
|
||||||
|
|
||||||
$allowedChildren = $obj->allowedChildren();
|
$allowedChildren = $obj->allowedChildren();
|
||||||
@ -268,7 +278,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
|
|||||||
|
|
||||||
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;
|
||||||
@ -280,20 +290,12 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
|
|||||||
|
|
||||||
$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
|
$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
|
||||||
if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
|
if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
|
||||||
|
|
||||||
$defaultChild = $obj->defaultChild();
|
$defaultChild = $obj->defaultChild();
|
||||||
|
if($defaultChild != 'Page' && $defaultChild != null) $def[$class]['defaultChild'] = $defaultChild;
|
||||||
if($defaultChild != 'Page' && $defaultChild != null)
|
|
||||||
$def[$class]['defaultChild'] = $defaultChild;
|
|
||||||
|
|
||||||
$defaultParent = $obj->defaultParent();
|
$defaultParent = $obj->defaultParent();
|
||||||
|
|
||||||
$parent = SiteTree::get_by_link($defaultParent);
|
$parent = SiteTree::get_by_link($defaultParent);
|
||||||
|
|
||||||
$id = $parent ? $parent->id : null;
|
$id = $parent ? $parent->id : null;
|
||||||
|
|
||||||
if ($defaultParent != 1 && $defaultParent != null) $def[$class]['defaultParent'] = $defaultParent;
|
if ($defaultParent != 1 && $defaultParent != null) $def[$class]['defaultParent'] = $defaultParent;
|
||||||
|
|
||||||
if(isset($def[$class]['disallowedChildren'])) {
|
if(isset($def[$class]['disallowedChildren'])) {
|
||||||
foreach($def[$class]['disallowedChildren'] as $disallowedChild) {
|
foreach($def[$class]['disallowedChildren'] as $disallowedChild) {
|
||||||
$def[$disallowedChild]['disallowedParents'][] = $class;
|
$def[$disallowedChild]['disallowedParents'][] = $class;
|
||||||
@ -304,7 +306,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
|
|||||||
$def['Root']['disallowedParents'][] = $class;
|
$def['Root']['disallowedParents'][] = $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Convert::raw2xml(Convert::raw2json($def));
|
$json = Convert::raw2xml(Convert::raw2json($def));
|
||||||
|
$cache->save($json, $cacheKey);
|
||||||
|
}
|
||||||
|
return $json;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user