1 | <?php |
||
14 | class Cache implements Adapter, |
||
|
|||
15 | MetadataSupporter |
||
16 | { |
||
17 | /** |
||
18 | * @var Adapter |
||
19 | */ |
||
20 | protected $source; |
||
21 | |||
22 | /** |
||
23 | * @var Adapter |
||
24 | */ |
||
25 | protected $cache; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $ttl; |
||
31 | |||
32 | /** |
||
33 | * @var Adapter |
||
34 | */ |
||
35 | protected $serializeCache; |
||
36 | |||
37 | /** |
||
38 | * @param Adapter $source The source adapter that must be cached |
||
39 | * @param Adapter $cache The adapter used to cache the source |
||
40 | * @param int $ttl Time to live of a cached file |
||
41 | * @param Adapter $serializeCache The adapter used to cache serializations |
||
42 | */ |
||
43 | public function __construct(Adapter $source, Adapter $cache, $ttl = 0, Adapter $serializeCache = null) |
||
44 | { |
||
45 | $this->source = $source; |
||
46 | $this->cache = $cache; |
||
47 | $this->ttl = $ttl; |
||
48 | |||
49 | if (!$serializeCache) { |
||
50 | $serializeCache = new InMemoryAdapter(); |
||
51 | } |
||
52 | $this->serializeCache = $serializeCache; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Returns the time to live of the cache. |
||
57 | * |
||
58 | * @return int $ttl |
||
59 | */ |
||
60 | public function getTtl() |
||
61 | { |
||
62 | return $this->ttl; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Defines the time to live of the cache. |
||
67 | * |
||
68 | * @param int $ttl |
||
69 | */ |
||
70 | public function setTtl($ttl) |
||
71 | { |
||
72 | $this->ttl = $ttl; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function read($key) |
||
79 | { |
||
80 | if ($this->needsReload($key)) { |
||
81 | $contents = $this->source->read($key); |
||
82 | $this->cache->write($key, $contents); |
||
83 | } else { |
||
84 | $contents = $this->cache->read($key); |
||
85 | } |
||
86 | |||
87 | return $contents; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function rename($key, $new) |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function write($key, $content, array $metadata = null) |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function exists($key) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function mtime($key) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function keys() |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function delete($key) |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function isDirectory($key) |
||
170 | |||
171 | /** |
||
172 | * {@inheritDoc} |
||
173 | */ |
||
174 | public function listDirectory($key) |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function setMetadata($key, $metadata) |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function getMetadata($key) |
||
204 | |||
205 | /** |
||
206 | * Indicates whether the cache for the specified key needs to be reloaded. |
||
207 | * |
||
208 | * @param string $key |
||
209 | */ |
||
210 | public function needsReload($key) |
||
229 | |||
230 | /** |
||
231 | * Indicates whether the serialized cache file needs to be rebuild. |
||
232 | * |
||
233 | * @param string $cacheFile |
||
234 | */ |
||
235 | public function needsRebuild($cacheFile) |
||
248 | } |
||
249 |