PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Zend/Cache/Backend/Apc.php

https://bitbucket.org/micromax/vox-fw
PHP | 355 lines | 159 code | 22 blank | 174 comment | 13 complexity | a08bfa75fc61070d2d4d2a1f32668a6b 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Apc.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Cache_Backend_Interface
  24. */
  25. require_once 'Zend/Cache/Backend/ExtendedInterface.php';
  26. /**
  27. * @see Zend_Cache_Backend
  28. */
  29. require_once 'Zend/Cache/Backend.php';
  30. /**
  31. * @package Zend_Cache
  32. * @subpackage Zend_Cache_Backend
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
  37. {
  38. /**
  39. * Log message
  40. */
  41. const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend';
  42. const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend';
  43. /**
  44. * Constructor
  45. *
  46. * @param array $options associative array of options
  47. * @throws Zend_Cache_Exception
  48. * @return void
  49. */
  50. public function __construct(array $options = array())
  51. {
  52. if (!extension_loaded('apc')) {
  53. Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
  54. }
  55. parent::__construct($options);
  56. }
  57. /**
  58. * Test if a cache is available for the given id and (if yes) return it (false else)
  59. *
  60. * WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
  61. *
  62. * @param string $id cache id
  63. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  64. * @return string cached datas (or false)
  65. */
  66. public function load($id, $doNotTestCacheValidity = false)
  67. {
  68. $tmp = apc_fetch($id);
  69. if (is_array($tmp)) {
  70. return $tmp[0];
  71. }
  72. return false;
  73. }
  74. /**
  75. * Test if a cache is available or not (for the given id)
  76. *
  77. * @param string $id cache id
  78. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  79. */
  80. public function test($id)
  81. {
  82. $tmp = apc_fetch($id);
  83. if (is_array($tmp)) {
  84. return $tmp[1];
  85. }
  86. return false;
  87. }
  88. /**
  89. * Save some string datas into a cache record
  90. *
  91. * Note : $data is always "string" (serialization is done by the
  92. * core not by the backend)
  93. *
  94. * @param string $data datas to cache
  95. * @param string $id cache id
  96. * @param array $tags array of strings, the cache record will be tagged by each string entry
  97. * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
  98. * @return boolean true if no problem
  99. */
  100. public function save($data, $id, $tags = array(), $specificLifetime = false)
  101. {
  102. $lifetime = $this->getLifetime($specificLifetime);
  103. $result = apc_store($id, array($data, time(), $lifetime), $lifetime);
  104. if (count($tags) > 0) {
  105. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  106. }
  107. return $result;
  108. }
  109. /**
  110. * Remove a cache record
  111. *
  112. * @param string $id cache id
  113. * @return boolean true if no problem
  114. */
  115. public function remove($id)
  116. {
  117. return apc_delete($id);
  118. }
  119. /**
  120. * Clean some cache records
  121. *
  122. * Available modes are :
  123. * 'all' (default) => remove all cache entries ($tags is not used)
  124. * 'old' => unsupported
  125. * 'matchingTag' => unsupported
  126. * 'notMatchingTag' => unsupported
  127. * 'matchingAnyTag' => unsupported
  128. *
  129. * @param string $mode clean mode
  130. * @param array $tags array of tags
  131. * @throws Zend_Cache_Exception
  132. * @return boolean true if no problem
  133. */
  134. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  135. {
  136. switch ($mode) {
  137. case Zend_Cache::CLEANING_MODE_ALL:
  138. return apc_clear_cache('user');
  139. break;
  140. case Zend_Cache::CLEANING_MODE_OLD:
  141. $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
  142. break;
  143. case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  144. case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  145. case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  146. $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
  147. break;
  148. default:
  149. Zend_Cache::throwException('Invalid mode for clean() method');
  150. break;
  151. }
  152. }
  153. /**
  154. * Return true if the automatic cleaning is available for the backend
  155. *
  156. * DEPRECATED : use getCapabilities() instead
  157. *
  158. * @deprecated
  159. * @return boolean
  160. */
  161. public function isAutomaticCleaningAvailable()
  162. {
  163. return false;
  164. }
  165. /**
  166. * Return the filling percentage of the backend storage
  167. *
  168. * @throws Zend_Cache_Exception
  169. * @return int integer between 0 and 100
  170. */
  171. public function getFillingPercentage()
  172. {
  173. $mem = apc_sma_info(true);
  174. $memSize = $mem['num_seg'] * $mem['seg_size'];
  175. $memAvailable= $mem['avail_mem'];
  176. $memUsed = $memSize - $memAvailable;
  177. if ($memSize == 0) {
  178. Zend_Cache::throwException('can\'t get apc memory size');
  179. }
  180. if ($memUsed > $memSize) {
  181. return 100;
  182. }
  183. return ((int) (100. * ($memUsed / $memSize)));
  184. }
  185. /**
  186. * Return an array of stored tags
  187. *
  188. * @return array array of stored tags (string)
  189. */
  190. public function getTags()
  191. {
  192. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  193. return array();
  194. }
  195. /**
  196. * Return an array of stored cache ids which match given tags
  197. *
  198. * In case of multiple tags, a logical AND is made between tags
  199. *
  200. * @param array $tags array of tags
  201. * @return array array of matching cache ids (string)
  202. */
  203. public function getIdsMatchingTags($tags = array())
  204. {
  205. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  206. return array();
  207. }
  208. /**
  209. * Return an array of stored cache ids which don't match given tags
  210. *
  211. * In case of multiple tags, a logical OR is made between tags
  212. *
  213. * @param array $tags array of tags
  214. * @return array array of not matching cache ids (string)
  215. */
  216. public function getIdsNotMatchingTags($tags = array())
  217. {
  218. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  219. return array();
  220. }
  221. /**
  222. * Return an array of stored cache ids which match any given tags
  223. *
  224. * In case of multiple tags, a logical AND is made between tags
  225. *
  226. * @param array $tags array of tags
  227. * @return array array of any matching cache ids (string)
  228. */
  229. public function getIdsMatchingAnyTags($tags = array())
  230. {
  231. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  232. return array();
  233. }
  234. /**
  235. * Return an array of stored cache ids
  236. *
  237. * @return array array of stored cache ids (string)
  238. */
  239. public function getIds()
  240. {
  241. $res = array();
  242. $array = apc_cache_info('user', false);
  243. $records = $array['cache_list'];
  244. foreach ($records as $record) {
  245. $res[] = $record['info'];
  246. }
  247. return $res;
  248. }
  249. /**
  250. * Return an array of metadatas for the given cache id
  251. *
  252. * The array must include these keys :
  253. * - expire : the expire timestamp
  254. * - tags : a string array of tags
  255. * - mtime : timestamp of last modification time
  256. *
  257. * @param string $id cache id
  258. * @return array array of metadatas (false if the cache id is not found)
  259. */
  260. public function getMetadatas($id)
  261. {
  262. $tmp = apc_fetch($id);
  263. if (is_array($tmp)) {
  264. $data = $tmp[0];
  265. $mtime = $tmp[1];
  266. if (!isset($tmp[2])) {
  267. // because this record is only with 1.7 release
  268. // if old cache records are still there...
  269. return false;
  270. }
  271. $lifetime = $tmp[2];
  272. return array(
  273. 'expire' => $mtime + $lifetime,
  274. 'tags' => array(),
  275. 'mtime' => $mtime
  276. );
  277. }
  278. return false;
  279. }
  280. /**
  281. * Give (if possible) an extra lifetime to the given cache id
  282. *
  283. * @param string $id cache id
  284. * @param int $extraLifetime
  285. * @return boolean true if ok
  286. */
  287. public function touch($id, $extraLifetime)
  288. {
  289. $tmp = apc_fetch($id);
  290. if (is_array($tmp)) {
  291. $data = $tmp[0];
  292. $mtime = $tmp[1];
  293. if (!isset($tmp[2])) {
  294. // because this record is only with 1.7 release
  295. // if old cache records are still there...
  296. return false;
  297. }
  298. $lifetime = $tmp[2];
  299. $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
  300. if ($newLifetime <=0) {
  301. return false;
  302. }
  303. apc_store($id, array($data, time(), $newLifetime), $newLifetime);
  304. return true;
  305. }
  306. return false;
  307. }
  308. /**
  309. * Return an associative array of capabilities (booleans) of the backend
  310. *
  311. * The array must include these keys :
  312. * - automatic_cleaning (is automating cleaning necessary)
  313. * - tags (are tags supported)
  314. * - expired_read (is it possible to read expired cache records
  315. * (for doNotTestCacheValidity option for example))
  316. * - priority does the backend deal with priority when saving
  317. * - infinite_lifetime (is infinite lifetime can work with this backend)
  318. * - get_list (is it possible to get the list of cache ids and the complete list of tags)
  319. *
  320. * @return array associative of with capabilities
  321. */
  322. public function getCapabilities()
  323. {
  324. return array(
  325. 'automatic_cleaning' => false,
  326. 'tags' => false,
  327. 'expired_read' => false,
  328. 'priority' => false,
  329. 'infinite_lifetime' => false,
  330. 'get_list' => true
  331. );
  332. }
  333. }