Skip to content

Commit

Permalink
Merge pull request #6 from andersundsehr/feature/cacheLifeTime
Browse files Browse the repository at this point in the history
FEATURE makes cache lifetime configurable
  • Loading branch information
weakbit authored Jun 3, 2024
2 parents bbb93da + b6c7cf8 commit f3988a5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Classes/ViewHelpers/RenderIncludeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('name', 'string', 'Specifies the file name of the cache (without .html ending)', true);
$this->registerArgument('cacheLifeTime', 'int', 'Specifies the lifetime in seconds (defaults to 300)', false, 300);
}

/**
Expand All @@ -44,7 +45,7 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
$filename = static::getSiteName() . '_' . static::getLangauge() . '_' . $name;
$basePath = self::SSI_INCLUDE_DIR . $filename;
$absolutePath = Environment::getPublicPath() . $basePath;
if (self::shouldRenderFile($absolutePath)) {
if (self::shouldRenderFile($absolutePath, $arguments['cacheLifeTime'])) {
$html = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
if (self::isBackendUser()) {
return $html;
Expand All @@ -62,12 +63,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
return '<!--# include wait="yes" virtual="' . $basePath . '?ssi_include=' . $filename . '" -->';
}

private static function shouldRenderFile(string $absolutePath): bool
private static function shouldRenderFile(string $absolutePath, int $cacheLifeTime): bool
{
if (!file_exists($absolutePath)) {
return true;
}
$cacheLifeTime = 5 * 60; // 5min TODO
if ((filemtime($absolutePath) + $cacheLifeTime) < time()) {
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ location @sfc {
And now the fun part. You can replace any partial rendering with the ViewHelper ``s:renderInclude``.
That Partial will only be rendered once every 5 minutes for the complete Site (Site Configuration Site (not Page)).
The only differentiation will be done by **site config**, **language** and the provided **name**.
Optionally, you can add **cacheLifeTime** to define the lifetime of the partial in seconds.
If you include want to render the same partial with diffrent arguments it will still be the same content if you have the same name.

#### before:
Expand All @@ -84,7 +85,7 @@ If you include want to render the same partial with diffrent arguments it will s

<f:section name="Main">
<div class="something something">
<s:renderInclude name="mainMenu" partial="Menus/MainMenu" arguments="{_all}"/>
<s:renderInclude name="mainMenu" cacheLifeTime="900" partial="Menus/MainMenu" arguments="{_all}"/>
</div>
</f:section>
````
Expand Down

0 comments on commit f3988a5

Please sign in to comment.