Skip to content

Commit

Permalink
Enh #3715: ContentContainer caching
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh4 committed Feb 19, 2021
1 parent 4fefcfc commit 3d86c7f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Expand Up @@ -8,6 +8,7 @@

namespace humhub\modules\content\helpers;

use humhub\modules\content\models\ContentContainer;
use Yii;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentContainerController;
Expand All @@ -25,6 +26,32 @@ class ContentContainerHelper
*/
private static $container;

private static $CACHE_LIMIT = 500;

private static $cache = [];

public static function getContainerByContentContainerId(?int $contentContainerId)
{
$result = null;

if($contentContainerId === null) {
return $result;
}

if(isset(static::$cache[$contentContainerId])) {
return static::$cache[$contentContainerId];
}

$contentContainer = ContentContainer::findOne(['id' => $contentContainerId]);

if($contentContainer && count(static::$cache) < static::$CACHE_LIMIT) {
$result = $contentContainer->getPolymorphicRelation();
static::$cache[$contentContainerId] = $result;
}

return $result;
}

/**
* @param string|null $type type filter available since 1.4
* @return ContentContainerActiveRecord|null currently active container from app context.
Expand Down
12 changes: 12 additions & 0 deletions protected/humhub/modules/content/models/Content.php
Expand Up @@ -16,6 +16,7 @@
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentContainerModule;
use humhub\modules\content\helpers\ContentContainerHelper;
use humhub\modules\content\interfaces\ContentOwner;
use humhub\modules\content\live\NewContent;
use humhub\modules\content\permissions\CreatePrivateContent;
Expand Down Expand Up @@ -614,6 +615,8 @@ public function setContainer(ContentContainerActiveRecord $container)
}
}

public static $USE_CACHE = false;

/**
* Returns the content container (e.g. space or user record) of this content
*
Expand All @@ -626,6 +629,15 @@ public function getContainer()
return $this->_container;
}

if(static::$USE_CACHE) {
$this->_container = ContentContainerHelper::getContainerByContentContainerId($this->contentcontainer_id);
return $this->_container;
}

if ($this->_container != null) {
return $this->_container;
}

if ($this->contentContainer !== null) {
$this->_container = $this->contentContainer->getPolymorphicRelation();
}
Expand Down

0 comments on commit 3d86c7f

Please sign in to comment.