-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepository.php
172 lines (154 loc) · 4.17 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* This file is part of response-cache, a Matchory library.
*
* @author Moritz Friedrich <[email protected]>
*/
declare(strict_types=1);
namespace Matchory\ResponseCache;
use BadMethodCallException;
use Illuminate\Cache\TaggableStore;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Matchory\ResponseCache\Events\Flush;
use Psr\SimpleCache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;
/**
* Cache Repository
*
* Responsible for cache persistence.
*
* @bundle Matchory\ResponseCache
*/
readonly class Repository
{
public function __construct(
protected CacheRepository $store,
protected Dispatcher $eventDispatcher
) {
}
/**
* Retrieves a response from the cache.
*
* @param string $key
* @param string[]|null $tags
*
* @return Response|null
* @throws BadMethodCallException
* @throws InvalidArgumentException
*/
public function get(string $key, array|null $tags = null): Response|null
{
$response = $this->getStore($tags)->get($key);
if ( ! $response) {
return null;
}
return $this->hydrate($response);
}
/**
* Deletes a cached response.
*
* @param string $key
* @param string[]|null $tags
*
* @throws BadMethodCallException
*/
public function delete(string $key, array|null $tags = null): void
{
$this->getStore($tags)->forget($key);
}
/**
* Flushes the response cache.
*
* @param string[]|null $tags
*
* @throws BadMethodCallException
*/
public function flush(array|null $tags = null): void
{
$this->getStore($tags)->clear();
$this->eventDispatcher->dispatch(new Flush($tags));
}
/**
* Checks whether a response is cached.
*
* @param string $key
* @param string[]|null $tags
*
* @return bool
* @throws BadMethodCallException
* @throws InvalidArgumentException
*/
public function has(string $key, array|null $tags = null): bool
{
return $this->getStore($tags)->has($key);
}
/**
* Puts a response into the cache.
*
* @param string $key
* @param Response $response
* @param string[]|null $tags
* @param int|null $ttl
*
* @throws BadMethodCallException
*/
public function put(
string $key,
Response $response,
array|null $tags = null,
int|null $ttl = null
): void {
$serialized = $this->serialize($response);
$this->getStore($tags)->put($key, $serialized, $ttl);
}
/**
* Retrieves the store instance. If it supports tags, a tagged instance for
* the given tags will be returned, the untagged store otherwise.
*
* @param string[]|null $tags
*
* @return CacheRepository
* @throws BadMethodCallException
*/
protected function getStore(array|null $tags = null): CacheRepository
{
return $this->supportsTags()
? $this->store->tags($tags)
: $this->store;
}
/**
* Hydrates a serialized response. This method may be overridden by children
* implementations to add custom hydration mechanisms.
*
* @param mixed $cached Cached representation of the response.
*
* @return Response Hydrated response instance.
*/
protected function hydrate(mixed $cached): Response
{
return $cached;
}
/**
* Serializes the response. This method may be overridden by children
* implementations to add custom serialization mechanisms.
*
* @param Response $response Response instance.
*
* @return mixed Serialized response representation.
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
protected function serialize(Response $response): mixed
{
return $response;
}
/**
* Checks whether the store supports tags.
*
* @return bool
*/
protected function supportsTags(): bool
{
return $this->store->getStore() instanceof TaggableStore;
}
}