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 1889015
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 28 deletions.
8 changes: 7 additions & 1 deletion 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 @@ -129,6 +129,12 @@ public function getCached(callable $method, ?callable $cacheOutdatedDecorator =
return $result;
}

public function getBackgroundJobCached(BuildStaticCache $buildStaticCache): string
{
$buildStaticCache->execute();
return $buildStaticCache->getResult();
}

private static function getDirectory(string $key): string {
return AntragsgruenApp::getInstance()->viewCacheFilePath . '/' . substr($key, 0, 2);
}
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
26 changes: 26 additions & 0 deletions views/consultation/LayoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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
29 changes: 4 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,9 @@
$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->getBackgroundJobCached(
new BuildStaticCache(BuildStaticCache::CACHE_ID_CONSULTATION_HOME, $consultation, $contentAdmin)
);

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 1889015

Please sign in to comment.