]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/utils/external_cache/SugarCache_ExternalAbstract.php
Add .gitignore
[Github/sugarcrm.git] / include / utils / external_cache / SugarCache_ExternalAbstract.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  * @abstract
41  */
42 class SugarCache_ExternalAbstract extends SugarCache_Base
43 {
44     /**
45      * Use the parent of object to attempt to retrieve cache?  i.e., use local
46      * memory cache.
47      *
48      * @var bool
49      * @access protected
50      */
51     var $_use_parent = true;
52     
53     /**
54      * An internal value that can be used to adjust the length of a timeout.
55      *
56      * If not set prior to calling {@link init()}, this will default to the constant
57      * EXTERNAL_CACHE_INTERVAL_SECONDS
58      *
59      * @var int
60      */
61     var $timeout = null;
62     
63     /**
64      * Stores the cache name
65      * @access private
66      */
67     var $_name = '';
68
69     /**
70      * Serves to initialize this cache
71      */
72     function init()
73     {
74         if (empty($this->_timeout)) {
75             $this->timeout = EXTERNAL_CACHE_INTERVAL_SECONDS;
76         }
77         
78         $this->_use_parent = false;
79         
80         $value = $this->get(EXTERNAL_CACHE_WORKING_CHECK_KEY);
81         if ($value != EXTERNAL_CACHE_WORKING_CHECK_KEY) {
82             $this->set(
83                 EXTERNAL_CACHE_WORKING_CHECK_KEY,
84                 EXTERNAL_CACHE_WORKING_CHECK_KEY
85             );
86             $value = $this->get(EXTERNAL_CACHE_WORKING_CHECK_KEY);
87             
88             // Clear the cache statistics after the test.  This makes the statistics work out.
89             $GLOBALS['external_cache_request_external_hits'] = 0;
90             $GLOBALS['external_cache_request_external_total'] = 0;
91         }
92         
93         $this->_use_parent = true;
94         $this->initialized = (EXTERNAL_CACHE_WORKING_CHECK_KEY == $value);
95         
96         if (empty($this->_name)) {
97             $this->_name = substr(get_class($this), 11);
98         }
99     }
100
101     function get($key)
102     {
103         if ($this->_use_parent && !is_null($value = parent::get($key))) {
104             if (EXTERNAL_CACHE_DEBUG) {
105                 SugarCache::log("{$this->_name}:: found {$key} in local memory cache");
106             }
107             return $value;
108         }
109
110         if(!$GLOBALS['external_cache_enabled']) {
111             if (EXTERNAL_CACHE_DEBUG) {
112                 SugarCache::log("{$this->_name}:: caching disabled", 'fail');
113             }
114             return null;
115         }
116
117         $GLOBALS['external_cache_request_external_total']++;
118
119         if(EXTERNAL_CACHE_DEBUG) {
120             SugarCache::log("{$this->_name}:: retrieving key from cache ($key)");
121         }
122
123         return null;
124     }
125
126     function _realKey($key)
127     {
128         return $GLOBALS['sugar_config']['unique_key'] . $key;
129     }
130
131     function _processGet($key, $value)
132     {
133         if (!empty($value)) {
134             if (EXTERNAL_CACHE_DEBUG) {
135                 SugarCache::log("{$this->_name}:: Retrieved from external cache: {$key}", 'pass');
136             }
137             $GLOBALS['external_cache_request_external_hits']++;
138             $this->_cache[$key] = $value;
139             return $this->_cache[$key];
140         }
141         if(EXTERNAL_CACHE_DEBUG) {
142             SugarCache::log("{$this->_name}:: External cache retrieve failed: $key", 'fail');
143         }
144         return null;
145     }
146 }