PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/Doctrine/Cache/Driver.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 280 lines | 114 code | 22 blank | 144 comment | 14 complexity | cd6b677bf1ec488a30e267b0f5dd0b60 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Driver.php 48519 2012-02-03 23:18:52Z btowles $
  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. * 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.doctrine-project.org
  28. * @since 1.0
  29. * @version $Revision: 7490 $
  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. * @var array $_options an array of options
  37. */
  38. protected $_options = array();
  39. /**
  40. * Configure cache driver with an array of options
  41. *
  42. * @param array $_options an array of options
  43. */
  44. public function __construct($options = array())
  45. {
  46. $this->_options = $options;
  47. }
  48. /**
  49. * Set option name and value
  50. *
  51. * @param mixed $option the option name
  52. * @param mixed $value option value
  53. * @return boolean TRUE on success, FALSE on failure
  54. */
  55. public function setOption($option, $value)
  56. {
  57. if (isset($this->_options[$option])) {
  58. $this->_options[$option] = $value;
  59. return true;
  60. }
  61. return false;
  62. }
  63. /**
  64. * Get value of option
  65. *
  66. * @param mixed $option the option name
  67. * @return mixed option value
  68. */
  69. public function getOption($option)
  70. {
  71. if ( ! isset($this->_options[$option])) {
  72. return null;
  73. }
  74. return $this->_options[$option];
  75. }
  76. /**
  77. * Fetch a cache record from this cache driver instance
  78. *
  79. * @param string $id cache id
  80. * @param boolean $testCacheValidity if set to false, the cache validity won't be tested
  81. * @return mixed Returns either the cached data or false
  82. */
  83. public function fetch($id, $testCacheValidity = true)
  84. {
  85. $key = $this->_getKey($id);
  86. return $this->_doFetch($key, $testCacheValidity);
  87. }
  88. /**
  89. * Test if a cache record exists for the passed id
  90. *
  91. * @param string $id cache id
  92. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  93. */
  94. public function contains($id)
  95. {
  96. $key = $this->_getKey($id);
  97. return $this->_doContains($key);
  98. }
  99. /**
  100. * Save some string datas into a cache record
  101. *
  102. * @param string $id cache id
  103. * @param string $data data to cache
  104. * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
  105. * @return boolean true if no problem
  106. */
  107. public function save($id, $data, $lifeTime = false)
  108. {
  109. $key = $this->_getKey($id);
  110. return $this->_doSave($key, $data, $lifeTime);
  111. }
  112. /**
  113. * Remove a cache record
  114. *
  115. * Note: This method accepts wildcards with the * character
  116. *
  117. * @param string $id cache id
  118. * @return boolean true if no problem
  119. */
  120. public function delete($id)
  121. {
  122. $key = $this->_getKey($id);
  123. if (strpos($key, '*') !== false) {
  124. return $this->deleteByRegex('/' . str_replace('*', '.*', $key) . '/');
  125. }
  126. return $this->_doDelete($key);
  127. }
  128. /**
  129. * Delete cache entries where the key matches a PHP regular expressions
  130. *
  131. * @param string $regex
  132. * @return integer $count The number of deleted cache entries
  133. */
  134. public function deleteByRegex($regex)
  135. {
  136. $count = 0;
  137. $keys = $this->_getCacheKeys();
  138. if (is_array($keys)) {
  139. foreach ($keys as $key) {
  140. if (preg_match($regex, $key)) {
  141. $count++;
  142. $this->delete($key);
  143. }
  144. }
  145. }
  146. return $count;
  147. }
  148. /**
  149. * Delete cache entries where the key has the passed prefix
  150. *
  151. * @param string $prefix
  152. * @return integer $count The number of deleted cache entries
  153. */
  154. public function deleteByPrefix($prefix)
  155. {
  156. $count = 0;
  157. $keys = $this->_getCacheKeys();
  158. if (is_array($keys)) {
  159. foreach ($keys as $key) {
  160. if (strpos($key, $prefix) === 0) {
  161. $count++;
  162. $this->delete($key);
  163. }
  164. }
  165. }
  166. return $count;
  167. }
  168. /**
  169. * Delete cache entries where the key has the passed suffix
  170. *
  171. * @param string $suffix
  172. * @return integer $count The number of deleted cache entries
  173. */
  174. public function deleteBySuffix($suffix)
  175. {
  176. $count = 0;
  177. $keys = $this->_getCacheKeys();
  178. if (is_array($keys)) {
  179. foreach ($keys as $key) {
  180. if (substr($key, -1 * strlen($suffix)) == $suffix) {
  181. $count++;
  182. $this->delete($key);
  183. }
  184. }
  185. }
  186. return $count;
  187. }
  188. /**
  189. * Delete all cache entries from the cache driver
  190. *
  191. * @return integer $count The number of deleted cache entries
  192. */
  193. public function deleteAll()
  194. {
  195. $count = 0;
  196. if (is_array($keys = $this->_getCacheKeys())) {
  197. foreach ($keys as $key) {
  198. $count++;
  199. $this->delete($key);
  200. }
  201. }
  202. return $count;
  203. }
  204. /**
  205. * Get the hash key passing its suffix
  206. *
  207. * @param string $id The hash key suffix
  208. * @return string Hash key to be used by drivers
  209. */
  210. protected function _getKey($id)
  211. {
  212. $prefix = isset($this->_options['prefix']) ? $this->_options['prefix'] : '';
  213. if ( ! $prefix || strpos($id, $prefix) === 0) {
  214. return $id;
  215. } else {
  216. return $prefix . $id;
  217. }
  218. }
  219. /**
  220. * Fetch an array of all keys stored in cache
  221. *
  222. * @return array Returns the array of cache keys
  223. */
  224. abstract protected function _getCacheKeys();
  225. /**
  226. * Fetch a cache record from this cache driver instance
  227. *
  228. * @param string $id cache id
  229. * @param boolean $testCacheValidity if set to false, the cache validity won't be tested
  230. * @return mixed Returns either the cached data or false
  231. */
  232. abstract protected function _doFetch($id, $testCacheValidity = true);
  233. /**
  234. * Test if a cache record exists for the passed id
  235. *
  236. * @param string $id cache id
  237. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  238. */
  239. abstract protected function _doContains($id);
  240. /**
  241. * Save a cache record directly. This method is implemented by the cache
  242. * drivers and used in Doctrine_Cache_Driver::save()
  243. *
  244. * @param string $id cache id
  245. * @param string $data data to cache
  246. * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
  247. * @return boolean true if no problem
  248. */
  249. abstract protected function _doSave($id, $data, $lifeTime = false);
  250. /**
  251. * Remove a cache record directly. This method is implemented by the cache
  252. * drivers and used in Doctrine_Cache_Driver::delete()
  253. *
  254. * @param string $id cache id
  255. * @return boolean true if no problem
  256. */
  257. abstract protected function _doDelete($id);
  258. }