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