Skip to content

Commit

Permalink
More work on cache
Browse files Browse the repository at this point in the history
  • Loading branch information
CatoTH committed Dec 22, 2024
1 parent af9e4d0 commit 0ff310d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 33 deletions.
26 changes: 21 additions & 5 deletions components/HashedStaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace app\components;

use app\models\backgroundJobs\IBackgroundJob;
use app\models\backgroundJobs\{BuildStaticCache, IBackgroundJob};
use app\models\settings\AntragsgruenApp;

class HashedStaticCache
Expand Down Expand Up @@ -94,7 +94,7 @@ public function getCacheKey(): string
return $this->cacheKey;
}

public function getCached(callable $method, ?callable $cacheOutdatedDecorator = null): mixed
public function getCached(?callable $method, ?callable $cacheOutdatedDecorator = null): mixed
{
if (!$this->skipCache) {
// Hint: don't even try to aquire a lock if a cache item already exists
Expand All @@ -110,16 +110,32 @@ public function getCached(callable $method, ?callable $cacheOutdatedDecorator =
// Check if the cache item has been generated in the meantime
$cached = $this->getCache();
if ($cached !== false) {
// @TODO Check age - trigger rebuild async or rebuild synchronously if too old
ResourceLock::unlockCache($this);
return $this->returnDecoratedCache($cached, $cacheOutdatedDecorator);
}

// @TODO call backgroundjob if set
if ($method) {
$result = $method();
} elseif ($this->rebuildBackgroundJob) {
$this->rebuildBackgroundJob->execute();

$result = $this->rebuildBackgroundJob->getResult();

Check failure on line 123 in components/HashedStaticCache.php

View workflow job for this annotation

GitHub Actions / evaluate-pr

Call to an undefined method app\models\backgroundJobs\IBackgroundJob::getResult().
} else {
throw new \Exception('Either a callback or a background job needs to be provided');
}

$result = $method();
ResourceLock::unlockCache($this);
} else {
$result = $method();
if ($method) {
$result = $method();
} elseif ($this->rebuildBackgroundJob) {
$this->rebuildBackgroundJob->execute();

$result = $this->rebuildBackgroundJob->getResult();

Check failure on line 135 in components/HashedStaticCache.php

View workflow job for this annotation

GitHub Actions / evaluate-pr

Call to an undefined method app\models\backgroundJobs\IBackgroundJob::getResult().
} else {
throw new \Exception('Either a callback or a background job needs to be provided');
}
}

if (!$this->skipCache) {
Expand Down
54 changes: 54 additions & 0 deletions models/backgroundJobs/BuildStaticCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace app\models\backgroundJobs;

use app\components\mail\Base;
use app\components\RequestContext;
use app\models\db\Consultation;
use app\models\db\EMailLog;
use app\models\exceptions\MailNotSent;
use app\models\settings\AntragsgruenApp;
use app\views\consultation\LayoutHelper;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;

class BuildStaticCache extends IBackgroundJob
{
public const TYPE_ID = 'BUILD_STATIC_CACHE';

public const CACHE_ID_CONSULTATION_HOME = 'CONSULTATION_HOME';

private ?string $result = null;

public function __construct(
public string $cacheId,
?Consultation $consultation,
public bool $adminMode
) {
$this->consultation = $consultation;
}

public function getTypeId(): string
{
return self::TYPE_ID;
}

public function execute(): void
{
$this->result = match ($this->cacheId) {
self::CACHE_ID_CONSULTATION_HOME => $this->getConsultationHome(),
default => null,
};
}

public function getResult(): ?string
{
return $this->result;
}

private function getConsultationHome(): string
{
return LayoutHelper::renderHomePageList($this->consultation, $this->adminMode);
}
}
1 change: 1 addition & 0 deletions models/backgroundJobs/IBackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function getAllBackgroundJobs(): array
{
return [
SendNotification::TYPE_ID => SendNotification::class,
BuildStaticCache::TYPE_ID => BuildStaticCache::class,
];
}

Expand Down
35 changes: 34 additions & 1 deletion views/consultation/LayoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace app\views\consultation;

use app\models\backgroundJobs\BuildStaticCache;
use app\components\{HashedStaticCache, HTMLTools, IMotionStatusFilter, MotionSorter, Tools, UrlHelper};
use app\models\IMotionList;
use app\models\settings\{AntragsgruenApp, Consultation as ConsultationSettings, Privileges};
Expand All @@ -26,14 +27,20 @@ private static function getHomePageCacheForType(Consultation $consultation, stri
$cache->setSkipCache(true);
}

if ($type === 'index_layout_agenda' && User::havePrivilege($consultation, Privileges::PRIVILEGE_CONTENT_EDIT, null)) {
$contentAdmin = User::havePrivilege($consultation, Privileges::PRIVILEGE_CONTENT_EDIT, null);

if ($type === 'index_layout_agenda' && $contentAdmin) {
$cache->setSkipCache(true);
}
if (!in_array($type, ['index_layout_std', 'index_layout_tags', 'index_layout_agenda', 'index_layout_discussion_tags'])) {
// Disable cache for plugin homepages, to prevent accidental over-caching
$cache->setSkipCache(true);
}

$cache->setRebuildBackgroundJob(
new BuildStaticCache(BuildStaticCache::CACHE_ID_CONSULTATION_HOME, $consultation, $contentAdmin)
);

return $cache;
}

Expand All @@ -54,6 +61,32 @@ public static function getHomePageCache(Consultation $consultation): HashedStati
return self::getHomePageCacheForType($consultation, $consultation->getSettings()->getStartLayoutView());
}

public static function renderHomePageList(Consultation $consultation, bool $contentAdmin): string
{
$output = '';
$resolutionMode = $consultation->getSettings()->startLayoutResolutions;
list($imotions, $resolutions) = MotionSorter::getIMotionsAndResolutions($consultation->motions);
if (count($resolutions) > 0 && $resolutionMode === ConsultationSettings::START_LAYOUT_RESOLUTIONS_ABOVE) {
$output .= \Yii::$app->controller->renderPartial('_index_resolutions', ['consultation' => $consultation, 'resolutions' => $resolutions]);
}

if (count($consultation->motionTypes) > 0 && $consultation->getSettings()->getStartLayoutView()) {
if ($resolutionMode === ConsultationSettings::START_LAYOUT_RESOLUTIONS_DEFAULT) {
$toShowImotions = $resolutions;
} else {
$toShowImotions = $imotions;
}
$output .= \Yii::$app->controller->renderPartial($consultation->getSettings()->getStartLayoutView(), [
'consultation' => $consultation,
'admin' => $contentAdmin,
'imotions' => $toShowImotions,
'isResolutionList' => ($resolutionMode === ConsultationSettings::START_LAYOUT_RESOLUTIONS_DEFAULT),
'skipTitle' => false,
]);
}
return $output;
}

public static function getTagMotionListCache(Consultation $consultation, ConsultationSettingsTag $tag, bool $isResolutionList): HashedStaticCache
{
$cache = HashedStaticCache::getInstance('tagMotionListCache', [
Expand Down
27 changes: 2 additions & 25 deletions views/consultation/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use app\models\db\{Amendment, AmendmentComment, AmendmentSupporter, Consultation, Motion, MotionComment, MotionSupporter, User};
use app\models\backgroundJobs\BuildStaticCache;
use app\components\{HashedStaticCache, MotionSorter, UrlHelper};
use app\models\settings\{Privileges, Consultation as ConsultationSettings};
use yii\helpers\Html;
Expand Down Expand Up @@ -147,31 +148,7 @@
$cache->setSkipCache(true);
}

echo $cache->getCached(function () use ($consultation, $layout, $contentAdmin) {
$output = '';
$resolutionMode = $consultation->getSettings()->startLayoutResolutions;
list($imotions, $resolutions) = MotionSorter::getIMotionsAndResolutions($consultation->motions);
if (count($resolutions) > 0 && $resolutionMode === ConsultationSettings::START_LAYOUT_RESOLUTIONS_ABOVE) {
$output .= $this->render('_index_resolutions', ['consultation' => $consultation, 'resolutions' => $resolutions]);
}

if (count($consultation->motionTypes) > 0 && $consultation->getSettings()->getStartLayoutView()) {
if ($resolutionMode === ConsultationSettings::START_LAYOUT_RESOLUTIONS_DEFAULT) {
$toShowImotions = $resolutions;
} else {
$toShowImotions = $imotions;
}
$output .= $this->render($consultation->getSettings()->getStartLayoutView(), [
'consultation' => $consultation,
'layout' => $layout,
'admin' => $contentAdmin,
'imotions' => $toShowImotions,
'isResolutionList' => ($resolutionMode === ConsultationSettings::START_LAYOUT_RESOLUTIONS_DEFAULT),
'skipTitle' => false,
]);
}
return $output;
});
echo $cache->getCached(null, null); // @TODO indicate age

echo $this->render('_index_private_comment_list', [
'myMotionComments' => $myMotionComments,
Expand Down
3 changes: 1 addition & 2 deletions views/consultation/index_layout_tags.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use app\models\sectionTypes\ISectionType;
use app\views\consultation\LayoutHelper;
use app\components\{MotionSorter, UrlHelper};
use app\models\layoutHooks\Layout as LayoutHooks;
use app\models\db\{Amendment, AmendmentComment, Consultation, ConsultationSettingsTag, IMotion, ISupporter, Motion, MotionComment, User};
use app\models\db\{Amendment, Consultation, ConsultationSettingsTag, IMotion, ISupporter, Motion};
use yii\helpers\Html;

/**
Expand Down

0 comments on commit 0ff310d

Please sign in to comment.