Merge pull request #5292 from jitendrapurohit/CRM-15933
[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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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
6a488035
TO
46 */
47 static private $_singleton = NULL;
48
49 /**
fe482240 50 * Constructor.
6a488035 51 *
77855840
TO
52 * @param array $config
53 * An array of configuration params.
6a488035 54 *
f4aaa82a 55 * @return \CRM_Utils_Cache
6a488035 56 */
00be9182 57 public function __construct(&$config) {
6a488035
TO
58 CRM_Core_Error::fatal(ts('this is just an interface and should not be called directly'));
59 }
60
61 /**
fe482240 62 * Singleton function used to manage this object.
6a488035
TO
63 *
64 * @return object
6a488035 65 */
00be9182 66 public static function &singleton() {
6a488035
TO
67 if (self::$_singleton === NULL) {
68 $className = 'ArrayCache'; // default to ArrayCache for now
69
70 // Maintain backward compatibility for now.
71 // Setting CIVICRM_USE_MEMCACHE or CIVICRM_USE_ARRAYCACHE will
72 // override the CIVICRM_DB_CACHE_CLASS setting.
73 // Going forward, CIVICRM_USE_xxxCACHE should be deprecated.
74 if (defined('CIVICRM_USE_MEMCACHE') && CIVICRM_USE_MEMCACHE) {
75 $className = 'Memcache';
ee1d325f 76 }
4c9b6178 77 elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) {
6a488035 78 $className = 'ArrayCache';
ee1d325f 79 }
4c9b6178 80 elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) {
6a488035
TO
81 $className = CIVICRM_DB_CACHE_CLASS;
82 }
83
84 // a generic method for utilizing any of the available db caches.
85 $dbCacheClass = 'CRM_Utils_Cache_' . $className;
e7292422 86 require_once str_replace('_', DIRECTORY_SEPARATOR, $dbCacheClass) . '.php';
6a488035
TO
87 $settings = self::getCacheSettings($className);
88 self::$_singleton = new $dbCacheClass($settings);
ee1d325f 89 }
6a488035
TO
90 return self::$_singleton;
91 }
92
93 /**
fe482240 94 * Get cache relevant settings.
6a488035 95 *
77b97be7
EM
96 * @param $cachePlugin
97 *
6a488035
TO
98 * @return array
99 * associative array of settings for the cache
6a488035 100 */
00be9182 101 public static function getCacheSettings($cachePlugin) {
6a488035
TO
102 switch ($cachePlugin) {
103 case 'ArrayCache':
104 case 'NoCache':
105 $defaults = array();
106 break;
107
108 case 'Memcache':
109 case 'Memcached':
110 $defaults = array(
111 'host' => 'localhost',
112 'port' => 11211,
113 'timeout' => 3600,
114 'prefix' => '',
115 );
116
117 // Use old constants if needed to ensure backward compatability
118 if (defined('CIVICRM_MEMCACHE_HOST')) {
119 $defaults['host'] = CIVICRM_MEMCACHE_HOST;
120 }
121
122 if (defined('CIVICRM_MEMCACHE_PORT')) {
123 $defaults['port'] = CIVICRM_MEMCACHE_PORT;
124 }
125
126 if (defined('CIVICRM_MEMCACHE_TIMEOUT')) {
127 $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT;
128 }
129
130 if (defined('CIVICRM_MEMCACHE_PREFIX')) {
131 $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX;
132 }
133
134 // Use new constants if possible
135 if (defined('CIVICRM_DB_CACHE_HOST')) {
136 $defaults['host'] = CIVICRM_DB_CACHE_HOST;
137 }
138
139 if (defined('CIVICRM_DB_CACHE_PORT')) {
140 $defaults['port'] = CIVICRM_DB_CACHE_PORT;
141 }
142
143 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
144 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
145 }
146
147 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
148 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
149 }
150
151 break;
152
153 case 'APCcache':
154 $defaults = array();
155 if (defined('CIVICRM_DB_CACHE_TIMEOUT')) {
156 $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT;
157 }
158 if (defined('CIVICRM_DB_CACHE_PREFIX')) {
159 $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX;
160 }
161 break;
162 }
163 return $defaults;
164 }
96025800 165
6a488035 166}