Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

SimpleTreeDropdownField Speedup?


Reply


2 Posts   763 Views

Avatar
Tobi Oetiker

Community Member, 6 Posts

13 April 2011 at 10:10am

I am supporting this site, where there are several instances of SimpleTreeDropdownField used in a Widget configuration screen.
The widget is used on most pages. Since the site is quite big, this has the unfortunate effect of makeing the navigation in the CMS pretty slow, as the SimpleTreeDropdownField gets generated every time the page switches ...

I have enabled the $cache property when creating the object, but this does not have any significant effect ...

Is there a way to add caching the parts of the cms screen ? after all the widget is always the same ...

cheers
tobi

Avatar
Tobi Oetiker

Community Member, 6 Posts

13 April 2011 at 9:48pm

Edited: 13/04/2011 9:51pm

I wrote this bit here ... would you be interested in integrating it ? Is there some hook to flush the cache when the site tree changes?

I put a copy on github https://gist.github.com/917288

--- SimpleTreeDropdownField.php 2011-03-21 21:54:18.000000000 +0100
+++ /home/speagwww/public_html/dataobject_manager/code/dropdown_fields/SimpleTreeDropdownField.php 2011-04-13 11:44:38.000000000 +0200
@@ -3,9 +3,8 @@
class SimpleTreeDropdownField extends HTMLDropdownField
{
protected $sourceClass, $labelField, $parentID, $useCache, $filter;
- private static $cache = array();

- function __construct($name, $title = "", $sourceClass = "SiteTree", $value = "", $labelField = "Title", $form = null, $emptyString = null, $parentID = 0, $cache = false)
+ function __construct($name, $title = "", $sourceClass = "SiteTree", $value = "", $labelField = "Title", $form = null, $emptyString = null, $parentID = 0, $cache = true)
{
$this->labelField = $labelField;
$this->parentID = $parentID;
@@ -44,13 +43,16 @@

private function getCachedHierarchy($parentID) {
$class = ($this->sourceClass == "SiteTree" || is_subclass_of($this->sourceClass, "SiteTree")) ? "SiteTree" : $this->sourceClass;
- if (!isset(self::$cache[$class][$parentID])) {
- if (!isset(self::$cache[$class])) {
- self::$cache[$class] = array();
- }
- self::$cache[$class][$parentID] = $this->getHierarchy($parentID);
- }
- return self::$cache[$class][$parentID];
+ $filter = $this->filter ? $this->filter : 'x';
+ $cache = SS_Cache::factory('HierarchyCache');
+ $key = preg_replace('/[^a-zA-Z0-9]/','_',$class."_".$parentID."_".$filter);
+ if (!$data = unserialize($cache->load($key))){
+ $data = $this->getHierarchy($parentID);
+// Debug::show($data);
+ $cache->save(serialize($data),$key);
+// Debug::message("Caching ".$key);
+ }
+ return $data;
}

private function getHierarchy($parentID, $level = 0)