apc-emulation.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_ttl();
  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. function apc_emul_fetch_unit($sKey)
  86. {
  87. // Try the 'TTLed' version
  88. $sValue = apc_emul_readcache_locked(apc_emul_get_cache_filename('-'.$sKey));
  89. if ($sValue === false)
  90. {
  91. $sValue = apc_emul_readcache_locked(apc_emul_get_cache_filename($sKey));
  92. if ($sValue === false)
  93. {
  94. return false;
  95. }
  96. }
  97. $oRes = @unserialize($sValue);
  98. return $oRes;
  99. }
  100. function apc_emul_readcache_locked($sFilename)
  101. {
  102. $file = @fopen($sFilename, 'r');
  103. if ($file === false)
  104. {
  105. return false;
  106. }
  107. flock($file, LOCK_SH);
  108. $sContent = @fread($file, @filesize($sFilename));
  109. flock($file, LOCK_UN);
  110. fclose($file);
  111. return $sContent;
  112. }
  113. /**
  114. * @param string $cache_type
  115. * @return bool
  116. */
  117. function apc_clear_cache($cache_type = '')
  118. {
  119. $sRootCacheDir = apc_emul_get_cache_filename('');
  120. apc_emul_delete_entry($sRootCacheDir);
  121. return true;
  122. }
  123. function apc_emul_delete_entry($sCache)
  124. {
  125. if (is_dir($sCache))
  126. {
  127. $aFiles = array_diff(scandir($sCache), array('.', '..'));
  128. foreach($aFiles as $sFile)
  129. {
  130. $sSubFile = $sCache.'/'.$sFile;
  131. if (!apc_emul_delete_entry($sSubFile))
  132. {
  133. return false;
  134. }
  135. }
  136. if (!@rmdir($sCache))
  137. {
  138. return false;
  139. }
  140. }
  141. else
  142. {
  143. if (!@unlink($sCache))
  144. {
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. /**
  151. * @param $key
  152. * @return bool|string[]
  153. */
  154. function apc_delete($key)
  155. {
  156. return apc_emul_delete_entry(apc_emul_get_cache_filename($key));
  157. }
  158. function apc_emul_get_cache_filename($sKey)
  159. {
  160. $sPath = str_replace(array(' ', '/', '\\', '.'), '-', $sKey);
  161. return utils::GetCachePath().'apc-emul/'.$sPath;
  162. }
  163. function apc_emul_manage_ttl()
  164. {
  165. // Check only once per request
  166. static $bAlreadyChecked = false;
  167. if ($bAlreadyChecked)
  168. {
  169. return;
  170. }
  171. $sRootCacheDir = apc_emul_get_cache_filename('');
  172. apc_emul_manage_ttl_dir($sRootCacheDir);
  173. $bAlreadyChecked = true;
  174. }
  175. function apc_emul_manage_ttl_dir($sCheck)
  176. {
  177. $iTTL = MetaModel::GetConfig()->Get('apc_cache.query_ttl');
  178. // Garbage collection
  179. $aFiles = array_diff(@scandir($sCheck), array('.', '..'));
  180. foreach($aFiles as $sFile)
  181. {
  182. $sSubFile = $sCheck.'/'.$sFile;
  183. if (is_dir($sSubFile))
  184. {
  185. apc_emul_manage_ttl_dir($sSubFile);
  186. }
  187. else
  188. {
  189. apc_emul_check_ttl_file($sSubFile, $iTTL);
  190. }
  191. }
  192. }
  193. function apc_emul_check_ttl_file($sFilename, $iTTL)
  194. {
  195. $iCurTime = time();
  196. if (strpos(basename($sFilename), '-') === 0)
  197. {
  198. $iLimitTime = @fileatime($sFilename) + $iTTL + rand(-$iTTL / 10, $iTTL / 10);
  199. if ($iLimitTime < $iCurTime)
  200. {
  201. @unlink($sFilename);
  202. }
  203. }
  204. }