1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Spatie\ResponseCache\Exceptions\InvalidReplacer; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Spatie\ResponseCache\CacheProfiles\CacheProfile; |
9
|
|
|
|
10
|
|
|
class ResponseCache |
11
|
|
|
{ |
12
|
|
|
/** @var \Spatie\ResponseCache\ResponseCache */ |
13
|
|
|
protected $cache; |
14
|
|
|
|
15
|
|
|
/** @var \Spatie\ResponseCache\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(Request $request, Response $response, $lifetimeInSeconds = null): Response |
47
|
|
|
{ |
48
|
|
|
if (config('responsecache.add_cache_time_header')) { |
49
|
|
|
$response = $this->addCachedHeader($response); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->cache->put( |
|
|
|
|
53
|
|
|
$this->hasher->getHashFor($request), |
54
|
|
|
$response, |
55
|
|
|
($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
foreach ($this->cacheProfile->replacers($request) as $key => $callback) { |
59
|
|
|
$replacer = new Replacer($key, $callback); |
60
|
|
|
|
61
|
|
|
$this->cache->putKey( |
|
|
|
|
62
|
|
|
$this->hasher->getHashFor($request).$replacer->getKey(), |
63
|
|
|
$replacer->getValue(), |
64
|
|
|
($lifetimeInSeconds) ? intval($lifetimeInSeconds) : $this->cacheProfile->cacheRequestUntil($request) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function hasBeenCached(Request $request): bool |
72
|
|
|
{ |
73
|
|
|
return config('responsecache.enabled') |
74
|
|
|
? $this->cache->has($this->hasher->getHashFor($request)) |
|
|
|
|
75
|
|
|
: false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getCachedResponseFor(Request $request): Response |
79
|
|
|
{ |
80
|
|
|
return $this->cache->get($this->hasher->getHashFor($request)); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getCachedKeyFor(Request $request, string $key): string |
84
|
|
|
{ |
85
|
|
|
return $this->cache->getKey($this->hasher->getHashFor($request).$key); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @deprecated Use the new clear method, this is just an alias. |
90
|
|
|
*/ |
91
|
|
|
public function flush() |
92
|
|
|
{ |
93
|
|
|
$this->clear(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function clear() |
97
|
|
|
{ |
98
|
|
|
$this->cache->clear(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function addCachedHeader(Response $response): Response |
102
|
|
|
{ |
103
|
|
|
$clonedResponse = clone $response; |
104
|
|
|
|
105
|
|
|
$clonedResponse->headers->set('laravel-responsecache', 'cached on '.date('Y-m-d H:i:s')); |
106
|
|
|
|
107
|
|
|
return $clonedResponse; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string|array $uris |
112
|
|
|
* |
113
|
|
|
* @return \Spatie\ResponseCache\ResponseCache |
114
|
|
|
*/ |
115
|
|
|
public function forget($uris): self |
116
|
|
|
{ |
117
|
|
|
$uris = is_array($uris) ? $uris : func_get_args(); |
118
|
|
|
|
119
|
|
|
collect($uris)->each(function ($uri) { |
120
|
|
|
$request = Request::create($uri); |
121
|
|
|
$hash = $this->hasher->getHashFor($request); |
122
|
|
|
|
123
|
|
|
if ($this->cache->has($hash)) { |
|
|
|
|
124
|
|
|
$this->cache->forget($hash); |
125
|
|
|
} |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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..