Skip to content

Commit 1a6e2dd

Browse files
jwadhamsclaude
andcommitted
feat: add getCacheTags() method to replace direct property access
Implementers can now override getCacheTags() to derive tags from other properties rather than setting state in the constructor. The property remains as the default backing store but is no longer accessed directly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 881a4bf commit 1a6e2dd

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

app/AbstractRequest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ abstract class AbstractRequest
4646
/** @var array Tags to be used when inserting to cache */
4747
protected array $cacheTags = [];
4848

49+
public function getCacheTags(): array
50+
{
51+
return $this->cacheTags;
52+
}
53+
4954
/** @var string Request method, for use in toGuzzle */
5055
protected string $method = 'POST';
5156

@@ -317,7 +322,7 @@ protected function getGuzzleClient(): Client
317322

318323
public function purgeCache(): self
319324
{
320-
Cache::tags($this->cacheTags)->forget($this->cacheKey());
325+
Cache::tags($this->getCacheTags())->forget($this->cacheKey());
321326
return $this;
322327
}
323328

@@ -338,7 +343,7 @@ protected function writeResponseToCache(): void
338343
// so we flatten the body to strings then rehydrate the Response class manually
339344
// Note, when the format of the cached value changes, you have to update CACHE_KEY_SEED
340345
// So that previous cache entries with incompatible cached data are not read by responseFromCache
341-
Cache::tags($this->cacheTags)->put(
346+
Cache::tags($this->getCacheTags())->put(
342347
$this->cacheKey(),
343348
[
344349
'logs' => $this->sentLogs,
@@ -363,7 +368,7 @@ protected function responseFromCache(): ?Response
363368

364369
// Note, when the format of $fromCache changes, you have to update CACHE_KEY_SEED
365370
// So that previous cache entries with incompatible cached data are not read by responseFromCache
366-
$fromCache = Cache::tags($this->cacheTags)->get($this->cacheKey());
371+
$fromCache = Cache::tags($this->getCacheTags())->get($this->cacheKey());
367372
if ($fromCache) {
368373
$this->sentLogs = array_map(
369374
fn ($filename) => Str::contains($filename, '/')
@@ -396,7 +401,7 @@ public function cacheKey(): string
396401

397402
public function canBeFulfilledByCache(): bool
398403
{
399-
return Cache::tags($this->cacheTags)->has($this->cacheKey());
404+
return Cache::tags($this->getCacheTags())->has($this->cacheKey());
400405
}
401406

402407
public function isFromCache(): bool

app/AbstractUseStaleRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function responseFromCache(): ?Response
2121
{
2222
$cachedResponse = parent::responseFromCache();
2323
if ($cachedResponse && $this->needsRefresh()) {
24-
Cache::tags($this->cacheTags)->put(
24+
Cache::tags($this->getCacheTags())->put(
2525
$this->refreshCacheKey(),
2626
'Wait between refreshes',
2727
$this->waitBetweenRefreshes(),
@@ -44,7 +44,7 @@ protected function responseFromCache(): ?Response
4444
protected function writeResponseToCache(): void
4545
{
4646
if ($this->shouldWriteResponseToCache()) {
47-
Cache::tags($this->cacheTags)->put($this->refreshCacheKey(), 'refresh after', $this->refreshAfter());
47+
Cache::tags($this->getCacheTags())->put($this->refreshCacheKey(), 'refresh after', $this->refreshAfter());
4848
}
4949
parent::writeResponseToCache();
5050
}
@@ -63,12 +63,12 @@ public function refreshCacheKey(): string
6363

6464
public function needsRefresh(): bool
6565
{
66-
return !Cache::tags($this->cacheTags)->has($this->refreshCacheKey());
66+
return !Cache::tags($this->getCacheTags())->has($this->refreshCacheKey());
6767
}
6868

6969
public function refreshOnNextRequest(): self
7070
{
71-
Cache::tags($this->cacheTags)->forget($this->refreshCacheKey());
71+
Cache::tags($this->getCacheTags())->forget($this->refreshCacheKey());
7272
return $this;
7373
}
7474

app/Testing/RequestClassAssertions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ protected static function mockRequestCachedResponse(
2121
array $headers = [],
2222
array $logs = [],
2323
): void {
24-
$tags = getProperty($request, 'cacheTags');
25-
Cache::tags($tags)->put($request->cacheKey(), [
24+
Cache::tags($request->getCacheTags())->put($request->cacheKey(), [
2625
'logs' => $logs,
2726
'response' => [$status, $headers, $body],
2827
]);

0 commit comments

Comments
 (0)