1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Spatie\ResponseCache\CacheProfiles\CacheProfile; |
8
|
|
|
|
9
|
|
|
class ResponseCache |
10
|
|
|
{ |
11
|
|
|
/** @var \Spatie\ResponseCache\ResponseCache */ |
12
|
|
|
protected $cache; |
13
|
|
|
|
14
|
|
|
/** @var \Spatie\ResponseCache\RequestHasher */ |
15
|
|
|
protected $hasher; |
16
|
|
|
|
17
|
|
|
/** @var \Spatie\ResponseCache\CacheProfiles\CacheProfile */ |
18
|
|
|
protected $cacheProfile; |
19
|
|
|
|
20
|
|
|
public function __construct(ResponseCacheRepository $cache, RequestHasher $hasher, CacheProfile $cacheProfile) |
21
|
|
|
{ |
22
|
|
|
$this->cache = $cache; |
|
|
|
|
23
|
|
|
$this->hasher = $hasher; |
24
|
|
|
$this->cacheProfile = $cacheProfile; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function enabled(Request $request): bool |
28
|
|
|
{ |
29
|
|
|
return $this->cacheProfile->enabled($request); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function shouldCache(Request $request, Response $response): bool |
33
|
|
|
{ |
34
|
|
|
if ($request->attributes->has('responsecache.doNotCache')) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (! $this->cacheProfile->shouldCacheRequest($request)) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->cacheProfile->shouldCacheResponse($response); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function cacheResponse(Request $request, Response $response, $lifetimeInSeconds = null): Response |
46
|
|
|
{ |
47
|
|
|
if (config('responsecache.add_cache_time_header')) { |
48
|
|
|
$response = $this->addCachedHeader($response); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->cache->put( |
|
|
|
|
52
|
|
|
$this->hasher->getHashFor($request), |
53
|
|
|
$response, |
54
|
|
|
($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
foreach ($this->cacheProfile->replacers($request) as $key => $callback) { |
58
|
|
|
$replacer = new Replacer($key, $callback); |
59
|
|
|
|
60
|
|
|
$this->cache->putKey( |
|
|
|
|
61
|
|
|
$this->hasher->getHashFor($request).$replacer->getKey(), |
62
|
|
|
$replacer->getValue(), |
63
|
|
|
($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $response; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function hasBeenCached(Request $request): bool |
71
|
|
|
{ |
72
|
|
|
return config('responsecache.enabled') |
73
|
|
|
? $this->cache->has($this->hasher->getHashFor($request)) |
|
|
|
|
74
|
|
|
: false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getCachedResponseFor(Request $request): Response |
78
|
|
|
{ |
79
|
|
|
return $this->cache->get($this->hasher->getHashFor($request)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getCachedKeyFor(Request $request, string $key): string |
83
|
|
|
{ |
84
|
|
|
return $this->cache->getKey($this->hasher->getHashFor($request).$key); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @deprecated Use the new clear method, this is just an alias. |
89
|
|
|
*/ |
90
|
|
|
public function flush() |
91
|
|
|
{ |
92
|
|
|
$this->clear(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function clear() |
96
|
|
|
{ |
97
|
|
|
$this->cache->clear(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function addCachedHeader(Response $response): Response |
101
|
|
|
{ |
102
|
|
|
$clonedResponse = clone $response; |
103
|
|
|
|
104
|
|
|
$clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s')); |
105
|
|
|
|
106
|
|
|
return $clonedResponse; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string|array $uris |
111
|
|
|
* |
112
|
|
|
* @return \Spatie\ResponseCache\ResponseCache |
113
|
|
|
*/ |
114
|
|
|
public function forget($uris): self |
115
|
|
|
{ |
116
|
|
|
$uris = is_array($uris) ? $uris : func_get_args(); |
117
|
|
|
|
118
|
|
|
collect($uris)->each(function ($uri) { |
119
|
|
|
$request = Request::create($uri); |
120
|
|
|
$hash = $this->hasher->getHashFor($request); |
121
|
|
|
|
122
|
|
|
if ($this->cache->has($hash)) { |
|
|
|
|
123
|
|
|
$this->cache->forget($hash); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..