add support for Redis as caching server
[civicrm-core.git] / CRM / Utils / Cache.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 */
37class 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
6a488035
TO
43 */
44 static private $_singleton = NULL;
45
46 /**
fe482240 47 * Constructor.
6a488035 48 *
77855840
TO
49 * @param array $config
50 * An array of configuration params.
6a488035 51 *
f4aaa82a 52 * @return \CRM_Utils_Cache
6a488035 53 */
00be9182 54 public function __construct(&$config) {
6a488035
TO
55 CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly'));
56 }
57
58 /**
fe482240 59 * Singleton function used to manage this object.
6a488035
TO
60 *
61 * @return object
6a488035 62 */
00be9182 63 public static function &singleton() {
6a488035
TO
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';
ee1d325f 73 }
4c9b6178 74 elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) {
6a488035 75 $className = 'ArrayCache';
ee1d325f 76 }
4c9b6178 77 elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) {
6a488035
TO
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;
6a488035
TO
83 $settings = self::getCacheSettings($className);
84 self::$_singleton = new $dbCacheClass($settings);
ee1d325f 85 }
6a488035
TO
86 return self::$_singleton;
87 }
88
89 /**
fe482240 90 * Get cache relevant settings.
6a488035 91 *
77b97be7
EM
92 * @param $cachePlugin
93 *
6a488035
TO
94 * @return array
95 * associative array of settings for the cache
6a488035 96 */
00be9182 97 public static function getCacheSettings($cachePlugin) {
6a488035
TO
98 switch ($cachePlugin) {
99 case 'ArrayCache':
100 case 'NoCache':
101 $defaults = array();
102 break;
59e56021 103 case 'Redis':
6a488035
TO
104 case 'Memcache':
105 case 'Memcached':
106 $defaults = array(
107 'host' => 'localhost',
108 'port' => 11211,
109 'timeout' => 3600,
110 'prefix' => '',
111 );
112
b44e3f84 113 // Use old constants if needed to ensure backward compatibility
6a488035
TO
114 if (defined('CIVICRM_MEMCACHE_HOST')) {
115 $defaults['host'] = CIVICRM_MEMCACHE_HOST;
116 }
117
118 if (defined('CIVICRM_MEMCACHE_PORT')) {
119 $defaults['port'] = CIVICRM_MEMCACHE_PORT;
120 }
121
122 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
123 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT;
124 }
125
126 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
127 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX;
128 }
129
130 // Use new constants if possible
131 if (defined('CIVICRM_DB_CACHE_HOST')) {
132 $defaults['host'] = CIVICRM_DB_CACHE_HOST;
133 }
134
135 if (defined('CIVICRM_DB_CACHE_PORT')) {
136 $defaults['port'] = CIVICRM_DB_CACHE_PORT;
137 }
138
139 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
140 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
141 }
142
143 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
144 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
145 }
146
147 break;
148
149 case 'APCcache':
150 $defaults = array();
151 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
152 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
153 }
154 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
155 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
156 }
157 break;
158 }
159 return $defaults;
160 }
96025800 161
a4704404
TO
162 /**
163 * Create a new, named, limited-use cache.
164 *
165 * This is a factory function. Generally, you should use Civi::cache($name)
166 * to locate managed cached instance.
167 *
168 * @param array $params
169 * Array with keys:
170 * - name: string, unique symbolic name.
171 * - type: array|string, list of acceptable cache types, in order of preference.
172 * - prefetch: bool, whether to prefetch all data in cache (if possible).
173 * @return CRM_Utils_Cache_Interface
174 * @throws CRM_Core_Exception
175 * @see Civi::cache()
176 */
177 public static function create($params = array()) {
178 $types = (array) $params['type'];
179
180 foreach ($types as $type) {
181 switch ($type) {
a944a143
TO
182 case '*memory*':
183 if (defined('CIVICRM_DB_CACHE_CLASS') && in_array(CIVICRM_DB_CACHE_CLASS, array('Memcache', 'Memcached'))) {
184 $dbCacheClass = 'CRM_Utils_Cache_' . CIVICRM_DB_CACHE_CLASS;
185 $settings = self::getCacheSettings(CIVICRM_DB_CACHE_CLASS);
186 $settings['prefix'] = $settings['prefix'] . '_' . $params['name'];
187 return new $dbCacheClass($settings);
188 }
189 break;
190
a4704404
TO
191 case 'SqlGroup':
192 if (defined('CIVICRM_DSN') && CIVICRM_DSN) {
193 return new CRM_Utils_Cache_SqlGroup(array(
194 'group' => $params['name'],
195 'prefetch' => CRM_Utils_Array::value('prefetch', $params, FALSE),
196 ));
197 }
198 break;
199
200 case 'Arraycache':
201 case 'ArrayCache':
202 return new CRM_Utils_Cache_ArrayCache(array());
203
204 }
205 }
206
207 throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1));
208 }
209
6a488035 210}