PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/Cache/Backend/WinCache.php

https://bitbucket.org/simukti/zf1
PHP | 349 lines | 157 code | 22 blank | 170 comment | 13 complexity | d3c567a87e267e15da67c30031bdc0b9 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-2012 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. /**
  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-2012 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_WinCache extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
  37. {
  38. /**
  39. * Log message
  40. */
  41. const TAGS_UNSUPPORTED_BY_CLEAN_OF_WINCACHE_BACKEND = 'Zend_Cache_Backend_WinCache::clean() : tags are unsupported by the WinCache backend';
  42. const TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND = 'Zend_Cache_Backend_WinCache::save() : tags are unsupported by the WinCache 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('wincache')) {
  53. Zend_Cache::throwException('The wincache 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 WinCache 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 = wincache_ucache_get($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 = wincache_ucache_get($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 = wincache_ucache_set($id, array($data, time(), $lifetime), $lifetime);
  104. if (count($tags) > 0) {
  105. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_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 wincache_ucache_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 wincache_ucache_clear();
  139. break;
  140. case Zend_Cache::CLEANING_MODE_OLD:
  141. $this->_log("Zend_Cache_Backend_WinCache::clean() : CLEANING_MODE_OLD is unsupported by the WinCache 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_WINCACHE_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 = wincache_ucache_meminfo();
  174. $memSize = $mem['memory_total'];
  175. $memUsed = $memSize - $mem['memory_free'];
  176. if ($memSize == 0) {
  177. Zend_Cache::throwException('can\'t get WinCache memory size');
  178. }
  179. if ($memUsed > $memSize) {
  180. return 100;
  181. }
  182. return ((int) (100. * ($memUsed / $memSize)));
  183. }
  184. /**
  185. * Return an array of stored tags
  186. *
  187. * @return array array of stored tags (string)
  188. */
  189. public function getTags()
  190. {
  191. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND);
  192. return array();
  193. }
  194. /**
  195. * Return an array of stored cache ids which match given tags
  196. *
  197. * In case of multiple tags, a logical AND is made between tags
  198. *
  199. * @param array $tags array of tags
  200. * @return array array of matching cache ids (string)
  201. */
  202. public function getIdsMatchingTags($tags = array())
  203. {
  204. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND);
  205. return array();
  206. }
  207. /**
  208. * Return an array of stored cache ids which don't match given tags
  209. *
  210. * In case of multiple tags, a logical OR is made between tags
  211. *
  212. * @param array $tags array of tags
  213. * @return array array of not matching cache ids (string)
  214. */
  215. public function getIdsNotMatchingTags($tags = array())
  216. {
  217. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND);
  218. return array();
  219. }
  220. /**
  221. * Return an array of stored cache ids which match any given tags
  222. *
  223. * In case of multiple tags, a logical AND is made between tags
  224. *
  225. * @param array $tags array of tags
  226. * @return array array of any matching cache ids (string)
  227. */
  228. public function getIdsMatchingAnyTags($tags = array())
  229. {
  230. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_WINCACHE_BACKEND);
  231. return array();
  232. }
  233. /**
  234. * Return an array of stored cache ids
  235. *
  236. * @return array array of stored cache ids (string)
  237. */
  238. public function getIds()
  239. {
  240. $res = array();
  241. $array = wincache_ucache_info();
  242. $records = $array['ucache_entries'];
  243. foreach ($records as $record) {
  244. $res[] = $record['key_name'];
  245. }
  246. return $res;
  247. }
  248. /**
  249. * Return an array of metadatas for the given cache id
  250. *
  251. * The array must include these keys :
  252. * - expire : the expire timestamp
  253. * - tags : a string array of tags
  254. * - mtime : timestamp of last modification time
  255. *
  256. * @param string $id cache id
  257. * @return array array of metadatas (false if the cache id is not found)
  258. */
  259. public function getMetadatas($id)
  260. {
  261. $tmp = wincache_ucache_get($id);
  262. if (is_array($tmp)) {
  263. $data = $tmp[0];
  264. $mtime = $tmp[1];
  265. if (!isset($tmp[2])) {
  266. return false;
  267. }
  268. $lifetime = $tmp[2];
  269. return array(
  270. 'expire' => $mtime + $lifetime,
  271. 'tags' => array(),
  272. 'mtime' => $mtime
  273. );
  274. }
  275. return false;
  276. }
  277. /**
  278. * Give (if possible) an extra lifetime to the given cache id
  279. *
  280. * @param string $id cache id
  281. * @param int $extraLifetime
  282. * @return boolean true if ok
  283. */
  284. public function touch($id, $extraLifetime)
  285. {
  286. $tmp = wincache_ucache_get($id);
  287. if (is_array($tmp)) {
  288. $data = $tmp[0];
  289. $mtime = $tmp[1];
  290. if (!isset($tmp[2])) {
  291. return false;
  292. }
  293. $lifetime = $tmp[2];
  294. $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
  295. if ($newLifetime <=0) {
  296. return false;
  297. }
  298. return wincache_ucache_set($id, array($data, time(), $newLifetime), $newLifetime);
  299. }
  300. return false;
  301. }
  302. /**
  303. * Return an associative array of capabilities (booleans) of the backend
  304. *
  305. * The array must include these keys :
  306. * - automatic_cleaning (is automating cleaning necessary)
  307. * - tags (are tags supported)
  308. * - expired_read (is it possible to read expired cache records
  309. * (for doNotTestCacheValidity option for example))
  310. * - priority does the backend deal with priority when saving
  311. * - infinite_lifetime (is infinite lifetime can work with this backend)
  312. * - get_list (is it possible to get the list of cache ids and the complete list of tags)
  313. *
  314. * @return array associative of with capabilities
  315. */
  316. public function getCapabilities()
  317. {
  318. return array(
  319. 'automatic_cleaning' => false,
  320. 'tags' => false,
  321. 'expired_read' => false,
  322. 'priority' => false,
  323. 'infinite_lifetime' => false,
  324. 'get_list' => true
  325. );
  326. }
  327. }