ZendServer.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage Zend_Cache_Backend
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** @see Zend_Cache_Backend_Interface */
  22. require_once 'Zend/Cache/Backend/Interface.php';
  23. /** @see Zend_Cache_Backend */
  24. require_once 'Zend/Cache/Backend.php';
  25. /**
  26. * @package Zend_Cache
  27. * @subpackage Zend_Cache_Backend
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Cache_Backend_ZendServer extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
  32. {
  33. /**
  34. * Available options
  35. *
  36. * =====> (string) namespace :
  37. * Namespace to be used for chaching operations
  38. *
  39. * @var array available options
  40. */
  41. protected $_options = array(
  42. 'namespace' => 'zendframework'
  43. );
  44. /**
  45. * Store data
  46. *
  47. * @var mixed $data Object to store
  48. * @var string $id Cache id
  49. * @var int $timeToLive Time to live in seconds
  50. * @throws Zend_Cache_Exception
  51. */
  52. abstract protected function _store($data, $id, $timeToLive);
  53. /**
  54. * Fetch data
  55. *
  56. * @var mixed $data Object to store
  57. * @var string $id Cache id
  58. * @var int $timeToLive Time to live in seconds
  59. * @throws Zend_Cache_Exception
  60. */
  61. abstract protected function _fetch($id);
  62. /**
  63. * Unset data
  64. *
  65. * @var string $id Cache id
  66. */
  67. abstract protected function _unset($id);
  68. /**
  69. * Clear cache
  70. */
  71. abstract protected function _clear();
  72. /**
  73. * Test if a cache is available for the given id and (if yes) return it (false else)
  74. *
  75. * @param string $id cache id
  76. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  77. * @return string cached datas (or false)
  78. */
  79. public function load($id, $doNotTestCacheValidity = false)
  80. {
  81. $tmp = $this->_fetch($id);
  82. if ($tmp !== null) {
  83. return $tmp;
  84. }
  85. return false;
  86. }
  87. /**
  88. * Test if a cache is available or not (for the given id)
  89. *
  90. * @param string $id cache id
  91. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  92. * @throws Zend_Cache_Exception
  93. */
  94. public function test($id)
  95. {
  96. $tmp = $this->_fetch('internal-metadatas---' . $id);
  97. if ($tmp !== null) {
  98. if (!is_array($tmp) || !isset($tmp['mtime'])) {
  99. Zend_Cache::throwException('Cache metadata for \'' . $id . '\' id is corrupted' );
  100. }
  101. return $tmp['mtime'];
  102. }
  103. return false;
  104. }
  105. /**
  106. * Compute & return the expire time
  107. *
  108. * @return int expire time (unix timestamp)
  109. */
  110. private function _expireTime($lifetime)
  111. {
  112. if ($lifetime === null) {
  113. return 9999999999;
  114. }
  115. return time() + $lifetime;
  116. }
  117. /**
  118. * Save some string datas into a cache record
  119. *
  120. * Note : $data is always "string" (serialization is done by the
  121. * core not by the backend)
  122. *
  123. * @param string $data datas to cache
  124. * @param string $id cache id
  125. * @param array $tags array of strings, the cache record will be tagged by each string entry
  126. * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
  127. * @return boolean true if no problem
  128. */
  129. public function save($data, $id, $tags = array(), $specificLifetime = false)
  130. {
  131. $lifetime = $this->getLifetime($specificLifetime);
  132. $metadatas = array(
  133. 'mtime' => time(),
  134. 'expire' => $this->_expireTime($lifetime),
  135. );
  136. if (count($tags) > 0) {
  137. $this->_log('Zend_Cache_Backend_ZendServer::save() : tags are unsupported by the ZendServer backends');
  138. }
  139. return $this->_store($data, $id, $lifetime) &&
  140. $this->_store($metadatas, 'internal-metadatas---' . $id, $lifetime);
  141. }
  142. /**
  143. * Remove a cache record
  144. *
  145. * @param string $id cache id
  146. * @return boolean true if no problem
  147. */
  148. public function remove($id)
  149. {
  150. $result1 = $this->_unset($id);
  151. $result2 = $this->_unset('internal-metadatas---' . $id);
  152. return $result1 && $result2;
  153. }
  154. /**
  155. * Clean some cache records
  156. *
  157. * Available modes are :
  158. * 'all' (default) => remove all cache entries ($tags is not used)
  159. * 'old' => unsupported
  160. * 'matchingTag' => unsupported
  161. * 'notMatchingTag' => unsupported
  162. * 'matchingAnyTag' => unsupported
  163. *
  164. * @param string $mode clean mode
  165. * @param array $tags array of tags
  166. * @throws Zend_Cache_Exception
  167. * @return boolean true if no problem
  168. */
  169. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  170. {
  171. switch ($mode) {
  172. case Zend_Cache::CLEANING_MODE_ALL:
  173. $this->_clear();
  174. return true;
  175. break;
  176. case Zend_Cache::CLEANING_MODE_OLD:
  177. $this->_log("Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends.");
  178. break;
  179. case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  180. case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  181. case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  182. $this->_clear();
  183. $this->_log('Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.');
  184. break;
  185. default:
  186. Zend_Cache::throwException('Invalid mode for clean() method');
  187. break;
  188. }
  189. }
  190. }