| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Driver.php 7490 2010-03-29 19:53:27Z jwage $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Abstract cache driver class
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Cache
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7490 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
33 |
*/
|
|
|
34 |
abstract class Doctrine_Cache_Driver implements Doctrine_Cache_Interface
|
|
|
35 |
{
|
|
|
36 |
/**
|
|
|
37 |
* @var array $_options an array of options
|
|
|
38 |
*/
|
|
|
39 |
protected $_options = array();
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Configure cache driver with an array of options
|
|
|
43 |
*
|
|
|
44 |
* @param array $_options an array of options
|
|
|
45 |
*/
|
|
|
46 |
public function __construct($options = array())
|
|
|
47 |
{
|
|
|
48 |
$this->_options = $options;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Set option name and value
|
|
|
53 |
*
|
|
|
54 |
* @param mixed $option the option name
|
|
|
55 |
* @param mixed $value option value
|
|
|
56 |
* @return boolean TRUE on success, FALSE on failure
|
|
|
57 |
*/
|
|
|
58 |
public function setOption($option, $value)
|
|
|
59 |
{
|
|
|
60 |
if (isset($this->_options[$option])) {
|
|
|
61 |
$this->_options[$option] = $value;
|
|
|
62 |
return true;
|
|
|
63 |
}
|
|
|
64 |
return false;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Get value of option
|
|
|
69 |
*
|
|
|
70 |
* @param mixed $option the option name
|
|
|
71 |
* @return mixed option value
|
|
|
72 |
*/
|
|
|
73 |
public function getOption($option)
|
|
|
74 |
{
|
|
|
75 |
if ( ! isset($this->_options[$option])) {
|
|
|
76 |
return null;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $this->_options[$option];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Fetch a cache record from this cache driver instance
|
|
|
84 |
*
|
|
|
85 |
* @param string $id cache id
|
|
|
86 |
* @param boolean $testCacheValidity if set to false, the cache validity won't be tested
|
|
|
87 |
* @return mixed Returns either the cached data or false
|
|
|
88 |
*/
|
|
|
89 |
public function fetch($id, $testCacheValidity = true)
|
|
|
90 |
{
|
|
|
91 |
$key = $this->_getKey($id);
|
|
|
92 |
return $this->_doFetch($key, $testCacheValidity);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Test if a cache record exists for the passed id
|
|
|
97 |
*
|
|
|
98 |
* @param string $id cache id
|
|
|
99 |
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
|
|
100 |
*/
|
|
|
101 |
public function contains($id)
|
|
|
102 |
{
|
|
|
103 |
$key = $this->_getKey($id);
|
|
|
104 |
return $this->_doContains($key);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Save some string datas into a cache record
|
|
|
109 |
*
|
|
|
110 |
* @param string $id cache id
|
|
|
111 |
* @param string $data data to cache
|
|
|
112 |
* @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
|
|
|
113 |
* @return boolean true if no problem
|
|
|
114 |
*/
|
|
|
115 |
public function save($id, $data, $lifeTime = false)
|
|
|
116 |
{
|
|
|
117 |
$key = $this->_getKey($id);
|
|
|
118 |
return $this->_doSave($key, $data, $lifeTime);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Remove a cache record
|
|
|
123 |
*
|
|
|
124 |
* Note: This method accepts wildcards with the * character
|
|
|
125 |
*
|
|
|
126 |
* @param string $id cache id
|
|
|
127 |
* @return boolean true if no problem
|
|
|
128 |
*/
|
|
|
129 |
public function delete($id)
|
|
|
130 |
{
|
|
|
131 |
$key = $this->_getKey($id);
|
|
|
132 |
|
|
|
133 |
if (strpos($key, '*') !== false) {
|
|
|
134 |
return $this->deleteByRegex('/' . str_replace('*', '.*', $key) . '/');
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
return $this->_doDelete($key);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Delete cache entries where the key matches a PHP regular expressions
|
|
|
142 |
*
|
|
|
143 |
* @param string $regex
|
|
|
144 |
* @return integer $count The number of deleted cache entries
|
|
|
145 |
*/
|
|
|
146 |
public function deleteByRegex($regex)
|
|
|
147 |
{
|
|
|
148 |
$count = 0;
|
|
|
149 |
$keys = $this->_getCacheKeys();
|
|
|
150 |
if (is_array($keys)) {
|
|
|
151 |
foreach ($keys as $key) {
|
|
|
152 |
if (preg_match($regex, $key)) {
|
|
|
153 |
$count++;
|
|
|
154 |
$this->delete($key);
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
return $count;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Delete cache entries where the key has the passed prefix
|
|
|
163 |
*
|
|
|
164 |
* @param string $prefix
|
|
|
165 |
* @return integer $count The number of deleted cache entries
|
|
|
166 |
*/
|
|
|
167 |
public function deleteByPrefix($prefix)
|
|
|
168 |
{
|
|
|
169 |
$count = 0;
|
|
|
170 |
$keys = $this->_getCacheKeys();
|
|
|
171 |
if (is_array($keys)) {
|
|
|
172 |
foreach ($keys as $key) {
|
|
|
173 |
if (strpos($key, $prefix) === 0) {
|
|
|
174 |
$count++;
|
|
|
175 |
$this->delete($key);
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
return $count;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Delete cache entries where the key has the passed suffix
|
|
|
184 |
*
|
|
|
185 |
* @param string $suffix
|
|
|
186 |
* @return integer $count The number of deleted cache entries
|
|
|
187 |
*/
|
|
|
188 |
public function deleteBySuffix($suffix)
|
|
|
189 |
{
|
|
|
190 |
$count = 0;
|
|
|
191 |
$keys = $this->_getCacheKeys();
|
|
|
192 |
if (is_array($keys)) {
|
|
|
193 |
foreach ($keys as $key) {
|
|
|
194 |
if (substr($key, -1 * strlen($suffix)) == $suffix) {
|
|
|
195 |
$count++;
|
|
|
196 |
$this->delete($key);
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
return $count;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Delete all cache entries from the cache driver
|
|
|
205 |
*
|
|
|
206 |
* @return integer $count The number of deleted cache entries
|
|
|
207 |
*/
|
|
|
208 |
public function deleteAll()
|
|
|
209 |
{
|
|
|
210 |
$count = 0;
|
|
|
211 |
if (is_array($keys = $this->_getCacheKeys())) {
|
|
|
212 |
foreach ($keys as $key) {
|
|
|
213 |
$count++;
|
|
|
214 |
$this->delete($key);
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
return $count;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Get the hash key passing its suffix
|
|
|
222 |
*
|
|
|
223 |
* @param string $id The hash key suffix
|
|
|
224 |
* @return string Hash key to be used by drivers
|
|
|
225 |
*/
|
|
|
226 |
protected function _getKey($id)
|
|
|
227 |
{
|
|
|
228 |
$prefix = isset($this->_options['prefix']) ? $this->_options['prefix'] : '';
|
|
|
229 |
|
|
|
230 |
if ( ! $prefix || strpos($id, $prefix) === 0) {
|
|
|
231 |
return $id;
|
|
|
232 |
} else {
|
|
|
233 |
return $prefix . $id;
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Fetch an array of all keys stored in cache
|
|
|
239 |
*
|
|
|
240 |
* @return array Returns the array of cache keys
|
|
|
241 |
*/
|
|
|
242 |
abstract protected function _getCacheKeys();
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Fetch a cache record from this cache driver instance
|
|
|
246 |
*
|
|
|
247 |
* @param string $id cache id
|
|
|
248 |
* @param boolean $testCacheValidity if set to false, the cache validity won't be tested
|
|
|
249 |
* @return mixed Returns either the cached data or false
|
|
|
250 |
*/
|
|
|
251 |
abstract protected function _doFetch($id, $testCacheValidity = true);
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Test if a cache record exists for the passed id
|
|
|
255 |
*
|
|
|
256 |
* @param string $id cache id
|
|
|
257 |
* @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
|
|
|
258 |
*/
|
|
|
259 |
abstract protected function _doContains($id);
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Save a cache record directly. This method is implemented by the cache
|
|
|
263 |
* drivers and used in Doctrine_Cache_Driver::save()
|
|
|
264 |
*
|
|
|
265 |
* @param string $id cache id
|
|
|
266 |
* @param string $data data to cache
|
|
|
267 |
* @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
|
|
|
268 |
* @return boolean true if no problem
|
|
|
269 |
*/
|
|
|
270 |
abstract protected function _doSave($id, $data, $lifeTime = false);
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Remove a cache record directly. This method is implemented by the cache
|
|
|
274 |
* drivers and used in Doctrine_Cache_Driver::delete()
|
|
|
275 |
*
|
|
|
276 |
* @param string $id cache id
|
|
|
277 |
* @return boolean true if no problem
|
|
|
278 |
*/
|
|
|
279 |
abstract protected function _doDelete($id);
|
|
|
280 |
}
|