]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCache/SugarCache.php
Release 6.2.0
[Github/sugarcrm.git] / include / SugarCache / SugarCache.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 class SugarCache
39 {
40     const EXTERNAL_CACHE_NULL_VALUE = "SUGAR_CACHE_NULL_ZZ";
41     
42     protected static $_cacheInstance;
43     
44     /**
45      * @var true if the cache has been reset during this request, so we no longer return values from 
46      *      cache until the next reset
47      */
48     public static $isCacheReset = false;
49     
50     private function __construct() {}
51     
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     /**
85      * Returns the instance of the SugarCacheAbstract object, cooresponding to the external
86      * cache being used.
87      */
88     public static function instance()
89     {
90         if ( !is_subclass_of(self::$_cacheInstance,'SugarCacheAbstract') )
91             self::_init();
92         
93         return self::$_cacheInstance;
94     }
95 }
96
97 /**
98  * Procedural API for external cache
99  */
100
101 /**
102  * Retrieve a key from cache.  For the Zend Platform, a maximum age of 5 minutes is assumed.
103  *
104  * @param String $key -- The item to retrieve.
105  * @return The item unserialized
106  */
107 function sugar_cache_retrieve($key)
108 {
109     return SugarCache::instance()->$key;
110 }
111
112 /**
113  * Put a value in the cache under a key
114  *
115  * @param String $key -- Global namespace cache.  Key for the data.
116  * @param Serializable $value -- The value to store in the cache.
117  */
118 function sugar_cache_put($key, $value)
119 {
120     SugarCache::instance()->$key = $value;
121 }
122
123 /**
124  * Clear a key from the cache.  This is used to invalidate a single key.
125  *
126  * @param String $key -- Key from global namespace
127  */
128 function sugar_cache_clear($key)
129 {
130     unset(SugarCache::instance()->$key);
131 }
132
133 /**
134  * Turn off external caching for the rest of this round trip and for all round 
135  * trips for the next cache timeout.  This function should be called when global arrays
136  * are affected (studio, module loader, upgrade wizard, ... ) and it is not ok to 
137  * wait for the cache to expire in order to see the change.
138  */
139 function sugar_cache_reset()
140 {
141     SugarCache::instance()->reset();
142 }
143
144 /**
145  * Internal -- Determine if there is an external cache available for use.  
146  * 
147  * @deprecated
148  */
149 function check_cache()
150 {
151     SugarCache::instance();
152 }
153
154 /**
155  * This function is called once an external cache has been identified to ensure that it is correctly
156  * working.
157  * 
158  * @deprecated
159  *
160  * @return true for success, false for failure.
161  */
162 function sugar_cache_validate()
163 {
164     $instance = SugarCache::instance();
165     
166     return is_object($instance);
167 }
168
169 /**
170  * Internal -- This function actually retrieves information from the caches.
171  * It is a helper function that provides that actual cache API abstraction.
172  *
173  * @param unknown_type $key
174  * @return unknown
175  * @deprecated
176  * @see sugar_cache_retrieve
177  */
178 function external_cache_retrieve_helper($key)
179 {
180     return SugarCache::instance()->$key;
181 }