apc-emulation.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. // Copyright (c) 2010-2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. //
  19. /**
  20. * Date: 27/09/2017
  21. */
  22. /**
  23. * @param array|string $key
  24. * @param $var
  25. * @param int $ttl
  26. * @return array|bool
  27. */
  28. function apc_store($key, $var = NULL, $ttl = 0)
  29. {
  30. if (is_array($key))
  31. {
  32. $aResult = array();
  33. foreach($key as $sKey => $value)
  34. {
  35. $aResult[] = apc_emul_store_unit($sKey, $value, $ttl);
  36. }
  37. return $aResult;
  38. }
  39. return apc_emul_store_unit($key, $var, $ttl);
  40. }
  41. /**
  42. * @param string $sKey
  43. * @param $value
  44. * @param int $iTTL time to live
  45. * @return bool
  46. */
  47. function apc_emul_store_unit($sKey, $value, $iTTL)
  48. {
  49. if ($iTTL > 0)
  50. {
  51. // hint for ttl management
  52. $sKey = '-'.$sKey;
  53. }
  54. $sFilename = apc_emul_get_cache_filename($sKey);
  55. // try to create the folder
  56. $sDirname = dirname($sFilename);
  57. if (!file_exists($sDirname))
  58. {
  59. if (!@mkdir($sDirname, 0755, true))
  60. {
  61. return false;
  62. }
  63. }
  64. $bRes = !(@file_put_contents($sFilename, serialize($value), LOCK_EX) === false);
  65. apc_emul_manage_new_entry($sFilename);
  66. return $bRes;
  67. }
  68. /**
  69. * @param $key string|array
  70. * @return mixed
  71. */
  72. function apc_fetch($key)
  73. {
  74. if (is_array($key))
  75. {
  76. $aResult = array();
  77. foreach($key as $sKey)
  78. {
  79. $aResult[$sKey] = apc_emul_fetch_unit($sKey);
  80. }
  81. return $aResult;
  82. }
  83. return apc_emul_fetch_unit($key);
  84. }
  85. /**
  86. * @param $sKey
  87. * @return bool|mixed
  88. */
  89. function apc_emul_fetch_unit($sKey)
  90. {
  91. // Try the 'TTLed' version
  92. $sValue = apc_emul_readcache_locked(apc_emul_get_cache_filename('-'.$sKey));
  93. if ($sValue === false)
  94. {
  95. $sValue = apc_emul_readcache_locked(apc_emul_get_cache_filename($sKey));
  96. if ($sValue === false)
  97. {
  98. return false;
  99. }
  100. }
  101. $oRes = @unserialize($sValue);
  102. return $oRes;
  103. }
  104. function apc_emul_readcache_locked($sFilename)
  105. {
  106. $file = @fopen($sFilename, 'r');
  107. if ($file === false)
  108. {
  109. return false;
  110. }
  111. flock($file, LOCK_SH);
  112. $sContent = @fread($file, @filesize($sFilename));
  113. flock($file, LOCK_UN);
  114. fclose($file);
  115. return $sContent;
  116. }
  117. /**
  118. * @param string $cache_type
  119. * @return bool
  120. */
  121. function apc_clear_cache($cache_type = '')
  122. {
  123. $sRootCacheDir = apc_emul_get_cache_filename('');
  124. apc_emul_delete_entry($sRootCacheDir);
  125. return true;
  126. }
  127. function apc_emul_delete_entry($sCache)
  128. {
  129. if (is_dir($sCache))
  130. {
  131. $aFiles = array_diff(scandir($sCache), array('.', '..'));
  132. foreach($aFiles as $sFile)
  133. {
  134. $sSubFile = $sCache.'/'.$sFile;
  135. if (!apc_emul_delete_entry($sSubFile))
  136. {
  137. return false;
  138. }
  139. }
  140. if (!@rmdir($sCache))
  141. {
  142. return false;
  143. }
  144. }
  145. else
  146. {
  147. if (!@unlink($sCache))
  148. {
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. /**
  155. * @param $key
  156. * @return bool|string[]
  157. */
  158. function apc_delete($key)
  159. {
  160. return apc_emul_delete_entry(apc_emul_get_cache_filename($key));
  161. }
  162. function apc_emul_get_cache_filename($sKey)
  163. {
  164. $sPath = str_replace(array(' ', '/', '\\', '.'), '-', $sKey);
  165. return utils::GetCachePath().'apc-emul/'.$sPath;
  166. }
  167. /** Manage the cache files when a new cache entry is added
  168. * @param string $sNewFilename new cache file added
  169. */
  170. function apc_emul_manage_new_entry($sNewFilename)
  171. {
  172. // Check only once per request
  173. static $aFilesByTime = null;
  174. static $iFileCount = 0;
  175. $iMaxFiles = MetaModel::GetConfig()->Get('apc_cache_emulation.max_entries');
  176. if (!$aFilesByTime)
  177. {
  178. $sRootCacheDir = apc_emul_get_cache_filename('');
  179. $aFilesByTime = apc_emul_list_files_time($sRootCacheDir);
  180. $iFileCount = count($aFilesByTime);
  181. if ($iMaxFiles !== 0)
  182. {
  183. asort($aFilesByTime);
  184. }
  185. }
  186. else
  187. {
  188. $aFilesByTime[$sNewFilename] = time();
  189. $iFileCount++;
  190. }
  191. if (($iMaxFiles !== 0) && ($iFileCount > $iMaxFiles))
  192. {
  193. $iFileNbToRemove = $iFileCount - $iMaxFiles;
  194. foreach($aFilesByTime as $sFileToRemove => $iTime)
  195. {
  196. @unlink($sFileToRemove);
  197. if ($iFileNbToRemove-- === 0)
  198. {
  199. break;
  200. }
  201. }
  202. $aFilesByTime = array_slice($aFilesByTime, $iFileCount - $iMaxFiles, null, true);
  203. $iFileCount = $iMaxFiles;
  204. }
  205. }
  206. /** Get the list of files with their associated access time
  207. * @param string $sCheck Directory to scan
  208. * @param array $aFilesByTime used by recursion
  209. * @return array
  210. */
  211. function apc_emul_list_files_time($sCheck, &$aFilesByTime = array())
  212. {
  213. // Garbage collection
  214. $aFiles = array_diff(@scandir($sCheck), array('.', '..'));
  215. foreach($aFiles as $sFile)
  216. {
  217. $sSubFile = $sCheck.'/'.$sFile;
  218. if (is_dir($sSubFile))
  219. {
  220. apc_emul_list_files_time($sSubFile, $aFilesByTime);
  221. }
  222. else
  223. {
  224. $iTime = apc_emul_get_file_time($sSubFile);
  225. if ($iTime !== false)
  226. {
  227. $aFilesByTime[$sSubFile] = $iTime;
  228. }
  229. }
  230. }
  231. return $aFilesByTime;
  232. }
  233. /** Get the file access time if TTL is managed
  234. * @param string $sFilename
  235. * @return bool|int returns the file atime or false if not relevant
  236. */
  237. function apc_emul_get_file_time($sFilename)
  238. {
  239. if (strpos(basename($sFilename), '-') === 0)
  240. {
  241. return @fileatime($sFilename);
  242. }
  243. return false;
  244. }