INFRA-132 - CRM/Upgrade - phpcbf (plus fixup)
[civicrm-core.git] / CRM / Utils / Cache.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Cache is an empty base object, we'll modify the scheme when we have different caching schemes
38 *
39 */
40class CRM_Utils_Cache {
41 /**
42 * We only need one instance of this object. So we use the singleton
43 * pattern and cache the instance in this variable
44 *
45 * @var object
46 * @static
47 */
48 static private $_singleton = NULL;
49
50 /**
51 * Constructor
52 *
77855840
TO
53 * @param array $config
54 * An array of configuration params.
6a488035 55 *
f4aaa82a 56 * @return \CRM_Utils_Cache
6a488035 57 */
00be9182 58 public function __construct(&$config) {
6a488035
TO
59 CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly'));
60 }
61
62 /**
100fef9d 63 * Singleton function used to manage this object
6a488035
TO
64 *
65 * @return object
66 * @static
67 *
68 */
00be9182 69 public static function &singleton() {
6a488035
TO
70 if (self::$_singleton === NULL) {
71 $className = 'ArrayCache'; // default to ArrayCache for now
72
73 // Maintain backward compatibility for now.
74 // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
75 // override the CIVICRM_DB_CACHE_CLASS setting.
76 // Going forward, CIVICRM_USE_xxxCACHE should be deprecated.
77 if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE) {
78 $className = 'Memcache';
ee1d325f 79 }
6a488035
TO
80 else if (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) {
81 $className = 'ArrayCache';
ee1d325f 82 }
6a488035
TO
83 else if (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) {
84 $className = CIVICRM_DB_CACHE_CLASS;
85 }
86
87 // a generic method for utilizing any of the available db caches.
88 $dbCacheClass = 'CRM_Utils_Cache_' . $className;
89 require_once(str_replace('_', DIRECTORY_SEPARATOR, $dbCacheClass) . '.php');
90 $settings = self::getCacheSettings($className);
91 self::$_singleton = new $dbCacheClass($settings);
ee1d325f 92 }
6a488035
TO
93 return self::$_singleton;
94 }
95
96 /**
97 * Get cache relevant settings
98 *
77b97be7
EM
99 * @param $cachePlugin
100 *
6a488035
TO
101 * @return array
102 * associative array of settings for the cache
103 * @static
104 */
00be9182 105 public static function getCacheSettings($cachePlugin) {
6a488035
TO
106 switch ($cachePlugin) {
107 case 'ArrayCache':
108 case 'NoCache':
109 $defaults = array();
110 break;
111
112 case 'Memcache':
113 case 'Memcached':
114 $defaults = array(
115 'host' => 'localhost',
116 'port' => 11211,
117 'timeout' => 3600,
118 'prefix' => '',
119 );
120
121 // Use old constants if needed to ensure backward compatability
122 if (defined('CIVICRM_MEMCACHE_HOST')) {
123 $defaults['host'] = CIVICRM_MEMCACHE_HOST;
124 }
125
126 if (defined('CIVICRM_MEMCACHE_PORT')) {
127 $defaults['port'] = CIVICRM_MEMCACHE_PORT;
128 }
129
130 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
131 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT;
132 }
133
134 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
135 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX;
136 }
137
138 // Use new constants if possible
139 if (defined('CIVICRM_DB_CACHE_HOST')) {
140 $defaults['host'] = CIVICRM_DB_CACHE_HOST;
141 }
142
143 if (defined('CIVICRM_DB_CACHE_PORT')) {
144 $defaults['port'] = CIVICRM_DB_CACHE_PORT;
145 }
146
147 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
148 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
149 }
150
151 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
152 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
153 }
154
155 break;
156
157 case 'APCcache':
158 $defaults = array();
159 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
160 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
161 }
162 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
163 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
164 }
165 break;
166 }
167 return $defaults;
168 }
169}