Skip to content

Commit

Permalink
Allow to skip rebuild of homepage cache
Browse files Browse the repository at this point in the history
  • Loading branch information
CatoTH committed Jan 19, 2025
1 parent 62cb5ce commit 42485b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
24 changes: 22 additions & 2 deletions components/HashedStaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class HashedStaticCache
private bool $isBulky = false;
private bool $isSynchronized = false;
private bool $skipCache = false;
private bool $noFlush = false;
private bool $forceRebuild = false;
private ?int $timeout = null;

public function __construct(string $functionName, ?array $dependencies)
Expand Down Expand Up @@ -46,6 +48,20 @@ public function isSkipCache(): bool
return $this->skipCache;
}

public function setNoFlush(bool $noFlush): self
{
$this->noFlush = $noFlush;

return $this;
}

public function forceRebuild(): self
{
$this->forceRebuild = true;

return $this;
}

public function setIsBulky(bool $isBulky): self
{
$this->isBulky = $isBulky;
Expand Down Expand Up @@ -89,7 +105,7 @@ public function getCached(callable $method): mixed
if (!$this->skipCache) {
// Hint: don't even try to aquire a lock if a cache item already exists
$cached = $this->getCache();
if ($cached !== false) {
if ($cached !== false && !$this->forceRebuild) {
return $cached;
}
}
Expand All @@ -99,7 +115,7 @@ public function getCached(callable $method): mixed

// Check if the cache item has been generated in the meantime
$cached = $this->getCache();
if ($cached !== false) {
if ($cached !== false && !$this->forceRebuild) {
ResourceLock::unlockCache($this);
return $cached;
}
Expand Down Expand Up @@ -164,6 +180,10 @@ public function setCache(mixed $data): void

public function flushCache(): void
{
if ($this->noFlush) {
return;
}

if ($this->isBulky && AntragsgruenApp::getInstance()->viewCacheFilePath) {
$directory = self::getDirectory($this->cacheKey);
if (file_exists($directory . '/' . $this->cacheKey)) {
Expand Down
1 change: 1 addition & 0 deletions config/urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
$consultationPaths = 'search|maintenance|notifications|activitylog|collecting|save-agenda-item-ajax|del-agenda-item-ajax|save-agenda-order-ajax';
$consultationPaths .= '|resolutions|motions|todo|todo-count|votings|voting-results|feeds|feedall|feedmotions|feedamendments|feedcomments';
$consultationPaths .= '|speech|admin-speech|admin-votings|proposed-procedure|proposed-procedure-ajax|debugbar-ajax';
$consultationPaths .= '|rebuild-homepage';
$motionPaths = 'createconfirm|createdone|edit|pdf|pdfamendcollection|pdfembed|odt|plainhtml|viewimage|viewpdf|embeddedpdf|embedded-amendments-pdf';
$motionPaths .= '|admin-speech|withdraw|view-changes|view-changes-odt';
$motionPaths .= '|save-proposal-status|edit-proposed-change|del-proposal-comment|save-editorial';
Expand Down
24 changes: 23 additions & 1 deletion controllers/ConsultationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
RedirectResponse,
ResponseInterface,
RestApiResponse};
use app\components\{DateTools, RSSExporter, Tools, UrlHelper};
use app\components\{DateTools, HashedStaticCache, RSSExporter, Tools, UrlHelper};
use app\models\db\{Amendment,
AmendmentComment,
IComment,
Expand Down Expand Up @@ -311,6 +311,28 @@ public function actionIndex(): ResponseInterface
$this->consultation->preloadAllMotionData(Consultation::PRELOAD_ONLY_AMENDMENTS);
}

return $this->renderHomePage($cache);
}

public function actionRebuildHomepage(): ResponseInterface
{
$user = User::getCurrentUser();
if ($user) {
return new HtmlResponse('Diesen Link bitte nicht eingeloggt öffnen, z.B. in einem privaten Fenster');
}

$cache = LayoutHelper::getHomePageCache($this->consultation);
$cache->forceRebuild();

if ($cache->isSkipCache() || !$cache->cacheIsFilled()) {
$this->consultation->preloadAllMotionData(Consultation::PRELOAD_ONLY_AMENDMENTS);
}

return $this->renderHomePage($cache);
}

private function renderHomePage(HashedStaticCache $cache): HtmlResponse
{
$this->layout = 'column2';
$this->consultationSidebar($this->consultation);

Expand Down
1 change: 1 addition & 0 deletions views/consultation/LayoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private static function getHomePageCacheForType(Consultation $consultation, stri
if (AntragsgruenApp::getInstance()->viewCacheFilePath) {
$cache->setIsSynchronized(true);
$cache->setIsBulky(true);
$cache->setNoFlush(true);
} else {
$cache->setSkipCache(true);
}
Expand Down

0 comments on commit 42485b3

Please sign in to comment.