*/ private array $cache = []; public function __construct( private readonly BlocklistBuilder $builder, private readonly Clock $clock, private readonly int $ttlSeconds = 30, ) { } public function getOrBuild(Policy $policy): Blocklist { $now = (float) $this->clock->now()->getTimestamp(); $entry = $this->cache[$policy->id] ?? null; if ($entry !== null && $now < $entry['expires_at']) { return $entry['blocklist']; } $blocklist = $this->builder->build($policy); if ($this->ttlSeconds > 0) { $this->cache[$policy->id] = [ 'blocklist' => $blocklist, 'expires_at' => $now + $this->ttlSeconds, ]; } return $blocklist; } public function invalidate(int $policyId): void { unset($this->cache[$policyId]); } public function invalidateAll(): void { $this->cache = []; } }