Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

SimpleTreeDropdownField performance patch

View SimpleTreeDropdownField patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
--- 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)

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
Something went wrong with that request. Please try again.