Skip to content

Commit

Permalink
[5.x] Prevent excessive nocache cache growth (#9999)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed May 7, 2024
1 parent b199ddf commit a32c923
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/StaticCaching/NoCache/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Session

protected $url;

private $regionCount = 0;

public function __construct($url)
{
$this->url = $url;
Expand Down Expand Up @@ -55,6 +57,13 @@ public function region(string $key): Region
throw new RegionNotFound($key);
}

public function getRegionId(): string
{
$this->regionCount += 1;

return md5($this->url.$this->regionCount);
}

public function pushRegion($contents, $context, $extension): StringRegion
{
$region = new StringRegion($this, trim($contents), $context, $extension);
Expand Down
4 changes: 1 addition & 3 deletions src/StaticCaching/NoCache/StringRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Statamic\StaticCaching\NoCache;

use Statamic\Support\Str;

class StringRegion extends Region
{
protected $content;
Expand All @@ -15,7 +13,7 @@ public function __construct(Session $session, string $content, array $context, s
$this->content = $content;
$this->context = $this->filterContext($context);
$this->extension = $extension;
$this->key = sha1($content.Str::random());
$this->key = sha1($content).$session->getRegionId();
}

public function key(): string
Expand Down
4 changes: 1 addition & 3 deletions src/StaticCaching/NoCache/ViewRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Statamic\StaticCaching\NoCache;

use Statamic\Support\Str;

class ViewRegion extends Region
{
protected $view;
Expand All @@ -13,7 +11,7 @@ public function __construct(Session $session, string $view, array $context)
$this->session = $session;
$this->view = $view;
$this->context = $this->filterContext($context);
$this->key = Str::random(32);
$this->key = sha1($view).$session->getRegionId();
}

public function key(): string
Expand Down

0 comments on commit a32c923

Please sign in to comment.