]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/utils/external_cache.php
Release 6.1.4
[Github/sugarcrm.git] / include / utils / external_cache.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 /**##@+
40  * Load require libraries
41  *
42  * @ignore
43  */
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';
53 /**##@- */
54
55 /**
56  * Internal -- Has the external cache been checked to determine if it is available and configured.
57  */
58 $GLOBALS['external_cache_checked'] = false;
59
60 /**
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).
64  */
65 $GLOBALS['external_cache_enabled'] = false;
66
67 /**
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.
70  */
71 $GLOBALS['external_cache_overwrite'] = false;
72
73 /**
74  * Internal -- The number of hits on the external out of process cache in this request
75  */
76 $GLOBALS['external_cache_request_external_hits'] = 0;
77
78 /**
79  * Internal -- The number of total requests to the external out of process cache in this request
80  */
81 $GLOBALS['external_cache_request_external_total'] = 0;
82
83 /**
84  * Internal -- The number of hits on the external local cache in this request
85  */
86 $GLOBALS['external_cache_request_local_hits'] = 0;
87
88 /**
89  * Internal -- The number of total requests to the external local cache in this request
90  */
91 $GLOBALS['external_cache_request_local_total'] = 0;
92
93 /**
94  * Internal -- The data structure for the local cache.
95  */
96 $GLOBALS['cache_local_store'] = array();
97
98 /**
99  * Internal -- The type of external store available.
100  */
101 $GLOBALS['external_cache_type'] = null;
102
103 /**
104  * The interval in seconds that an external cache entry is valid.
105  */
106 define('EXTERNAL_CACHE_INTERVAL_SECONDS', 300 );
107
108 /**
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
112  * stored for null.
113  */
114 define('EXTERNAL_CACHE_NULL_VALUE', "SUGAR_CACHE_NULL_ZZ");
115
116 /**
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.
120  */
121 define('EXTERNAL_CACHE_DEBUG', false);
122
123 /**
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.
127  */
128 define('EXTENRAL_CACHE_VALIDATE_STORES', false);
129
130 /**
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.
133  */
134 define('EXTERNAL_CACHE_WORKING_CHECK_KEY', 'EXTERNAL_CACHE_WORKING_CHECK_KEY');
135
136 /**
137  * Internal -- Determine if there is an external cache available for use.
138  * Currently only Zend Platform is supported.
139  */
140 function check_cache()
141 {
142     if(EXTERNAL_CACHE_DEBUG) SugarCache::log("Checking cache");
143
144     if($GLOBALS['external_cache_checked'] == false)
145     {
146         $GLOBALS['external_cache_checked'] = true;
147         $GLOBALS['external_cache_object'] = SugarCache::discover();
148     }
149
150     if(EXTERNAL_CACHE_DEBUG) SugarCache::log("Checking cache: " . var_export($GLOBALS['external_cache_enabled'], true));
151 }
152
153 /**
154  * This function is called once an external cache has been identified to ensure that it is correctly
155  * working.
156  * @return true for success, false for failure.
157  */
158 function sugar_cache_validate()
159 {
160     if($GLOBALS['external_cache_checked'] == false) {
161         check_cache();
162     }
163     // should never be called
164     $GLOBALS['external_cache_object']->init();
165     return $GLOBALS['external_cache_object']->initialized;
166 }
167
168 /**
169  * Retrieve a key from cache.  For the Zend Platform, a maximum age of 5 minutes is assumed.
170  *
171  * @param String $key -- The item to retrieve.
172  * @return The item unserialized
173  */
174 function sugar_cache_retrieve($key)
175 {
176     if($GLOBALS['external_cache_checked'] == false) {
177         check_cache();
178     }
179     return $GLOBALS['external_cache_object']->get($key);
180 }
181
182 /**
183  * Internal -- This function actually retrieves information from the caches.
184  * It is a helper function that provides that actual cache API abstraction.
185  *
186  * @param unknown_type $key
187  * @return unknown
188  * @deprecated
189  * @see sugar_cache_retrieve
190  */
191 function external_cache_retrieve_helper($key)
192 {
193     if($GLOBALS['external_cache_checked'] == false) {
194         check_cache();
195     }
196     return sugar_cache_retrieve($key);
197 }
198
199 /**
200  * Put a value in the cache under a key
201  *
202  * @param String $key -- Global namespace cache.  Key for the data.
203  * @param Serializable $value -- The value to store in the cache.
204  */
205 function sugar_cache_put($key, $value)
206 {
207     if($GLOBALS['external_cache_checked'] == false) {
208         check_cache();
209     }
210     $GLOBALS['external_cache_object']->set($key, $value);
211 }
212
213 /**
214  * Clear a key from the cache.  This is used to invalidate a single key.
215  *
216  * @param String $key -- Key from global namespace
217  */
218 function sugar_cache_clear($key)
219 {
220     if($GLOBALS['external_cache_checked'] == false) {
221         check_cache();
222     }
223     $GLOBALS['external_cache_object']->__unset($key);
224 }
225
226 /**
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.
231  */
232 function sugar_cache_reset()
233 {
234     //@todo implement this in new code
235     // Set a flag to clear the code.
236     sugar_cache_put('EXTERNAL_CACHE_RESET', true);
237
238     // Clear the local cache
239     $GLOBALS['cache_local_store'] = array();
240
241     // Disable the external cache for the rest of the round trip
242     $GLOBALS['external_cache_enabled'] = false;
243
244     sugar_clean_opcodes();
245 }
246
247 /**
248  * Reset opcode cache if present
249  */
250 function sugar_clean_opcodes()
251 {
252     if($GLOBALS['external_cache_checked'] == false) {
253         check_cache();
254     }
255
256     if($GLOBALS['external_cache_object']) {
257         $GLOBALS['external_cache_object']->clean_opcodes();
258     }
259 }