PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Doctrine/Cache/Driver.php

https://github.com/ascorbic/doctrine-tracker
PHP | 329 lines | 137 code | 30 blank | 162 comment | 16 complexity | ba61bf73ee4ffe8adfe203b9c195014f MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * $Id$
  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.phpdoctrine.org>.
  20. */
  21. /**
  22. * Abstract cache driver class
  23. *
  24. * @package Doctrine
  25. * @subpackage Cache
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.phpdoctrine.org
  28. * @since 1.0
  29. * @version $Revision$
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. * @author Jonathan H. Wage <jonwage@gmail.com>
  32. */
  33. abstract class Doctrine_Cache_Driver implements Doctrine_Cache_Interface
  34. {
  35. /**
  36. * The key used to store the index of cache keys in this cache driver instance
  37. *
  38. * @var string
  39. */
  40. protected $_cacheKeyIndexKey = 'doctrine_cache_keys';
  41. /**
  42. * @var array $_options an array of options
  43. */
  44. protected $_options = array();
  45. /**
  46. * Configure cache driver with an array of options
  47. *
  48. * @param array $_options an array of options
  49. */
  50. public function __construct($options = array())
  51. {
  52. $this->_options = $options;
  53. }
  54. /**
  55. * Set option name and value
  56. *
  57. * @param mixed $option the option name
  58. * @param mixed $value option value
  59. * @return boolean TRUE on success, FALSE on failure
  60. */
  61. public function setOption($option, $value)
  62. {
  63. if (isset($this->_options[$option])) {
  64. $this->_options[$option] = $value;
  65. return true;
  66. }
  67. return false;
  68. }
  69. /**
  70. * Get value of option
  71. *
  72. * @param mixed $option the option name
  73. * @return mixed option value
  74. */
  75. public function getOption($option)
  76. {
  77. if ( ! isset($this->_options[$option])) {
  78. return null;
  79. }
  80. return $this->_options[$option];
  81. }
  82. /**
  83. * Get the number of cache records stored in this cache driver instance
  84. *
  85. * @return integer $count
  86. */
  87. public function count()
  88. {
  89. $keys = $this->fetch($this->_cacheKeyIndexKey);
  90. return $keys ? count($keys) : 0;
  91. }
  92. /**
  93. * Fetch a cache record from this cache driver instance
  94. *
  95. * @param string $id cache id
  96. * @param boolean $testCacheValidity if set to false, the cache validity won't be tested
  97. * @return mixed Returns either the cached data or false
  98. */
  99. public function fetch($id, $testCacheValidity = true)
  100. {
  101. $key = $this->_getKey($id);
  102. return $this->_doFetch($key, $testCacheValidity);
  103. }
  104. /**
  105. * Test if a cache record exists for the passed id
  106. *
  107. * @param string $id cache id
  108. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  109. */
  110. public function contains($id)
  111. {
  112. $key = $this->_getKey($id);
  113. return $this->_doContains($key);
  114. }
  115. /**
  116. * Save some string datas into a cache record
  117. *
  118. * @param string $id cache id
  119. * @param string $data data to cache
  120. * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
  121. * @param boolean $saveKey Whether or not to save the key in the cache key index
  122. * @return boolean true if no problem
  123. */
  124. public function save($id, $data, $lifeTime = false, $saveKey = true)
  125. {
  126. $key = $this->_getKey($id);
  127. if ($this->_doSave($key, $data, $lifeTime)) {
  128. if ($saveKey) {
  129. $this->_saveKey($key);
  130. }
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. /**
  137. * Remove a cache record
  138. *
  139. * Note: This method accepts wildcards with the * character
  140. *
  141. * @param string $id cache id
  142. * @return boolean true if no problem
  143. */
  144. public function delete($id)
  145. {
  146. $key = $this->_getKey($id);
  147. if (strpos($key, '*') !== false) {
  148. return $this->deleteByRegex('/' . str_replace('*', '.*', $key) . '/');
  149. }
  150. if ($this->_doDelete($key)) {
  151. $this->_deleteKey($key);
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. }
  157. /**
  158. * Delete all cache records from the cache driver
  159. *
  160. * @return void
  161. */
  162. public function deleteAll()
  163. {
  164. $keys = $this->fetch($this->_cacheKeyIndexKey);
  165. foreach ($keys as $key) {
  166. $this->delete($key);
  167. }
  168. }
  169. /**
  170. * Delete cache entries where the key matches a PHP regular expressions
  171. *
  172. * @param string $regex
  173. * @return integer $count The number of deleted cache entries
  174. */
  175. public function deleteByRegex($regex)
  176. {
  177. $count = 0;
  178. $keys = $this->fetch($this->_cacheKeyIndexKey);
  179. foreach ($keys as $key) {
  180. if (preg_match($regex, $key)) {
  181. $count++;
  182. $this->delete($key);
  183. }
  184. }
  185. return $count;
  186. }
  187. /**
  188. * Delete cache entries where the key has the passed prefix
  189. *
  190. * @param string $prefix
  191. * @return integer $count The number of deleted cache entries
  192. */
  193. public function deleteByPrefix($prefix)
  194. {
  195. $count = 0;
  196. $keys = $this->fetch($this->_cacheKeyIndexKey);
  197. foreach ($keys as $key) {
  198. if (strpos($key, $prefix) === 0) {
  199. $count++;
  200. $this->delete($key);
  201. }
  202. }
  203. return $count;
  204. }
  205. /**
  206. * Delete cache entries where the key has the passed suffix
  207. *
  208. * @param string $suffix
  209. * @return integer $count The number of deleted cache entries
  210. */
  211. public function deleteBySuffix($suffix)
  212. {
  213. $count = 0;
  214. $keys = $this->fetch($this->_cacheKeyIndexKey);
  215. foreach ($keys as $key) {
  216. if (substr($key, -1 * strlen($suffix)) == $suffix) {
  217. $count++;
  218. $this->delete($key);
  219. }
  220. }
  221. return $count;
  222. }
  223. /**
  224. * Get the hash key passing its suffix
  225. *
  226. * @param string $id The hash key suffix
  227. * @return string Hash key to be used by drivers
  228. */
  229. protected function _getKey($id)
  230. {
  231. $prefix = isset($this->_options['prefix']) ? $this->_options['prefix'] : '';
  232. if ( ! $prefix || strpos($id, $prefix) === 0) {
  233. return $id;
  234. } else {
  235. return $prefix . $id;
  236. }
  237. }
  238. /**
  239. * Save a cache key in the index of cache keys
  240. *
  241. * @param string $key
  242. * @return boolean True if successful and false if something went wrong.
  243. */
  244. protected function _saveKey($key)
  245. {
  246. $keys = $this->fetch($this->_cacheKeyIndexKey);
  247. $keys[] = $key;
  248. return $this->save($this->_cacheKeyIndexKey, $keys, null, false);
  249. }
  250. /**
  251. * Delete a cache key from the index of cache keys
  252. *
  253. * @param string $key
  254. * @return boolean True if successful and false if something went wrong.
  255. */
  256. public function _deleteKey($key)
  257. {
  258. $keys = $this->fetch($this->_cacheKeyIndexKey);
  259. $key = array_search($key, $keys);
  260. if ($key !== false) {
  261. unset($keys[$key]);
  262. return $this->save($this->_cacheKeyIndexKey, $keys, null, false);
  263. }
  264. return false;
  265. }
  266. /**
  267. * Fetch a cache record from this cache driver instance
  268. *
  269. * @param string $id cache id
  270. * @param boolean $testCacheValidity if set to false, the cache validity won't be tested
  271. * @return mixed Returns either the cached data or false
  272. */
  273. abstract protected function _doFetch($id, $testCacheValidity = true);
  274. /**
  275. * Test if a cache record exists for the passed id
  276. *
  277. * @param string $id cache id
  278. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  279. */
  280. abstract protected function _doContains($id);
  281. /**
  282. * Save a cache record directly. This method is implemented by the cache
  283. * drivers and used in Doctrine_Cache_Driver::save()
  284. *
  285. * @param string $id cache id
  286. * @param string $data data to cache
  287. * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
  288. * @return boolean true if no problem
  289. */
  290. abstract protected function _doSave($id, $data, $lifeTime = false);
  291. /**
  292. * Remove a cache record directly. This method is implemented by the cache
  293. * drivers and used in Doctrine_Cache_Driver::delete()
  294. *
  295. * @param string $id cache id
  296. * @return boolean true if no problem
  297. */
  298. abstract protected function _doDelete($id);
  299. }