ZendServer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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-2015 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-2015 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. * @param mixed $data Object to store
  49. * @param string $id Cache id
  50. * @param 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. * @param string $id Cache id
  58. * @throws Zend_Cache_Exception
  59. */
  60. abstract protected function _fetch($id);
  61. /**
  62. * Unset data
  63. *
  64. * @param string $id Cache id
  65. */
  66. abstract protected function _unset($id);
  67. /**
  68. * Clear cache
  69. */
  70. abstract protected function _clear();
  71. /**
  72. * Test if a cache is available for the given id and (if yes) return it (false else)
  73. *
  74. * @param string $id cache id
  75. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  76. * @return string cached datas (or false)
  77. */
  78. public function load($id, $doNotTestCacheValidity = false)
  79. {
  80. $tmp = $this->_fetch($id);
  81. if ($tmp !== null) {
  82. return $tmp;
  83. }
  84. return false;
  85. }
  86. /**
  87. * Test if a cache is available or not (for the given id)
  88. *
  89. * @param string $id cache id
  90. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  91. * @throws Zend_Cache_Exception
  92. */
  93. public function test($id)
  94. {
  95. $tmp = $this->_fetch('internal-metadatas---' . $id);
  96. if ($tmp !== false) {
  97. if (!is_array($tmp) || !isset($tmp['mtime'])) {
  98. Zend_Cache::throwException('Cache metadata for \'' . $id . '\' id is corrupted' );
  99. }
  100. return $tmp['mtime'];
  101. }
  102. return false;
  103. }
  104. /**
  105. * Compute & return the expire time
  106. *
  107. * @return int expire time (unix timestamp)
  108. */
  109. private function _expireTime($lifetime)
  110. {
  111. if ($lifetime === null) {
  112. return 9999999999;
  113. }
  114. return time() + $lifetime;
  115. }
  116. /**
  117. * Save some string datas into a cache record
  118. *
  119. * Note : $data is always "string" (serialization is done by the
  120. * core not by the backend)
  121. *
  122. * @param string $data datas to cache
  123. * @param string $id cache id
  124. * @param array $tags array of strings, the cache record will be tagged by each string entry
  125. * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
  126. * @return boolean true if no problem
  127. */
  128. public function save($data, $id, $tags = array(), $specificLifetime = false)
  129. {
  130. $lifetime = $this->getLifetime($specificLifetime);
  131. $metadatas = array(
  132. 'mtime' => time(),
  133. 'expire' => $this->_expireTime($lifetime),
  134. );
  135. if (count($tags) > 0) {
  136. $this->_log('Zend_Cache_Backend_ZendServer::save() : tags are unsupported by the ZendServer backends');
  137. }
  138. return $this->_store($data, $id, $lifetime) &&
  139. $this->_store($metadatas, 'internal-metadatas---' . $id, $lifetime);
  140. }
  141. /**
  142. * Remove a cache record
  143. *
  144. * @param string $id cache id
  145. * @return boolean true if no problem
  146. */
  147. public function remove($id)
  148. {
  149. $result1 = $this->_unset($id);
  150. $result2 = $this->_unset('internal-metadatas---' . $id);
  151. return $result1 && $result2;
  152. }
  153. /**
  154. * Clean some cache records
  155. *
  156. * Available modes are :
  157. * 'all' (default) => remove all cache entries ($tags is not used)
  158. * 'old' => unsupported
  159. * 'matchingTag' => unsupported
  160. * 'notMatchingTag' => unsupported
  161. * 'matchingAnyTag' => unsupported
  162. *
  163. * @param string $mode clean mode
  164. * @param array $tags array of tags
  165. * @throws Zend_Cache_Exception
  166. * @return boolean true if no problem
  167. */
  168. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  169. {
  170. switch ($mode) {
  171. case Zend_Cache::CLEANING_MODE_ALL:
  172. $this->_clear();
  173. return true;
  174. break;
  175. case Zend_Cache::CLEANING_MODE_OLD:
  176. $this->_log("Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends.");
  177. break;
  178. case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  179. case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  180. case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  181. $this->_clear();
  182. $this->_log('Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.');
  183. break;
  184. default:
  185. Zend_Cache::throwException('Invalid mode for clean() method');
  186. break;
  187. }
  188. }
  189. }