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