|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Carbon; |
|
7
|
|
|
use Spatie\ResponseCache\CacheProfiles\CacheProfile; |
|
8
|
|
|
use Spatie\ResponseCache\Hasher\RequestHasher; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
|
|
11
|
|
|
class ResponseCache |
|
12
|
|
|
{ |
|
13
|
|
|
protected ResponseCacheRepository $cache; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
protected RequestHasher $hasher; |
|
16
|
|
|
|
|
17
|
|
|
protected CacheProfile $cacheProfile; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(ResponseCacheRepository $cache, RequestHasher $hasher, CacheProfile $cacheProfile) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->cache = $cache; |
|
22
|
|
|
$this->hasher = $hasher; |
|
23
|
|
|
$this->cacheProfile = $cacheProfile; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function enabled(Request $request): bool |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->cacheProfile->enabled($request); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function shouldCache(Request $request, Response $response): bool |
|
32
|
|
|
{ |
|
33
|
|
|
if ($request->attributes->has('responsecache.doNotCache')) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (! $this->cacheProfile->shouldCacheRequest($request)) { |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->cacheProfile->shouldCacheResponse($response); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function cacheResponse( |
|
45
|
|
|
Request $request, |
|
46
|
|
|
Response $response, |
|
47
|
|
|
?int $lifetimeInSeconds = null, |
|
48
|
|
|
array $tags = [] |
|
49
|
|
|
): Response { |
|
50
|
|
|
if (config('responsecache.add_cache_time_header')) { |
|
51
|
|
|
$response = $this->addCachedHeader($response); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->taggedCache($tags)->put( |
|
55
|
|
|
$this->hasher->getHashFor($request), |
|
56
|
|
|
$response, |
|
57
|
|
|
$lifetimeInSeconds ?? $this->cacheProfile->cacheRequestUntil($request), |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
return $response; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function hasBeenCached(Request $request, array $tags = []): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return config('responsecache.enabled') |
|
66
|
|
|
? $this->taggedCache($tags)->has($this->hasher->getHashFor($request)) |
|
67
|
|
|
: false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getCachedResponseFor(Request $request, array $tags = []): Response |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->taggedCache($tags)->get($this->hasher->getHashFor($request)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function clear(array $tags = []) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->taggedCache($tags)->clear(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function addCachedHeader(Response $response): Response |
|
81
|
|
|
{ |
|
82
|
|
|
$clonedResponse = clone $response; |
|
83
|
|
|
|
|
84
|
|
|
$clonedResponse->headers->set( |
|
85
|
|
|
config('responsecache.cache_time_header_name'), |
|
86
|
|
|
Carbon::now()->toRfc2822String(), |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
return $clonedResponse; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string|array $uris |
|
94
|
|
|
* @param string[] $tags |
|
95
|
|
|
* |
|
96
|
|
|
* @return \Spatie\ResponseCache\ResponseCache |
|
97
|
|
|
*/ |
|
98
|
|
|
public function forget($uris, array $tags = []): self |
|
99
|
|
|
{ |
|
100
|
|
|
$uris = is_array($uris) ? $uris : func_get_args(); |
|
101
|
|
|
|
|
102
|
|
|
collect($uris)->each(function ($uri) use ($tags) { |
|
103
|
|
|
$request = Request::create(url($uri)); |
|
104
|
|
|
$hash = $this->hasher->getHashFor($request); |
|
105
|
|
|
|
|
106
|
|
|
if ($this->taggedCache($tags)->has($hash)) { |
|
107
|
|
|
$this->taggedCache($tags)->forget($hash); |
|
108
|
|
|
} |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function taggedCache(array $tags = []): ResponseCacheRepository |
|
115
|
|
|
{ |
|
116
|
|
|
if (empty($tags)) { |
|
117
|
|
|
return $this->cache; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->cache->tags($tags); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|