|
0
|
1 |
<?php |
|
|
2 |
/* |
|
|
3 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
4 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
5 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
6 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
7 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
8 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
9 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
10 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
11 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
12 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
13 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
14 |
* |
|
|
15 |
* This software consists of voluntary contributions made by many individuals |
|
|
16 |
* and is licensed under the LGPL. For more information, see |
|
|
17 |
* <http://www.doctrine-project.org>. |
|
|
18 |
*/ |
|
|
19 |
|
|
|
20 |
namespace Doctrine\Common\Cache; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* Base class for cache driver implementations. |
|
|
24 |
* |
|
|
25 |
* @since 2.0 |
|
|
26 |
* @author Benjamin Eberlei <kontakt@beberlei.de> |
|
|
27 |
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
|
|
28 |
* @author Jonathan Wage <jonwage@gmail.com> |
|
|
29 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
30 |
*/ |
|
|
31 |
abstract class AbstractCache implements Cache |
|
|
32 |
{ |
|
|
33 |
/** @var string The namespace to prefix all cache ids with */ |
|
|
34 |
private $_namespace = ''; |
|
|
35 |
|
|
|
36 |
/** |
|
|
37 |
* Set the namespace to prefix all cache ids with. |
|
|
38 |
* |
|
|
39 |
* @param string $namespace |
|
|
40 |
* @return void |
|
|
41 |
*/ |
|
|
42 |
public function setNamespace($namespace) |
|
|
43 |
{ |
|
|
44 |
$this->_namespace = (string) $namespace; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
/** |
|
|
48 |
* {@inheritdoc} |
|
|
49 |
*/ |
|
|
50 |
public function fetch($id) |
|
|
51 |
{ |
|
|
52 |
return $this->_doFetch($this->_getNamespacedId($id)); |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* {@inheritdoc} |
|
|
57 |
*/ |
|
|
58 |
public function contains($id) |
|
|
59 |
{ |
|
|
60 |
return $this->_doContains($this->_getNamespacedId($id)); |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
/** |
|
|
64 |
* {@inheritdoc} |
|
|
65 |
*/ |
|
|
66 |
public function save($id, $data, $lifeTime = 0) |
|
|
67 |
{ |
|
|
68 |
return $this->_doSave($this->_getNamespacedId($id), $data, $lifeTime); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
/** |
|
|
72 |
* {@inheritdoc} |
|
|
73 |
*/ |
|
|
74 |
public function delete($id) |
|
|
75 |
{ |
|
|
76 |
$id = $this->_getNamespacedId($id); |
|
|
77 |
|
|
|
78 |
if (strpos($id, '*') !== false) { |
|
|
79 |
return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/'); |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
return $this->_doDelete($id); |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
/** |
|
|
86 |
* Delete all cache entries. |
|
|
87 |
* |
|
|
88 |
* @return array $deleted Array of the deleted cache ids |
|
|
89 |
*/ |
|
|
90 |
public function deleteAll() |
|
|
91 |
{ |
|
|
92 |
$ids = $this->getIds(); |
|
|
93 |
|
|
|
94 |
foreach ($ids as $id) { |
|
|
95 |
$this->delete($id); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
return $ids; |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
/** |
|
|
102 |
* Delete cache entries where the id matches a PHP regular expressions |
|
|
103 |
* |
|
|
104 |
* @param string $regex |
|
|
105 |
* @return array $deleted Array of the deleted cache ids |
|
|
106 |
*/ |
|
|
107 |
public function deleteByRegex($regex) |
|
|
108 |
{ |
|
|
109 |
$deleted = array(); |
|
|
110 |
|
|
|
111 |
$ids = $this->getIds(); |
|
|
112 |
|
|
|
113 |
foreach ($ids as $id) { |
|
|
114 |
if (preg_match($regex, $id)) { |
|
|
115 |
$this->delete($id); |
|
|
116 |
$deleted[] = $id; |
|
|
117 |
} |
|
|
118 |
} |
|
|
119 |
|
|
|
120 |
return $deleted; |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
/** |
|
|
124 |
* Delete cache entries where the id has the passed prefix |
|
|
125 |
* |
|
|
126 |
* @param string $prefix |
|
|
127 |
* @return array $deleted Array of the deleted cache ids |
|
|
128 |
*/ |
|
|
129 |
public function deleteByPrefix($prefix) |
|
|
130 |
{ |
|
|
131 |
$deleted = array(); |
|
|
132 |
|
|
|
133 |
$prefix = $this->_getNamespacedId($prefix); |
|
|
134 |
$ids = $this->getIds(); |
|
|
135 |
|
|
|
136 |
foreach ($ids as $id) { |
|
|
137 |
if (strpos($id, $prefix) === 0) { |
|
|
138 |
$this->delete($id); |
|
|
139 |
$deleted[] = $id; |
|
|
140 |
} |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
return $deleted; |
|
|
144 |
} |
|
|
145 |
|
|
|
146 |
/** |
|
|
147 |
* Delete cache entries where the id has the passed suffix |
|
|
148 |
* |
|
|
149 |
* @param string $suffix |
|
|
150 |
* @return array $deleted Array of the deleted cache ids |
|
|
151 |
*/ |
|
|
152 |
public function deleteBySuffix($suffix) |
|
|
153 |
{ |
|
|
154 |
$deleted = array(); |
|
|
155 |
|
|
|
156 |
$ids = $this->getIds(); |
|
|
157 |
|
|
|
158 |
foreach ($ids as $id) { |
|
|
159 |
if (substr($id, -1 * strlen($suffix)) === $suffix) { |
|
|
160 |
$this->delete($id); |
|
|
161 |
$deleted[] = $id; |
|
|
162 |
} |
|
|
163 |
} |
|
|
164 |
|
|
|
165 |
return $deleted; |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
/** |
|
|
169 |
* Prefix the passed id with the configured namespace value |
|
|
170 |
* |
|
|
171 |
* @param string $id The id to namespace |
|
|
172 |
* @return string $id The namespaced id |
|
|
173 |
*/ |
|
|
174 |
private function _getNamespacedId($id) |
|
|
175 |
{ |
|
|
176 |
return $this->_namespace . $id; |
|
|
177 |
} |
|
|
178 |
|
|
|
179 |
/** |
|
|
180 |
* Fetches an entry from the cache. |
|
|
181 |
* |
|
|
182 |
* @param string $id cache id The id of the cache entry to fetch. |
|
|
183 |
* @return string The cached data or FALSE, if no cache entry exists for the given id. |
|
|
184 |
*/ |
|
|
185 |
abstract protected function _doFetch($id); |
|
|
186 |
|
|
|
187 |
/** |
|
|
188 |
* Test if an entry exists in the cache. |
|
|
189 |
* |
|
|
190 |
* @param string $id cache id The cache id of the entry to check for. |
|
|
191 |
* @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise. |
|
|
192 |
*/ |
|
|
193 |
abstract protected function _doContains($id); |
|
|
194 |
|
|
|
195 |
/** |
|
|
196 |
* Puts data into the cache. |
|
|
197 |
* |
|
|
198 |
* @param string $id The cache id. |
|
|
199 |
* @param string $data The cache entry/data. |
|
|
200 |
* @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime). |
|
|
201 |
* @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise. |
|
|
202 |
*/ |
|
|
203 |
abstract protected function _doSave($id, $data, $lifeTime = false); |
|
|
204 |
|
|
|
205 |
/** |
|
|
206 |
* Deletes a cache entry. |
|
|
207 |
* |
|
|
208 |
* @param string $id cache id |
|
|
209 |
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. |
|
|
210 |
*/ |
|
|
211 |
abstract protected function _doDelete($id); |
|
|
212 |
|
|
|
213 |
/** |
|
|
214 |
* Get an array of all the cache ids stored |
|
|
215 |
* |
|
|
216 |
* @return array $ids |
|
|
217 |
*/ |
|
|
218 |
abstract public function getIds(); |
|
|
219 |
} |