Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * Cache is an empty base object, we'll modify the scheme when we have different caching schemes | |
6a488035 TO |
36 | */ |
37 | class CRM_Utils_Cache { | |
38 | /** | |
64c2ecd4 TO |
39 | * (Quasi-Private) Treat this as private. It is marked public to facilitate testing. |
40 | * | |
6a488035 TO |
41 | * We only need one instance of this object. So we use the singleton |
42 | * pattern and cache the instance in this variable | |
43 | * | |
44 | * @var object | |
6a488035 | 45 | */ |
64c2ecd4 | 46 | public static $_singleton = NULL; |
6a488035 TO |
47 | |
48 | /** | |
fe482240 | 49 | * Constructor. |
6a488035 | 50 | * |
77855840 TO |
51 | * @param array $config |
52 | * An array of configuration params. | |
6a488035 | 53 | * |
f4aaa82a | 54 | * @return \CRM_Utils_Cache |
6a488035 | 55 | */ |
00be9182 | 56 | public function __construct(&$config) { |
6a488035 TO |
57 | CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly')); |
58 | } | |
59 | ||
60 | /** | |
fe482240 | 61 | * Singleton function used to manage this object. |
6a488035 | 62 | * |
c039f658 | 63 | * @return CRM_Utils_Cache_Interface |
6a488035 | 64 | */ |
00be9182 | 65 | public static function &singleton() { |
6a488035 TO |
66 | if (self::$_singleton === NULL) { |
67 | $className = 'ArrayCache'; // default to ArrayCache for now | |
68 | ||
69 | // Maintain backward compatibility for now. | |
70 | // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will | |
71 | // override the CIVICRM_DB_CACHE_CLASS setting. | |
72 | // Going forward, CIVICRM_USE_xxxCACHE should be deprecated. | |
73 | if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE) { | |
74 | $className = 'Memcache'; | |
ee1d325f | 75 | } |
4c9b6178 | 76 | elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) { |
6a488035 | 77 | $className = 'ArrayCache'; |
ee1d325f | 78 | } |
4c9b6178 | 79 | elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) { |
6a488035 TO |
80 | $className = CIVICRM_DB_CACHE_CLASS; |
81 | } | |
82 | ||
83 | // a generic method for utilizing any of the available db caches. | |
84 | $dbCacheClass = 'CRM_Utils_Cache_' . $className; | |
6a488035 TO |
85 | $settings = self::getCacheSettings($className); |
86 | self::$_singleton = new $dbCacheClass($settings); | |
ee1d325f | 87 | } |
6a488035 TO |
88 | return self::$_singleton; |
89 | } | |
90 | ||
91 | /** | |
fe482240 | 92 | * Get cache relevant settings. |
6a488035 | 93 | * |
77b97be7 EM |
94 | * @param $cachePlugin |
95 | * | |
6a488035 TO |
96 | * @return array |
97 | * associative array of settings for the cache | |
6a488035 | 98 | */ |
00be9182 | 99 | public static function getCacheSettings($cachePlugin) { |
6a488035 TO |
100 | switch ($cachePlugin) { |
101 | case 'ArrayCache': | |
102 | case 'NoCache': | |
103 | $defaults = array(); | |
104 | break; | |
838fc623 | 105 | |
59e56021 | 106 | case 'Redis': |
6a488035 TO |
107 | case 'Memcache': |
108 | case 'Memcached': | |
109 | $defaults = array( | |
110 | 'host' => 'localhost', | |
111 | 'port' => 11211, | |
112 | 'timeout' => 3600, | |
113 | 'prefix' => '', | |
114 | ); | |
115 | ||
b44e3f84 | 116 | // Use old constants if needed to ensure backward compatibility |
6a488035 TO |
117 | if (defined('CIVICRM_MEMCACHE_HOST')) { |
118 | $defaults['host'] = CIVICRM_MEMCACHE_HOST; | |
119 | } | |
120 | ||
121 | if (defined('CIVICRM_MEMCACHE_PORT')) { | |
122 | $defaults['port'] = CIVICRM_MEMCACHE_PORT; | |
123 | } | |
124 | ||
125 | if (defined('CIVICRM_MEMCACHE_TIMEOUT')) { | |
126 | $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT; | |
127 | } | |
128 | ||
129 | if (defined('CIVICRM_MEMCACHE_PREFIX')) { | |
130 | $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX; | |
131 | } | |
132 | ||
133 | // Use new constants if possible | |
134 | if (defined('CIVICRM_DB_CACHE_HOST')) { | |
135 | $defaults['host'] = CIVICRM_DB_CACHE_HOST; | |
136 | } | |
137 | ||
138 | if (defined('CIVICRM_DB_CACHE_PORT')) { | |
139 | $defaults['port'] = CIVICRM_DB_CACHE_PORT; | |
140 | } | |
141 | ||
142 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { | |
143 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; | |
144 | } | |
145 | ||
146 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { | |
147 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; | |
148 | } | |
149 | ||
150 | break; | |
151 | ||
152 | case 'APCcache': | |
153 | $defaults = array(); | |
154 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { | |
155 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; | |
156 | } | |
157 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { | |
158 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; | |
159 | } | |
160 | break; | |
161 | } | |
162 | return $defaults; | |
163 | } | |
96025800 | 164 | |
a4704404 TO |
165 | /** |
166 | * Create a new, named, limited-use cache. | |
167 | * | |
168 | * This is a factory function. Generally, you should use Civi::cache($name) | |
169 | * to locate managed cached instance. | |
170 | * | |
171 | * @param array $params | |
172 | * Array with keys: | |
173 | * - name: string, unique symbolic name. | |
174 | * - type: array|string, list of acceptable cache types, in order of preference. | |
175 | * - prefetch: bool, whether to prefetch all data in cache (if possible). | |
176 | * @return CRM_Utils_Cache_Interface | |
177 | * @throws CRM_Core_Exception | |
178 | * @see Civi::cache() | |
179 | */ | |
180 | public static function create($params = array()) { | |
181 | $types = (array) $params['type']; | |
182 | ||
183 | foreach ($types as $type) { | |
184 | switch ($type) { | |
a944a143 | 185 | case '*memory*': |
eac3023b | 186 | if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, array('Memcache', 'Memcached', 'Redis'))) { |
a944a143 TO |
187 | $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS; |
188 | $settings = self::getCacheSettings(CIVICRM_DB_CACHE_CLASS); | |
189 | $settings['prefix'] = $settings['prefix'] . '_' . $params['name']; | |
190 | return new $dbCacheClass($settings); | |
191 | } | |
192 | break; | |
193 | ||
a4704404 TO |
194 | case 'SqlGroup': |
195 | if (defined('CIVICRM_DSN') && CIVICRM_DSN) { | |
196 | return new CRM_Utils_Cache_SqlGroup(array( | |
197 | 'group' => $params['name'], | |
198 | 'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE), | |
199 | )); | |
200 | } | |
201 | break; | |
202 | ||
203 | case 'Arraycache': | |
204 | case 'ArrayCache': | |
205 | return new CRM_Utils_Cache_ArrayCache(array()); | |
206 | ||
207 | } | |
208 | } | |
209 | ||
210 | throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1)); | |
211 | } | |
212 | ||
6a488035 | 213 | } |