/ui/obminclude/lib/Zend/Cache/Backend/Apc.php

https://github.com/goldoraf/OBM · PHP · 167 lines · 64 code · 12 blank · 91 comment · 9 complexity · cce4e8e8afce5697769901f15302a365 MD5 · raw file

  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Cache_Backend_Interface
  23. */
  24. require_once 'Zend/Cache/Backend/Interface.php';
  25. /**
  26. * @see Zend_Cache_Backend
  27. */
  28. require_once 'Zend/Cache/Backend.php';
  29. /**
  30. * @package Zend_Cache
  31. * @subpackage Zend_Cache_Backend
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
  36. {
  37. /**
  38. * Constructor
  39. *
  40. * @param array $options associative array of options
  41. * @throws Zend_Cache_Exception
  42. * @return void
  43. */
  44. public function __construct(array $options = array())
  45. {
  46. if (!extension_loaded('apc')) {
  47. Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
  48. }
  49. parent::__construct($options);
  50. }
  51. /**
  52. * Test if a cache is available for the given id and (if yes) return it (false else)
  53. *
  54. * WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
  55. *
  56. * @param string $id cache id
  57. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  58. * @return string cached datas (or false)
  59. */
  60. public function load($id, $doNotTestCacheValidity = false)
  61. {
  62. if ($doNotTestCacheValidity) {
  63. $this->_log("Zend_Cache_Backend_Apc::load() : \$doNotTestCacheValidity=true is unsupported by the Apc backend");
  64. }
  65. $tmp = apc_fetch($id);
  66. if (is_array($tmp)) {
  67. return $tmp[0];
  68. }
  69. return false;
  70. }
  71. /**
  72. * Test if a cache is available or not (for the given id)
  73. *
  74. * @param string $id cache id
  75. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  76. */
  77. public function test($id)
  78. {
  79. $tmp = apc_fetch($id);
  80. if (is_array($tmp)) {
  81. return $tmp[1];
  82. }
  83. return false;
  84. }
  85. /**
  86. * Save some string datas into a cache record
  87. *
  88. * Note : $data is always "string" (serialization is done by the
  89. * core not by the backend)
  90. *
  91. * @param string $data datas to cache
  92. * @param string $id cache id
  93. * @param array $tags array of strings, the cache record will be tagged by each string entry
  94. * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
  95. * @return boolean true if no problem
  96. */
  97. public function save($data, $id, $tags = array(), $specificLifetime = false)
  98. {
  99. $lifetime = $this->getLifetime($specificLifetime);
  100. $result = apc_store($id, array($data, time()), $lifetime);
  101. if (count($tags) > 0) {
  102. $this->_log("Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend");
  103. }
  104. return $result;
  105. }
  106. /**
  107. * Remove a cache record
  108. *
  109. * @param string $id cache id
  110. * @return boolean true if no problem
  111. */
  112. public function remove($id)
  113. {
  114. return apc_delete($id);
  115. }
  116. /**
  117. * Clean some cache records
  118. *
  119. * Available modes are :
  120. * 'all' (default) => remove all cache entries ($tags is not used)
  121. * 'old' => remove too old cache entries ($tags is not used)
  122. * 'matchingTag' => remove cache entries matching all given tags
  123. * ($tags can be an array of strings or a single string)
  124. * 'notMatchingTag' => remove cache entries not matching one of the given tags
  125. * ($tags can be an array of strings or a single string)
  126. *
  127. * @param string $mode clean mode
  128. * @param array $tags array of tags
  129. * @return boolean true if no problem
  130. */
  131. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  132. {
  133. if ($mode==Zend_Cache::CLEANING_MODE_ALL) {
  134. return apc_clear_cache('user');
  135. }
  136. if ($mode==Zend_Cache::CLEANING_MODE_OLD) {
  137. $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
  138. }
  139. if ($mode==Zend_Cache::CLEANING_MODE_MATCHING_TAG) {
  140. $this->_log("Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend");
  141. }
  142. if ($mode==Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG) {
  143. $this->_log("Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend");
  144. }
  145. }
  146. /**
  147. * Return true if the automatic cleaning is available for the backend
  148. *
  149. * @return boolean
  150. */
  151. public function isAutomaticCleaningAvailable()
  152. {
  153. return false;
  154. }
  155. }