2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
40 * Load require libraries
44 require 'include/utils/external_cache/SugarCache.php';
45 require 'include/utils/external_cache/SugarCache_Base.php';
46 require 'include/utils/external_cache/SugarCache_ExternalAbstract.php';
47 require 'include/utils/external_cache/SugarCache_APC.php';
48 require 'include/utils/external_cache/SugarCache_Memcache.php';
49 require 'include/utils/external_cache/SugarCache_Zend.php';
50 require 'include/utils/external_cache/SugarCache_ZendServer.php';
51 require 'include/utils/external_cache/SugarCache_sMash.php';
52 require 'include/utils/external_cache/SugarCache_Wincache.php';
56 * Internal -- Has the external cache been checked to determine if it is available and configured.
58 $GLOBALS['external_cache_checked'] = false;
61 * Internal -- Is the external cache available. This setting is determined by checking for the availability
62 * of the external cache functions. It can be overridden by adding a config variable
63 * (external_cache_disabled=true).
65 $GLOBALS['external_cache_enabled'] = false;
68 * Internal -- This is controlled by a config setting (external_cache_reset) that will update the cache
69 * with new values, but not read from the cache.
71 $GLOBALS['external_cache_overwrite'] = false;
74 * Internal -- The number of hits on the external out of process cache in this request
76 $GLOBALS['external_cache_request_external_hits'] = 0;
79 * Internal -- The number of total requests to the external out of process cache in this request
81 $GLOBALS['external_cache_request_external_total'] = 0;
84 * Internal -- The number of hits on the external local cache in this request
86 $GLOBALS['external_cache_request_local_hits'] = 0;
89 * Internal -- The number of total requests to the external local cache in this request
91 $GLOBALS['external_cache_request_local_total'] = 0;
94 * Internal -- The data structure for the local cache.
96 $GLOBALS['cache_local_store'] = array();
99 * Internal -- The type of external store available.
101 $GLOBALS['external_cache_type'] = null;
104 * The interval in seconds that an external cache entry is valid.
106 define('EXTERNAL_CACHE_INTERVAL_SECONDS', 300 );
109 * This constant is provided as a convenience for users that want to store null values
110 * in the cache. If your function frequently has null results that take a long time to
111 * calculate, store those results in the cache. On retrieval, substitue the value you
114 define('EXTERNAL_CACHE_NULL_VALUE', "SUGAR_CACHE_NULL_ZZ");
117 * Set this to true to see cache debugging messages in the end user UI.
118 * This is a quick way to determine how well the cache is working and find out what
119 * records are not being cached effectively.
121 define('EXTERNAL_CACHE_DEBUG', false);
124 * Set this to true to validate that all items stored in the external cache are
125 * identical when they are retrieved. This forces an immediate retrieve after each
126 * store call to make sure the contents are reproduced exactly.
128 define('EXTENRAL_CACHE_VALIDATE_STORES', false);
131 * This key is used to write to the external cache to validate that it is indeed working.
132 * This prevents non-functioning caches from being detected as functional.
134 define('EXTERNAL_CACHE_WORKING_CHECK_KEY', 'EXTERNAL_CACHE_WORKING_CHECK_KEY');
137 * Internal -- Determine if there is an external cache available for use.
138 * Currently only Zend Platform is supported.
140 function check_cache()
142 if(EXTERNAL_CACHE_DEBUG) SugarCache::log("Checking cache");
144 if($GLOBALS['external_cache_checked'] == false)
146 $GLOBALS['external_cache_checked'] = true;
147 $GLOBALS['external_cache_object'] = SugarCache::discover();
150 if(EXTERNAL_CACHE_DEBUG) SugarCache::log("Checking cache: " . var_export($GLOBALS['external_cache_enabled'], true));
154 * This function is called once an external cache has been identified to ensure that it is correctly
156 * @return true for success, false for failure.
158 function sugar_cache_validate()
160 if($GLOBALS['external_cache_checked'] == false) {
163 // should never be called
164 $GLOBALS['external_cache_object']->init();
165 return $GLOBALS['external_cache_object']->initialized;
169 * Retrieve a key from cache. For the Zend Platform, a maximum age of 5 minutes is assumed.
171 * @param String $key -- The item to retrieve.
172 * @return The item unserialized
174 function sugar_cache_retrieve($key)
176 if($GLOBALS['external_cache_checked'] == false) {
179 return $GLOBALS['external_cache_object']->get($key);
183 * Internal -- This function actually retrieves information from the caches.
184 * It is a helper function that provides that actual cache API abstraction.
186 * @param unknown_type $key
189 * @see sugar_cache_retrieve
191 function external_cache_retrieve_helper($key)
193 if($GLOBALS['external_cache_checked'] == false) {
196 return sugar_cache_retrieve($key);
200 * Put a value in the cache under a key
202 * @param String $key -- Global namespace cache. Key for the data.
203 * @param Serializable $value -- The value to store in the cache.
205 function sugar_cache_put($key, $value)
207 if($GLOBALS['external_cache_checked'] == false) {
210 $GLOBALS['external_cache_object']->set($key, $value);
214 * Clear a key from the cache. This is used to invalidate a single key.
216 * @param String $key -- Key from global namespace
218 function sugar_cache_clear($key)
220 if($GLOBALS['external_cache_checked'] == false) {
223 $GLOBALS['external_cache_object']->__unset($key);
227 * Turn off external caching for the rest of this round trip and for all round
228 * trips for the next cache timeout. This function should be called when global arrays
229 * are affected (studio, module loader, upgrade wizard, ... ) and it is not ok to
230 * wait for the cache to expire in order to see the change.
232 function sugar_cache_reset()
234 //@todo implement this in new code
235 // Set a flag to clear the code.
236 sugar_cache_put('EXTERNAL_CACHE_RESET', true);
238 // Clear the local cache
239 $GLOBALS['cache_local_store'] = array();
241 // Disable the external cache for the rest of the round trip
242 $GLOBALS['external_cache_enabled'] = false;
244 sugar_clean_opcodes();
248 * Reset opcode cache if present
250 function sugar_clean_opcodes()
252 if($GLOBALS['external_cache_checked'] == false) {
256 if($GLOBALS['external_cache_object']) {
257 $GLOBALS['external_cache_object']->clean_opcodes();