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

/concrete/libraries/3rdparty/Zend/Cache/Backend/ZendPlatform.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 317 lines | 181 code | 18 blank | 118 comment | 46 complexity | 9699a7e582d0dca90cd4f92f3cdcfd05 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: ZendPlatform.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Cache_Backend_Interface
  24. */
  25. require_once 'Zend/Cache/Backend.php';
  26. /**
  27. * @see Zend_Cache_Backend_Interface
  28. */
  29. require_once 'Zend/Cache/Backend/Interface.php';
  30. /**
  31. * Impementation of Zend Cache Backend using the Zend Platform (Output Content Caching)
  32. *
  33. * @package Zend_Cache
  34. * @subpackage Zend_Cache_Backend
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Cache_Backend_ZendPlatform extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
  39. {
  40. /**
  41. * internal ZP prefix
  42. */
  43. const TAGS_PREFIX = "internal_ZPtag:";
  44. /**
  45. * Constructor
  46. * Validate that the Zend Platform is loaded and licensed
  47. *
  48. * @param array $options Associative array of options
  49. * @throws Zend_Cache_Exception
  50. * @return void
  51. */
  52. public function __construct(array $options = array())
  53. {
  54. if (!function_exists('accelerator_license_info')) {
  55. Zend_Cache::throwException('The Zend Platform extension must be loaded for using this backend !');
  56. }
  57. if (!function_exists('accelerator_get_configuration')) {
  58. $licenseInfo = accelerator_license_info();
  59. Zend_Cache::throwException('The Zend Platform extension is not loaded correctly: '.$licenseInfo['failure_reason']);
  60. }
  61. $accConf = accelerator_get_configuration();
  62. if (@!$accConf['output_cache_licensed']) {
  63. Zend_Cache::throwException('The Zend Platform extension does not have the proper license to use content caching features');
  64. }
  65. if (@!$accConf['output_cache_enabled']) {
  66. Zend_Cache::throwException('The Zend Platform content caching feature must be enabled for using this backend, set the \'zend_accelerator.output_cache_enabled\' directive to On !');
  67. }
  68. if (!is_writable($accConf['output_cache_dir'])) {
  69. Zend_Cache::throwException('The cache copies directory \''. ini_get('zend_accelerator.output_cache_dir') .'\' must be writable !');
  70. }
  71. parent:: __construct($options);
  72. }
  73. /**
  74. * Test if a cache is available for the given id and (if yes) return it (false else)
  75. *
  76. * @param string $id Cache id
  77. * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
  78. * @return string Cached data (or false)
  79. */
  80. public function load($id, $doNotTestCacheValidity = false)
  81. {
  82. // doNotTestCacheValidity implemented by giving zero lifetime to the cache
  83. if ($doNotTestCacheValidity) {
  84. $lifetime = 0;
  85. } else {
  86. $lifetime = $this->_directives['lifetime'];
  87. }
  88. $res = output_cache_get($id, $lifetime);
  89. if($res) {
  90. return $res[0];
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * Test if a cache is available or not (for the given id)
  97. *
  98. * @param string $id Cache id
  99. * @return mixed|false false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  100. */
  101. public function test($id)
  102. {
  103. $result = output_cache_get($id, $this->_directives['lifetime']);
  104. if ($result) {
  105. return $result[1];
  106. }
  107. return false;
  108. }
  109. /**
  110. * Save some string datas into a cache record
  111. *
  112. * Note : $data is always "string" (serialization is done by the
  113. * core not by the backend)
  114. *
  115. * @param string $data Data to cache
  116. * @param string $id Cache id
  117. * @param array $tags Array of strings, the cache record will be tagged by each string entry
  118. * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
  119. * @return boolean true if no problem
  120. */
  121. public function save($data, $id, $tags = array(), $specificLifetime = false)
  122. {
  123. if (!($specificLifetime === false)) {
  124. $this->_log("Zend_Cache_Backend_ZendPlatform::save() : non false specifc lifetime is unsuported for this backend");
  125. }
  126. $lifetime = $this->_directives['lifetime'];
  127. $result1 = output_cache_put($id, array($data, time()));
  128. $result2 = (count($tags) == 0);
  129. foreach ($tags as $tag) {
  130. $tagid = self::TAGS_PREFIX.$tag;
  131. $old_tags = output_cache_get($tagid, $lifetime);
  132. if ($old_tags === false) {
  133. $old_tags = array();
  134. }
  135. $old_tags[$id] = $id;
  136. output_cache_remove_key($tagid);
  137. $result2 = output_cache_put($tagid, $old_tags);
  138. }
  139. return $result1 && $result2;
  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. return output_cache_remove_key($id);
  150. }
  151. /**
  152. * Clean some cache records
  153. *
  154. * Available modes are :
  155. * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
  156. * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
  157. * This mode is not supported in this backend
  158. * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
  159. * ($tags can be an array of strings or a single string)
  160. * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => unsupported
  161. * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
  162. * ($tags can be an array of strings or a single string)
  163. *
  164. * @param string $mode Clean mode
  165. * @param array $tags Array of tags
  166. * @throws Zend_Cache_Exception
  167. * @return boolean True if no problem
  168. */
  169. public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  170. {
  171. switch ($mode) {
  172. case Zend_Cache::CLEANING_MODE_ALL:
  173. case Zend_Cache::CLEANING_MODE_OLD:
  174. $cache_dir = ini_get('zend_accelerator.output_cache_dir');
  175. if (!$cache_dir) {
  176. return false;
  177. }
  178. $cache_dir .= '/.php_cache_api/';
  179. return $this->_clean($cache_dir, $mode);
  180. break;
  181. case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  182. $idlist = null;
  183. foreach ($tags as $tag) {
  184. $next_idlist = output_cache_get(self::TAGS_PREFIX.$tag, $this->_directives['lifetime']);
  185. if ($idlist) {
  186. $idlist = array_intersect_assoc($idlist, $next_idlist);
  187. } else {
  188. $idlist = $next_idlist;
  189. }
  190. if (count($idlist) == 0) {
  191. // if ID list is already empty - we may skip checking other IDs
  192. $idlist = null;
  193. break;
  194. }
  195. }
  196. if ($idlist) {
  197. foreach ($idlist as $id) {
  198. output_cache_remove_key($id);
  199. }
  200. }
  201. return true;
  202. break;
  203. case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  204. $this->_log("Zend_Cache_Backend_ZendPlatform::clean() : CLEANING_MODE_NOT_MATCHING_TAG is not supported by the Zend Platform backend");
  205. return false;
  206. break;
  207. case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  208. $idlist = null;
  209. foreach ($tags as $tag) {
  210. $next_idlist = output_cache_get(self::TAGS_PREFIX.$tag, $this->_directives['lifetime']);
  211. if ($idlist) {
  212. $idlist = array_merge_recursive($idlist, $next_idlist);
  213. } else {
  214. $idlist = $next_idlist;
  215. }
  216. if (count($idlist) == 0) {
  217. // if ID list is already empty - we may skip checking other IDs
  218. $idlist = null;
  219. break;
  220. }
  221. }
  222. if ($idlist) {
  223. foreach ($idlist as $id) {
  224. output_cache_remove_key($id);
  225. }
  226. }
  227. return true;
  228. break;
  229. default:
  230. Zend_Cache::throwException('Invalid mode for clean() method');
  231. break;
  232. }
  233. }
  234. /**
  235. * Clean a directory and recursivly go over it's subdirectories
  236. *
  237. * Remove all the cached files that need to be cleaned (according to mode and files mtime)
  238. *
  239. * @param string $dir Path of directory ot clean
  240. * @param string $mode The same parameter as in Zend_Cache_Backend_ZendPlatform::clean()
  241. * @return boolean True if ok
  242. */
  243. private function _clean($dir, $mode)
  244. {
  245. $d = @dir($dir);
  246. if (!$d) {
  247. return false;
  248. }
  249. $result = true;
  250. while (false !== ($file = $d->read())) {
  251. if ($file == '.' || $file == '..') {
  252. continue;
  253. }
  254. $file = $d->path . $file;
  255. if (is_dir($file)) {
  256. $result = ($this->_clean($file .'/', $mode)) && ($result);
  257. } else {
  258. if ($mode == Zend_Cache::CLEANING_MODE_ALL) {
  259. $result = ($this->_remove($file)) && ($result);
  260. } else if ($mode == Zend_Cache::CLEANING_MODE_OLD) {
  261. // Files older than lifetime get deleted from cache
  262. if ($this->_directives['lifetime'] !== null) {
  263. if ((time() - @filemtime($file)) > $this->_directives['lifetime']) {
  264. $result = ($this->_remove($file)) && ($result);
  265. }
  266. }
  267. }
  268. }
  269. }
  270. $d->close();
  271. return $result;
  272. }
  273. /**
  274. * Remove a file
  275. *
  276. * If we can't remove the file (because of locks or any problem), we will touch
  277. * the file to invalidate it
  278. *
  279. * @param string $file Complete file path
  280. * @return boolean True if ok
  281. */
  282. private function _remove($file)
  283. {
  284. if (!@unlink($file)) {
  285. # If we can't remove the file (because of locks or any problem), we will touch
  286. # the file to invalidate it
  287. $this->_log("Zend_Cache_Backend_ZendPlatform::_remove() : we can't remove $file => we are going to try to invalidate it");
  288. if ($this->_directives['lifetime'] === null) {
  289. return false;
  290. }
  291. if (!file_exists($file)) {
  292. return false;
  293. }
  294. return @touch($file, time() - 2*abs($this->_directives['lifetime']));
  295. }
  296. return true;
  297. }
  298. }