ZendServer.php 6.6 KB

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