Create a gist now

Instantly share code, notes, and snippets.

SimpleTreeDropdownField performance patch
--- 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)
@davidjeliba

Made an update to this gist here: https://gist.github.com/davidjeliba/635fc16df5c2952fafbc which generates the cache key in such a way that when content changes the cache doesn't have to be manually flushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment