|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Cache\Repository; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
|
|
8
|
|
|
class ResponseCacheRepository |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var \Illuminate\Cache\Repository */ |
|
11
|
|
|
protected $cache; |
|
12
|
|
|
|
|
13
|
|
|
/** @var \Spatie\ResponseCache\ResponseSerializer */ |
|
14
|
|
|
protected $responseSerializer; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ResponseSerializer $responseSerializer, Repository $cache) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->cache = $cache; |
|
19
|
|
|
$this->responseSerializer = $responseSerializer; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $key |
|
24
|
|
|
* @param \Symfony\Component\HttpFoundation\Response $response |
|
25
|
|
|
* @param \DateTime|int $seconds |
|
26
|
|
|
*/ |
|
27
|
|
|
public function put(string $key, $response, $seconds): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->cache->put($key, $this->responseSerializer->serialize($response), $seconds); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function putKey(string $key, $value, $seconds): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->cache->put($key, $value, $seconds); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function has(string $key): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->cache->has($key); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function get(string $key): Response |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->responseSerializer->unserialize($this->cache->get($key)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getKey(string $key): string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->cache->get($key, ''); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @deprecated Use the new clear method, this is just an alias. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function flush() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->clear(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function clear() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->cache->flush(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function forget(string $key): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->cache->forget($key); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|