Merge pull request #7892 from eileenmcnaughton/CRM-18125
[civicrm-core.git] / CRM / Utils / Cache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * Cache is an empty base object, we'll modify the scheme when we have different caching schemes
36 */
37 class CRM_Utils_Cache {
38 /**
39 * We only need one instance of this object. So we use the singleton
40 * pattern and cache the instance in this variable
41 *
42 * @var object
43 */
44 static private $_singleton = NULL;
45
46 /**
47 * Constructor.
48 *
49 * @param array $config
50 * An array of configuration params.
51 *
52 * @return \CRM_Utils_Cache
53 */
54 public function __construct(&$config) {
55 CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly'));
56 }
57
58 /**
59 * Singleton function used to manage this object.
60 *
61 * @return CRM_Utils_Cache_Interface
62 */
63 public static function &singleton() {
64 if (self::$_singleton === NULL) {
65 $className = 'ArrayCache'; // default to ArrayCache for now
66
67 // Maintain backward compatibility for now.
68 // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
69 // override the CIVICRM_DB_CACHE_CLASS setting.
70 // Going forward, CIVICRM_USE_xxxCACHE should be deprecated.
71 if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE) {
72 $className = 'Memcache';
73 }
74 elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) {
75 $className = 'ArrayCache';
76 }
77 elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) {
78 $className = CIVICRM_DB_CACHE_CLASS;
79 }
80
81 // a generic method for utilizing any of the available db caches.
82 $dbCacheClass = 'CRM_Utils_Cache_' . $className;
83 $settings = self::getCacheSettings($className);
84 self::$_singleton = new $dbCacheClass($settings);
85 }
86 return self::$_singleton;
87 }
88
89 /**
90 * Get cache relevant settings.
91 *
92 * @param $cachePlugin
93 *
94 * @return array
95 * associative array of settings for the cache
96 */
97 public static function getCacheSettings($cachePlugin) {
98 switch ($cachePlugin) {
99 case 'ArrayCache':
100 case 'NoCache':
101 $defaults = array();
102 break;
103
104 case 'Redis':
105 case 'Memcache':
106 case 'Memcached':
107 $defaults = array(
108 'host' => 'localhost',
109 'port' => 11211,
110 'timeout' => 3600,
111 'prefix' => '',
112 );
113
114 // Use old constants if needed to ensure backward compatibility
115 if (defined('CIVICRM_MEMCACHE_HOST')) {
116 $defaults['host'] = CIVICRM_MEMCACHE_HOST;
117 }
118
119 if (defined('CIVICRM_MEMCACHE_PORT')) {
120 $defaults['port'] = CIVICRM_MEMCACHE_PORT;
121 }
122
123 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
124 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT;
125 }
126
127 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
128 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX;
129 }
130
131 // Use new constants if possible
132 if (defined('CIVICRM_DB_CACHE_HOST')) {
133 $defaults['host'] = CIVICRM_DB_CACHE_HOST;
134 }
135
136 if (defined('CIVICRM_DB_CACHE_PORT')) {
137 $defaults['port'] = CIVICRM_DB_CACHE_PORT;
138 }
139
140 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
141 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
142 }
143
144 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
145 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
146 }
147
148 break;
149
150 case 'APCcache':
151 $defaults = array();
152 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
153 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
154 }
155 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
156 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
157 }
158 break;
159 }
160 return $defaults;
161 }
162
163 /**
164 * Create a new, named, limited-use cache.
165 *
166 * This is a factory function. Generally, you should use Civi::cache($name)
167 * to locate managed cached instance.
168 *
169 * @param array $params
170 * Array with keys:
171 * - name: string, unique symbolic name.
172 * - type: array|string, list of acceptable cache types, in order of preference.
173 * - prefetch: bool, whether to prefetch all data in cache (if possible).
174 * @return CRM_Utils_Cache_Interface
175 * @throws CRM_Core_Exception
176 * @see Civi::cache()
177 */
178 public static function create($params = array()) {
179 $types = (array) $params['type'];
180
181 foreach ($types as $type) {
182 switch ($type) {
183 case '*memory*':
184 if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, array('Memcache', 'Memcached', 'Redis'))) {
185 $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS;
186 $settings = self::getCacheSettings(CIVICRM_DB_CACHE_CLASS);
187 $settings['prefix'] = $settings['prefix'] . '_' . $params['name'];
188 return new $dbCacheClass($settings);
189 }
190 break;
191
192 case 'SqlGroup':
193 if (defined('CIVICRM_DSN') && CIVICRM_DSN) {
194 return new CRM_Utils_Cache_SqlGroup(array(
195 'group' => $params['name'],
196 'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE),
197 ));
198 }
199 break;
200
201 case 'Arraycache':
202 case 'ArrayCache':
203 return new CRM_Utils_Cache_ArrayCache(array());
204
205 }
206 }
207
208 throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1));
209 }
210
211 }