Eaccelerator.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Cache\Backend;
  7. class Eaccelerator extends \Zend_Cache_Backend implements \Zend_Cache_Backend_ExtendedInterface
  8. {
  9. /**
  10. * Log message
  11. */
  12. const TAGS_UNSUPPORTED_BY_CLEAN_OF_EACCELERATOR_BACKEND =
  13. 'Magento\Framework\Cache\Backend\Eaccelerator::clean() : tags are unsupported by the Eaccelerator backend';
  14. const TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND =
  15. 'Magento\Framework\Cache\Backend\Eaccelerator::save() : tags are unsupported by the Eaccelerator backend';
  16. /**
  17. * Constructor
  18. *
  19. * @param array $options associative array of options
  20. * @throws \Zend_Cache_Exception
  21. */
  22. public function __construct(array $options = [])
  23. {
  24. if (!extension_loaded('eaccelerator')) {
  25. \Zend_Cache::throwException('The eaccelerator extension must be loaded for using this backend !');
  26. }
  27. parent::__construct($options);
  28. }
  29. /**
  30. * Test if a cache is available for the given id and (if yes) return it (false else)
  31. *
  32. * WARNING $doNotTestCacheValidity=true is unsupported by the Eaccelerator backend
  33. *
  34. * @param string $id cache id
  35. * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
  36. * @return string cached datas (or false)
  37. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  38. */
  39. public function load($id, $doNotTestCacheValidity = false)
  40. {
  41. $tmp = eaccelerator_get($id);
  42. if (is_array($tmp)) {
  43. return $tmp[0];
  44. }
  45. return false;
  46. }
  47. /**
  48. * Test if a cache is available or not (for the given id)
  49. *
  50. * @param string $id cache id
  51. * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  52. */
  53. public function test($id)
  54. {
  55. $tmp = eaccelerator_get($id);
  56. if (is_array($tmp)) {
  57. return $tmp[1];
  58. }
  59. return false;
  60. }
  61. /**
  62. * Save some string datas into a cache record
  63. *
  64. * Note : $data is always "string" (serialization is done by the
  65. * core not by the backend)
  66. *
  67. * @param string $data datas to cache
  68. * @param string $id cache id
  69. * @param string[] $tags array of strings, the cache record will be tagged by each string entry
  70. * @param int|bool $specificLifetime Integer to set a specific lifetime or null for infinite lifetime
  71. * @return bool true if no problem
  72. */
  73. public function save($data, $id, $tags = [], $specificLifetime = false)
  74. {
  75. $lifetime = $this->getLifetime($specificLifetime);
  76. $result = eaccelerator_put($id, [$data, time(), $lifetime], $lifetime);
  77. if (count($tags) > 0) {
  78. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Remove a cache record
  84. *
  85. * @param string $id cache id
  86. * @return bool true if no problem
  87. */
  88. public function remove($id)
  89. {
  90. return eaccelerator_rm($id);
  91. }
  92. /**
  93. * Clean some cache records
  94. *
  95. * Available modes are :
  96. * 'all' (default) => remove all cache entries ($tags is not used)
  97. * 'old' => unsupported
  98. * 'matchingTag' => unsupported
  99. * 'notMatchingTag' => unsupported
  100. * 'matchingAnyTag' => unsupported
  101. *
  102. * @param string $mode clean mode
  103. * @param string[] $tags array of tags
  104. * @throws \Zend_Cache_Exception
  105. * @return bool|void true if no problem
  106. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  107. */
  108. public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
  109. {
  110. switch ($mode) {
  111. case \Zend_Cache::CLEANING_MODE_ALL:
  112. return eaccelerator_clean();
  113. break;
  114. case \Zend_Cache::CLEANING_MODE_OLD:
  115. $this->_log(
  116. "Magento\Framework\Cache\Backend\Eaccelerator::clean() : ".
  117. "CLEANING_MODE_OLD is unsupported by the Eaccelerator backend"
  118. );
  119. break;
  120. case \Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  121. case \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  122. case \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  123. $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_EACCELERATOR_BACKEND);
  124. break;
  125. default:
  126. \Zend_Cache::throwException('Invalid mode for clean() method');
  127. break;
  128. }
  129. }
  130. /**
  131. * Return the filling percentage of the backend storage
  132. *
  133. * @throws \Zend_Cache_Exception
  134. * @return int integer between 0 and 100
  135. */
  136. public function getFillingPercentage()
  137. {
  138. $mem = eaccelerator_info();
  139. $memSize = $mem['memorySize'];
  140. $memAvailable = $mem['memoryAvailable'];
  141. $memUsed = $memSize - $memAvailable;
  142. if ($memSize == 0) {
  143. \Zend_Cache::throwException('can\'t get eaccelerator memory size');
  144. }
  145. if ($memUsed > $memSize) {
  146. return 100;
  147. }
  148. return (int)(100. * ($memUsed / $memSize));
  149. }
  150. /**
  151. * Return an array of stored tags
  152. *
  153. * @return string[] array of stored tags (string)
  154. */
  155. public function getTags()
  156. {
  157. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
  158. return [];
  159. }
  160. /**
  161. * Return an array of stored cache ids which match given tags
  162. *
  163. * In case of multiple tags, a logical AND is made between tags
  164. *
  165. * @param array $tags array of tags
  166. * @return string[] array of matching cache ids (string)
  167. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  168. */
  169. public function getIdsMatchingTags($tags = [])
  170. {
  171. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
  172. return [];
  173. }
  174. /**
  175. * Return an array of stored cache ids which don't match given tags
  176. *
  177. * In case of multiple tags, a logical OR is made between tags
  178. *
  179. * @param string[] $tags array of tags
  180. * @return string[] array of not matching cache ids (string)
  181. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  182. */
  183. public function getIdsNotMatchingTags($tags = [])
  184. {
  185. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
  186. return [];
  187. }
  188. /**
  189. * Return an array of stored cache ids which match any given tags
  190. *
  191. * In case of multiple tags, a logical AND is made between tags
  192. *
  193. * @param string[] $tags array of tags
  194. * @return string[] array of any matching cache ids (string)
  195. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  196. */
  197. public function getIdsMatchingAnyTags($tags = [])
  198. {
  199. $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
  200. return [];
  201. }
  202. /**
  203. * Return an array of stored cache ids
  204. *
  205. * @return string[] array of stored cache ids (string)
  206. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  207. */
  208. public function getIds()
  209. {
  210. $res = [];
  211. $array = eaccelerator_list_keys();
  212. foreach ($array as $key => $info) {
  213. $res[] = $key;
  214. }
  215. return $res;
  216. }
  217. /**
  218. * Return an array of metadatas for the given cache id
  219. *
  220. * The array must include these keys :
  221. * - expire : the expire timestamp
  222. * - tags : a string array of tags
  223. * - mtime : timestamp of last modification time
  224. *
  225. * @param string $id cache id
  226. * @return array|false array of metadatas (false if the cache id is not found)
  227. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  228. */
  229. public function getMetadatas($id)
  230. {
  231. $tmp = eaccelerator_get($id);
  232. if (is_array($tmp)) {
  233. $data = $tmp[0];
  234. $mtime = $tmp[1];
  235. if (!isset($tmp[2])) {
  236. // because this record is only with 1.7 release
  237. // if old cache records are still there...
  238. return false;
  239. }
  240. $lifetime = $tmp[2];
  241. return ['expire' => $mtime + $lifetime, 'tags' => [], 'mtime' => $mtime];
  242. }
  243. return false;
  244. }
  245. /**
  246. * Give (if possible) an extra lifetime to the given cache id
  247. *
  248. * @param string $id cache id
  249. * @param int $extraLifetime
  250. * @return bool true if ok
  251. */
  252. public function touch($id, $extraLifetime)
  253. {
  254. $tmp = eaccelerator_get($id);
  255. if (is_array($tmp)) {
  256. $data = $tmp[0];
  257. $mtime = $tmp[1];
  258. if (!isset($tmp[2])) {
  259. // because this record is only with 1.7 release
  260. // if old cache records are still there...
  261. return false;
  262. }
  263. $lifetime = $tmp[2];
  264. $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
  265. if ($newLifetime <= 0) {
  266. return false;
  267. }
  268. eaccelerator_put($id, [$data, time(), $newLifetime], $newLifetime);
  269. return true;
  270. }
  271. return false;
  272. }
  273. /**
  274. * Return an associative array of capabilities (booleans) of the backend
  275. *
  276. * The array must include these keys :
  277. * - automatic_cleaning (is automating cleaning necessary)
  278. * - tags (are tags supported)
  279. * - expired_read (is it possible to read expired cache records
  280. * (for doNotTestCacheValidity option for example))
  281. * - priority does the backend deal with priority when saving
  282. * - infinite_lifetime (is infinite lifetime can work with this backend)
  283. * - get_list (is it possible to get the list of cache ids and the complete list of tags)
  284. *
  285. * @return array associative of with capabilities
  286. */
  287. public function getCapabilities()
  288. {
  289. return [
  290. 'automatic_cleaning' => false,
  291. 'tags' => false,
  292. 'expired_read' => false,
  293. 'priority' => false,
  294. 'infinite_lifetime' => false,
  295. 'get_list' => true
  296. ];
  297. }
  298. }