Skip to content
Snippets Groups Projects
Commit 2bbcfb7b authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

proposal for caching, re #3234

Merge request studip/studip!2194
parent 9e3cfdbe
No related branches found
No related tags found
No related merge requests found
<?php
trait StudipTreeNodeCachableTrait
{
protected static $descendants_cache_array = null;
protected static function getDescendantsCacheArray(): StudipCachedArray
{
if (self::$descendants_cache_array === null) {
self::$descendants_cache_array = new StudipCachedArray(
static::class . '/descendants',
30 * 60
);
}
return self::$descendants_cache_array;
}
protected static function registerCachableCallbacks(array $config): array
{
if (!isset($config['registered_callbacks'])) {
$config['registered_callbacks'] = [];
}
if (!isset($config['registered_callbacks']['before_store'])) {
$config['registered_callbacks']['before_store'] = [];
}
$config['registered_callbacks']['before_store'][] = function ($node): void {
self::getDescendantsCacheArray()->expire();
};
if (!isset($config['registered_callbacks']['after_delete'])) {
$config['registered_callbacks']['after_delete'] = [];
}
$config['registered_callbacks']['after_delete'][] = function ($node): void {
self::getDescendantsCacheArray()->expire();
};
return $config;
}
protected function getDescendantIds(): array
{
$cache = self::getDescendantsCacheArray();
if (isset($cache[$this->id])) {
return $cache[$this->id];
}
$ids = [];
foreach ($this->getChildNodes() as $child) {
$ids = array_merge($ids, [$child->id], $child->getDescendantIds());
}
$cache[$this->id] = $ids;
return $ids;
}
}
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
*/ */
class RangeTreeNode extends SimpleORMap implements StudipTreeNode class RangeTreeNode extends SimpleORMap implements StudipTreeNode
{ {
use StudipTreeNodeCachableTrait;
protected static function configure($config = []) protected static function configure($config = [])
{ {
$config['db_table'] = 'range_tree'; $config['db_table'] = 'range_tree';
...@@ -41,12 +43,13 @@ class RangeTreeNode extends SimpleORMap implements StudipTreeNode ...@@ -41,12 +43,13 @@ class RangeTreeNode extends SimpleORMap implements StudipTreeNode
]; ];
$config['has_many']['children'] = [ $config['has_many']['children'] = [
'class_name' => RangeTreeNode::class, 'class_name' => RangeTreeNode::class,
'foreign_key' => 'item_id',
'assoc_foreign_key' => 'parent_id', 'assoc_foreign_key' => 'parent_id',
'order_by' => 'ORDER BY priority, name', 'order_by' => 'ORDER BY priority, name',
'on_delete' => 'delete' 'on_delete' => 'delete',
]; ];
$config = self::registerCachableCallbacks($config);
parent::configure($config); parent::configure($config);
} }
...@@ -240,17 +243,6 @@ class RangeTreeNode extends SimpleORMap implements StudipTreeNode ...@@ -240,17 +243,6 @@ class RangeTreeNode extends SimpleORMap implements StudipTreeNode
return DBManager::get()->fetchAll($query, $parameters, 'Course::buildExisting'); return DBManager::get()->fetchAll($query, $parameters, 'Course::buildExisting');
} }
public function getDescendantIds()
{
$ids = [];
foreach ($this->children as $child) {
$ids = array_merge($ids, [$child->id], $child->getDescendantIds());
}
return $ids;
}
public function getAncestors(): array public function getAncestors(): array
{ {
$path = [ $path = [
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
class StudipStudyArea extends SimpleORMap implements StudipTreeNode class StudipStudyArea extends SimpleORMap implements StudipTreeNode
{ {
use StudipTreeNodeCachableTrait;
/** /**
* This constant represents the key of the root area. * This constant represents the key of the root area.
*/ */
...@@ -53,6 +55,9 @@ class StudipStudyArea extends SimpleORMap implements StudipTreeNode ...@@ -53,6 +55,9 @@ class StudipStudyArea extends SimpleORMap implements StudipTreeNode
'class_name' => StudipStudyArea::class, 'class_name' => StudipStudyArea::class,
'foreign_key' => 'parent_id', 'foreign_key' => 'parent_id',
]; ];
$config = self::registerCachableCallbacks($config);
parent::configure($config); parent::configure($config);
} }
...@@ -64,7 +69,7 @@ class StudipStudyArea extends SimpleORMap implements StudipTreeNode ...@@ -64,7 +69,7 @@ class StudipStudyArea extends SimpleORMap implements StudipTreeNode
/** /**
* Returns the children of the study area with the specified ID. * Returns the children of the study area with the specified ID.
*/ */
static function findByParent($parent_id) public static function findByParent($parent_id)
{ {
return self::findByparent_id($parent_id, "ORDER BY priority,name"); return self::findByparent_id($parent_id, "ORDER BY priority,name");
} }
...@@ -601,17 +606,6 @@ class StudipStudyArea extends SimpleORMap implements StudipTreeNode ...@@ -601,17 +606,6 @@ class StudipStudyArea extends SimpleORMap implements StudipTreeNode
return $path; return $path;
} }
private function getDescendantIds()
{
$ids = [];
foreach ($this->_children as $child) {
$ids = array_merge($ids, [$child->id], $child->getDescendantIds());
}
return $ids;
}
/** /**
* Constructs an index from the level hierarchy, This index is a number, * Constructs an index from the level hierarchy, This index is a number,
* containing the "depth" level and the priority on this level. For example, * containing the "depth" level and the priority on this level. For example,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment