Skip to content

Commit 2573f18

Browse files
committed
Refactor cache interface and implementations to return boolean for set method; update response type hints in caching methods
1 parent 1344108 commit 2573f18

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

src/Fetch/Cache/CacheInterface.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ public function get(string $key): ?CachedResponse;
2121
* Store a response in the cache.
2222
*
2323
* @param string $key The cache key
24-
* @param CachedResponse $response The response to cache
25-
* @param int|null $ttl Time to live in seconds:
24+
* @param CachedResponse $cachedResponse The response to cache
25+
* @param int|null $ttl Time to live in seconds
26+
* @return bool True if the item was stored, false otherwise
27+
*/
28+
public function set(string $key, CachedResponse $cachedResponse, ?int $ttl = null): bool;
29+
30+
/**
31+
* Delete a cached response by key.
32+
*
2633
* @param string $key The cache key
2734
* @return bool True if the item was deleted, false otherwise
2835
*/

src/Fetch/Cache/FileCache.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function get(string $key): ?CachedResponse
8989
/**
9090
* {@inheritdoc}
9191
*/
92-
public function set(string $key, CachedResponse $response, ?int $ttl = null): void
92+
public function set(string $key, CachedResponse $response, ?int $ttl = null): bool
9393
{
9494
$this->ensureDirectoryExists();
9595

@@ -144,6 +144,8 @@ public function set(string $key, CachedResponse $response, ?int $ttl = null): vo
144144
if ($result === false) {
145145
throw new RuntimeException("Failed to write cache file: {$path}");
146146
}
147+
148+
return true;
147149
}
148150

149151
/**

src/Fetch/Cache/MemoryCache.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function get(string $key): ?CachedResponse
5959
/**
6060
* {@inheritdoc}
6161
*/
62-
public function set(string $key, CachedResponse $response, ?int $ttl = null): void
62+
public function set(string $key, CachedResponse $response, ?int $ttl = null): bool
6363
{
6464
// Ensure we don't exceed max items
6565
if (count($this->cache) >= $this->maxItems && ! isset($this->cache[$key])) {
@@ -83,6 +83,8 @@ public function set(string $key, CachedResponse $response, ?int $ttl = null): vo
8383
'response' => $response,
8484
'expires_at' => $expiresAt,
8585
];
86+
87+
return true;
8688
}
8789

8890
/**

src/Fetch/Concerns/ManagesCache.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Fetch\Cache\MemoryCache;
1212
use Fetch\Http\Response;
1313
use Fetch\Interfaces\ClientHandler;
14+
use Fetch\Interfaces\Response as ResponseInterface;
1415

1516
/**
1617
* Trait for managing HTTP caching.
@@ -188,7 +189,7 @@ protected function getCachedResponse(string $method, string $uri, array $options
188189
*
189190
* @param array<string, mixed> $options Request options
190191
*/
191-
protected function cacheResponse(string $method, string $uri, Response $response, array $options = []): void
192+
protected function cacheResponse(string $method, string $uri, ResponseInterface $response, array $options = []): void
192193
{
193194
if ($this->cache === null || ! $this->isCacheableMethod($method)) {
194195
return;
@@ -225,7 +226,7 @@ protected function cacheResponse(string $method, string $uri, Response $response
225226
*
226227
* @param array<string, mixed> $options Request options
227228
*/
228-
protected function calculateTtl(Response $response, CacheControl $cacheControl, array $options = []): ?int
229+
protected function calculateTtl(ResponseInterface $response, CacheControl $cacheControl, array $options = []): ?int
229230
{
230231
// Check for per-request TTL
231232
$cacheConfig = $options['cache'] ?? [];
@@ -280,7 +281,7 @@ protected function addConditionalHeaders(array $options, ?CachedResponse $cached
280281
/**
281282
* Handle a 304 Not Modified response.
282283
*/
283-
protected function handleNotModified(CachedResponse $cached, Response $response): Response
284+
protected function handleNotModified(CachedResponse $cached, ResponseInterface $response): Response
284285
{
285286
// Create a new response with the cached body but potentially updated headers
286287
$headers = $cached->getHeaders();

src/Fetch/Concerns/PerformsHttpRequests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getEffectiveTimeout(): int
273273
* @param array<string, mixed> $options The Guzzle options
274274
* @param float $startTime The request start time
275275
* @param array<string, mixed>|null $cachedResult The cached result data
276-
* @param mixed $handler The cloned handler instance with request-specific state
276+
* @param self $handler The cloned handler instance with request-specific state
277277
* @return ResponseInterface The response
278278
*/
279279
protected function executeSyncRequestWithCache(
@@ -282,7 +282,7 @@ protected function executeSyncRequestWithCache(
282282
array $options,
283283
float $startTime,
284284
?array $cachedResult,
285-
mixed $handler
285+
self $handler
286286
): ResponseInterface {
287287
try {
288288
$response = $handler->executeSyncRequest($method, $uri, $options, $startTime);

0 commit comments

Comments
 (0)