PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/include/SugarCache/SugarCache.php

https://gitlab.com/tjaafar/SuiteCRM
PHP | 252 lines | 111 code | 21 blank | 120 comment | 26 complexity | 59d9d2c2ec7b7bc8a6cbaa5df4ab99c2 MD5 | raw file
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  5. * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
  6. * Copyright (C) 2011 - 2014 Salesagility Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU Affero General Public License version 3 as published by the
  10. * Free Software Foundation with the addition of the following permission added
  11. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  12. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  13. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License along with
  21. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  22. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301 USA.
  24. *
  25. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  26. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  27. *
  28. * The interactive user interfaces in modified source and object code versions
  29. * of this program must display Appropriate Legal Notices, as required under
  30. * Section 5 of the GNU Affero General Public License version 3.
  31. *
  32. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  33. * these Appropriate Legal Notices must retain the display of the "Powered by
  34. * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
  35. * reasonably feasible for technical reasons, the Appropriate Legal Notices must
  36. * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
  37. ********************************************************************************/
  38. /**
  39. * Sugar Cache manager
  40. * @api
  41. */
  42. class SugarCache
  43. {
  44. const EXTERNAL_CACHE_NULL_VALUE = "SUGAR_CACHE_NULL_ZZ";
  45. protected static $_cacheInstance;
  46. /**
  47. * @var true if the cache has been reset during this request, so we no longer return values from
  48. * cache until the next reset
  49. */
  50. public static $isCacheReset = false;
  51. private function __construct() {}
  52. /**
  53. * initializes the cache in question
  54. */
  55. protected static function _init()
  56. {
  57. $lastPriority = 1000;
  58. $locations = array('include/SugarCache','custom/include/SugarCache');
  59. foreach ( $locations as $location ) {
  60. if (sugar_is_dir($location) && $dir = opendir($location)) {
  61. while (($file = readdir($dir)) !== false) {
  62. if ($file == ".."
  63. || $file == "."
  64. || !is_file("$location/$file")
  65. )
  66. continue;
  67. require_once("$location/$file");
  68. $cacheClass = basename($file, ".php");
  69. if ( class_exists($cacheClass) && is_subclass_of($cacheClass,'SugarCacheAbstract') ) {
  70. $GLOBALS['log']->debug("Found cache backend $cacheClass");
  71. $cacheInstance = new $cacheClass();
  72. if ( $cacheInstance->useBackend()
  73. && $cacheInstance->getPriority() < $lastPriority ) {
  74. $GLOBALS['log']->debug("Using cache backend $cacheClass, since ".$cacheInstance->getPriority()." is less than ".$lastPriority);
  75. self::$_cacheInstance = $cacheInstance;
  76. $lastPriority = $cacheInstance->getPriority();
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Returns the instance of the SugarCacheAbstract object, cooresponding to the external
  85. * cache being used.
  86. */
  87. public static function instance()
  88. {
  89. if ( !is_subclass_of(self::$_cacheInstance,'SugarCacheAbstract') )
  90. self::_init();
  91. return self::$_cacheInstance;
  92. }
  93. /**
  94. * Try to reset any opcode caches we know about
  95. *
  96. * @todo make it so developers can extend this somehow
  97. */
  98. public static function cleanOpcodes()
  99. {
  100. // APC
  101. if ( function_exists('apc_clear_cache') && ini_get('apc.stat') == 0 ) {
  102. apc_clear_cache();
  103. }
  104. // Wincache
  105. if ( function_exists('wincache_refresh_if_changed') ) {
  106. wincache_refresh_if_changed();
  107. }
  108. // Zend
  109. if ( function_exists('accelerator_reset') ) {
  110. accelerator_reset();
  111. }
  112. // eAccelerator
  113. if ( function_exists('eaccelerator_clear') ) {
  114. eaccelerator_clear();
  115. }
  116. // XCache
  117. if ( function_exists('xcache_clear_cache') && !ini_get('xcache.admin.enable_auth') ) {
  118. $max = xcache_count(XC_TYPE_PHP);
  119. for ($i = 0; $i < $max; $i++) {
  120. if (!xcache_clear_cache(XC_TYPE_PHP, $i)) {
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Try to reset file from caches
  128. */
  129. public static function cleanFile( $file )
  130. {
  131. // APC
  132. if ( function_exists('apc_delete_file') && ini_get('apc.stat') == 0 )
  133. {
  134. apc_delete_file( $file );
  135. }
  136. }
  137. }
  138. /**
  139. * Procedural API for external cache
  140. */
  141. /**
  142. * Retrieve a key from cache. For the Zend Platform, a maximum age of 5 minutes is assumed.
  143. *
  144. * @param String $key -- The item to retrieve.
  145. * @return The item unserialized
  146. */
  147. function sugar_cache_retrieve($key)
  148. {
  149. return SugarCache::instance()->$key;
  150. }
  151. /**
  152. * Put a value in the cache under a key
  153. *
  154. * @param String $key -- Global namespace cache. Key for the data.
  155. * @param Serializable $value -- The value to store in the cache.
  156. */
  157. function sugar_cache_put($key, $value, $ttl = null)
  158. {
  159. SugarCache::instance()->set($key,$value, $ttl);
  160. }
  161. /**
  162. * Clear a key from the cache. This is used to invalidate a single key.
  163. *
  164. * @param String $key -- Key from global namespace
  165. */
  166. function sugar_cache_clear($key)
  167. {
  168. unset(SugarCache::instance()->$key);
  169. }
  170. /**
  171. * Turn off external caching for the rest of this round trip and for all round
  172. * trips for the next cache timeout. This function should be called when global arrays
  173. * are affected (studio, module loader, upgrade wizard, ... ) and it is not ok to
  174. * wait for the cache to expire in order to see the change.
  175. */
  176. function sugar_cache_reset()
  177. {
  178. SugarCache::instance()->reset();
  179. SugarCache::cleanOpcodes();
  180. }
  181. /**
  182. * Flush the cache in its entirety including the local and external store along with the opcodes.
  183. */
  184. function sugar_cache_reset_full()
  185. {
  186. SugarCache::instance()->resetFull();
  187. SugarCache::cleanOpcodes();
  188. }
  189. /**
  190. * Clean out whatever opcode cache we may have out there.
  191. */
  192. function sugar_clean_opcodes()
  193. {
  194. SugarCache::cleanOpcodes();
  195. }
  196. /**
  197. * Internal -- Determine if there is an external cache available for use.
  198. *
  199. * @deprecated
  200. */
  201. function check_cache()
  202. {
  203. SugarCache::instance();
  204. }
  205. /**
  206. * This function is called once an external cache has been identified to ensure that it is correctly
  207. * working.
  208. *
  209. * @deprecated
  210. *
  211. * @return true for success, false for failure.
  212. */
  213. function sugar_cache_validate()
  214. {
  215. $instance = SugarCache::instance();
  216. return is_object($instance);
  217. }
  218. /**
  219. * Internal -- This function actually retrieves information from the caches.
  220. * It is a helper function that provides that actual cache API abstraction.
  221. *
  222. * @param unknown_type $key
  223. * @return unknown
  224. * @deprecated
  225. * @see sugar_cache_retrieve
  226. */
  227. function external_cache_retrieve_helper($key)
  228. {
  229. return SugarCache::instance()->$key;
  230. }