Skip to content

Commit c351570

Browse files
committed
wip
1 parent 8d6ffc7 commit c351570

31 files changed

Lines changed: 206 additions & 291 deletions

config/responsecache.php

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,35 @@
66
*/
77
'enabled' => env('RESPONSE_CACHE_ENABLED', true),
88

9-
/*
10-
* Cache configuration settings
11-
*/
129
'cache' => [
1310
/*
14-
* Here you may define the cache store that should be used to store
15-
* requests. This can be the name of any store that is
16-
* configured in app/config/cache.php
11+
* Here you may define the cache store that should be used
12+
* to store requests. This can be the name of any store
13+
* that is configured in your app's cache.php config
1714
*/
1815
'store' => env('RESPONSE_CACHE_DRIVER', 'file'),
1916

2017
/*
21-
* When using the default CacheRequestFilter this setting controls the
22-
* default number of seconds responses must be cached.
18+
* The default number of seconds responses will be cached
19+
* when using the default CacheProfile settings.
2320
*/
2421
'lifetime_in_seconds' => (int) env('RESPONSE_CACHE_LIFETIME', 60 * 60 * 24 * 7),
2522

2623
/*
27-
* If the cache driver you configured supports tags, you may specify a tag name
28-
* here. All responses will be tagged. When clearing the responsecache only
29-
* items with that tag will be flushed.
24+
* If your cache driver supports tags, you may specify a tag
25+
* name here. All responses will be tagged. When clearing
26+
* the responsecache only items with that tag flushed.
3027
*
3128
* You may use a string or an array here.
3229
*/
3330
'tag' => env('RESPONSE_CACHE_TAG', ''),
3431
],
3532

36-
/*
37-
* Cache bypass header configuration
38-
*/
3933
'bypass' => [
4034
/*
41-
* Whether cache bypass functionality is enabled
42-
*/
43-
'enabled' => env('CACHE_BYPASS_HEADER_NAME') !== null,
44-
45-
/*
46-
* The header name that will force a cache bypass.
47-
* This can be useful to monitor the performance of your application.
35+
* The header name that will force a bypass of the cache.
36+
* This is useful when you want to see the performance
37+
* of your application without the caching enabled.
4838
*/
4939
'header_name' => env('CACHE_BYPASS_HEADER_NAME'),
5040

@@ -54,42 +44,42 @@
5444
'header_value' => env('CACHE_BYPASS_HEADER_VALUE'),
5545
],
5646

57-
/*
58-
* Debug and development settings
59-
*/
6047
'debug' => [
6148
/*
62-
* This setting determines if a http header named with the cache time
63-
* should be added to a cached response. This can be handy when
64-
* debugging.
49+
* Determines if debug headers are added to cached
50+
* responses. This can be handy for debugging how
51+
* response caching is performing in your app.
52+
*/
53+
'enabled' => env('APP_DEBUG', false),
54+
55+
/*
56+
* The name of the http header containing the
57+
* point at which the response was cached.
6558
*/
66-
'add_time_header' => env('APP_DEBUG', false),
59+
'cache_time_header_name' => 'X-Cache-Time',
6760

6861
/*
69-
* This setting determines the name of the http header that contains
70-
* the time at which the response was cached
62+
* The name of the header for the cache status that
63+
* indicates whether a response was HIT or MISS.
7164
*/
72-
'time_header_name' => env('RESPONSE_CACHE_HEADER_NAME', 'laravel-responsecache'),
65+
'cache_status_header_name' => 'X-Cache-Status',
7366

7467
/*
75-
* This setting determines if a http header named with the cache age
76-
* should be added to a cached response. This can be handy when
77-
* debugging.
78-
* ONLY works when "add_time_header" is also active!
68+
* The header name for the cache age in seconds.
7969
*/
80-
'add_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false),
70+
'cache_age_header_name' => 'X-Cache-Age',
8171

8272
/*
83-
* This setting determines the name of the http header that contains
84-
* the age of cache
73+
* The header name used for the response cache key.
74+
* This is only added when app.debug is enabled.
8575
*/
86-
'age_header_name' => env('RESPONSE_CACHE_AGE_HEADER_NAME', 'laravel-responsecache-age'),
76+
'cache_key_header_name' => 'X-Cache-Key',
8777
],
8878

8979
/*
90-
* Query parameters listed here will be ignored when generating the cache key.
91-
* This is useful for tracking parameters like UTM tags or gclid that don't
92-
* affect the page content but would otherwise create separate cache entries.
80+
* These query parameters will be ignored when generating
81+
* the cache key. This is useful for ignoring tracking
82+
* parameters like UTM tags, gclid and also fbclid.
9383
*/
9484
'ignored_query_parameters' => [
9585
'utm_source',
@@ -102,21 +92,15 @@
10292
],
10393

10494
/*
105-
* Customization - Class implementations
106-
*/
107-
108-
/*
109-
* The given class will determinate if a request should be cached. The
110-
* default class will cache all successful GET-requests.
111-
*
112-
* You can provide your own class given that it implements the
113-
* CacheProfile interface.
95+
* The given class determines if a request should be cached.
96+
* By default all successful GET-requests will be cached.
97+
* You can provide your own by using the CacheProfile.
11498
*/
11599
'cache_profile' => Spatie\ResponseCache\CacheProfiles\CacheAllSuccessfulGetRequests::class,
116100

117101
/*
118-
* This class is responsible for generating a hash for a request. This hash
119-
* is used to look up a cached response.
102+
* This class is responsible for generating a hash for
103+
* a request. Used for looking up cached responses.
120104
*/
121105
'hasher' => \Spatie\ResponseCache\Hasher\DefaultHasher::class,
122106

@@ -126,8 +110,9 @@
126110
'serializer' => \Spatie\ResponseCache\Serializers\JsonSerializer::class,
127111

128112
/*
129-
* Here you may define replacers that dynamically replace content from the response.
130-
* Each replacer must implement the Replacer interface.
113+
* Here you may define the replacers that will replace
114+
* dynamic content from the response. Each replacer
115+
* must always implement the Replacer interface.
131116
*/
132117
'replacers' => [
133118
\Spatie\ResponseCache\Replacers\CsrfTokenReplacer::class,

docs/advanced-usage/configuration.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,22 @@ Set this to an empty array if you want all query parameters to be included in th
5353

5454
## Debug headers
5555

56-
When `APP_DEBUG` is `true`, the package adds a header to cached responses showing when the response was cached. You can customize this behavior.
56+
When `APP_DEBUG` is `true`, the package adds debug headers to cached responses. You can customize this behavior.
5757

5858
```php
5959
// config/responsecache.php
6060

6161
'debug' => [
62-
'add_time_header' => env('APP_DEBUG', false),
63-
'time_header_name' => 'laravel-responsecache',
64-
'add_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false),
65-
'age_header_name' => 'laravel-responsecache-age',
62+
'enabled' => env('APP_DEBUG', false),
63+
'cache_time_header_name' => 'X-Cache-Time',
64+
'cache_status_header_name' => 'X-Cache-Status',
65+
'cache_age_header_name' => 'X-Cache-Age',
66+
'cache_key_header_name' => 'X-Cache-Key',
6667
],
6768
```
6869

69-
When `add_age_header` is enabled (requires `add_time_header` to also be active), cached responses will include a header showing how many seconds ago the response was cached.
70+
When enabled, cached responses will include the following headers:
71+
- `X-Cache-Status`: `HIT` or `MISS` indicating whether the response was served from cache
72+
- `X-Cache-Time`: the timestamp when the response was cached
73+
- `X-Cache-Age`: how many seconds ago the response was cached (only on cache hits)
74+
- `X-Cache-Key`: the cache key used (only when `app.debug` is `true`)

docs/advanced-usage/custom-cache-profiles.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ You can create your own cache profile by implementing the `CacheProfile` interfa
1010
```php
1111
namespace App\CacheProfiles;
1212

13-
use DateTime;
1413
use Illuminate\Http\Request;
1514
use Spatie\ResponseCache\CacheProfiles\CacheProfile;
1615
use Symfony\Component\HttpFoundation\Response;
@@ -33,9 +32,9 @@ class CustomCacheProfile implements CacheProfile
3332
return $response->isSuccessful();
3433
}
3534

36-
public function cacheRequestUntil(Request $request): DateTime
35+
public function cacheLifetimeInSeconds(Request $request): int
3736
{
38-
return now()->addMinutes(30);
37+
return 30 * 60;
3938
}
4039

4140
public function useCacheNameSuffix(Request $request): string

docs/basic-usage/caching-responses.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ Route::get('/posts', [PostController::class, 'index'])
2626
Route::get('/posts/{post}', [PostController::class, 'show'])
2727
->middleware(CacheResponse::for(hours(1), tags: ['posts']));
2828

29-
// Cache with a specific driver
30-
Route::get('/api/posts', [ApiPostController::class, 'index'])
31-
->middleware(CacheResponse::for(minutes(5), tags: ['api', 'posts'], driver: 'redis'));
3229
```
3330

3431
The `lifetime` parameter accepts Laravel's `minutes()`, `hours()`, and `days()` helpers (or any `CarbonInterval`), or an `int` in seconds.
@@ -48,7 +45,7 @@ class PostController
4845
return view('posts.index', ['posts' => Post::all()]);
4946
}
5047

51-
#[Cache(lifetime: 10 * 60, driver: 'redis')]
48+
#[Cache(lifetime: 10 * 60)]
5249
public function show(Post $post)
5350
{
5451
return view('posts.show', ['post' => $post]);
@@ -59,7 +56,6 @@ class PostController
5956
The `#[Cache]` attribute accepts the following parameters.
6057
- `lifetime`: Cache duration in seconds (defaults to the config value)
6158
- `tags`: Cache tags (array)
62-
- `driver`: Cache driver to use (defaults to the config value)
6359

6460
Attributes can also be applied at the class level to cache all methods in the controller.
6561

docs/installation-setup.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ return [
5555
],
5656

5757
'bypass' => [
58-
'enabled' => env('CACHE_BYPASS_HEADER_NAME') !== null,
5958
'header_name' => env('CACHE_BYPASS_HEADER_NAME'),
6059
'header_value' => env('CACHE_BYPASS_HEADER_VALUE'),
6160
],
6261

6362
'debug' => [
64-
'add_time_header' => env('APP_DEBUG', false),
65-
'time_header_name' => env('RESPONSE_CACHE_HEADER_NAME', 'laravel-responsecache'),
66-
'add_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false),
67-
'age_header_name' => env('RESPONSE_CACHE_AGE_HEADER_NAME', 'laravel-responsecache-age'),
63+
'enabled' => env('APP_DEBUG', false),
64+
'cache_time_header_name' => 'X-Cache-Time',
65+
'cache_status_header_name' => 'X-Cache-Status',
66+
'cache_age_header_name' => 'X-Cache-Age',
67+
'cache_key_header_name' => 'X-Cache-Key',
6868
],
6969

7070
'ignored_query_parameters' => [

freek-blog-full.png

798 KB
Loading

src/Attributes/Cache.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ class Cache
1010
public function __construct(
1111
public ?int $lifetime = null,
1212
public array $tags = [],
13-
public ?string $driver = null,
1413
) {}
1514
}

src/Attributes/FlexibleCache.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
88
class FlexibleCache
99
{
10-
public int $fresh;
11-
12-
public int $stale;
13-
1410
public function __construct(
1511
public int $lifetime,
1612
public int $grace,
1713
public array $tags = [],
18-
) {
19-
$this->fresh = $lifetime;
20-
$this->stale = $grace;
21-
}
14+
) {}
2215
}

src/CacheItemSelector/AbstractRequestBuilder.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ abstract class AbstractRequestBuilder
1717

1818
protected ?string $cacheNameSuffix = null;
1919

20-
public function withPutMethod(): self
20+
public function withPutMethod(): static
2121
{
2222
$this->method = 'PUT';
2323

2424
return $this;
2525
}
2626

27-
public function withPatchMethod(): self
27+
public function withPatchMethod(): static
2828
{
2929
$this->method = 'PATCH';
3030

3131
return $this;
3232
}
3333

34-
public function withPostMethod(): self
34+
public function withPostMethod(): static
3535
{
3636
$this->method = 'POST';
3737

@@ -42,21 +42,21 @@ public function withPostMethod(): self
4242
* if method is GET then will be converted to query
4343
* otherwise it will became part of request input
4444
*/
45-
public function withParameters(array $parameters): self
45+
public function withParameters(array $parameters): static
4646
{
4747
$this->parameters = $parameters;
4848

4949
return $this;
5050
}
5151

52-
public function withCookies(array $cookies): self
52+
public function withCookies(array $cookies): static
5353
{
5454
$this->cookies = $cookies;
5555

5656
return $this;
5757
}
5858

59-
public function withHeaders(array $headers): self
59+
public function withHeaders(array $headers): static
6060
{
6161
$this->server = collect($this->server)
6262
->filter(fn (string $val, string $key) => ! str_starts_with($key, 'HTTP_'))
@@ -69,14 +69,14 @@ public function withHeaders(array $headers): self
6969
return $this;
7070
}
7171

72-
public function withRemoteAddress($remoteAddress): self
72+
public function withRemoteAddress(string $remoteAddress): static
7373
{
7474
$this->server['REMOTE_ADDR'] = $remoteAddress;
7575

7676
return $this;
7777
}
7878

79-
public function usingSuffix($cacheNameSuffix): self
79+
public function usingSuffix(string $cacheNameSuffix): static
8080
{
8181
$this->cacheNameSuffix = $cacheNameSuffix;
8282

src/CacheItemSelector/CacheItemSelector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function forget(): void
4141

4242
return $this->hasher->getHashFor($request);
4343
})
44-
->filter(fn ($hash) => $this->taggedCache($this->tags)->has($hash))
4544
->each(fn ($hash) => $this->taggedCache($this->tags)->forget($hash));
4645
}
4746
}

0 commit comments

Comments
 (0)