Commit | Line | Data |
---|---|---|
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 | */ | |
40 | class 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 | * | |
f4aaa82a | 53 | * @param array $config 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 | /** | |
100fef9d | 62 | * Singleton function used to manage this object |
6a488035 TO |
63 | * |
64 | * @return object | |
65 | * @static | |
66 | * | |
67 | */ | |
00be9182 | 68 | public static function &singleton() { |
6a488035 TO |
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'; | |
ee1d325f | 78 | } |
6a488035 TO |
79 | else if (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) { |
80 | $className = 'ArrayCache'; | |
ee1d325f | 81 | } |
6a488035 TO |
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); | |
ee1d325f | 91 | } |
6a488035 TO |
92 | return self::$_singleton; |
93 | } | |
94 | ||
95 | /** | |
96 | * Get cache relevant settings | |
97 | * | |
77b97be7 EM |
98 | * @param $cachePlugin |
99 | * | |
6a488035 TO |
100 | * @return array |
101 | * associative array of settings for the cache | |
102 | * @static | |
103 | */ | |
00be9182 | 104 | public static function getCacheSettings($cachePlugin) { |
6a488035 TO |
105 | switch ($cachePlugin) { |
106 | case 'ArrayCache': | |
107 | case 'NoCache': | |
108 | $defaults = array(); | |
109 | break; | |
110 | ||
111 | case 'Memcache': | |
112 | case 'Memcached': | |
113 | $defaults = array( | |
114 | 'host' => 'localhost', | |
115 | 'port' => 11211, | |
116 | 'timeout' => 3600, | |
117 | 'prefix' => '', | |
118 | ); | |
119 | ||
120 | // Use old constants if needed to ensure backward compatability | |
121 | if (defined('CIVICRM_MEMCACHE_HOST')) { | |
122 | $defaults['host'] = CIVICRM_MEMCACHE_HOST; | |
123 | } | |
124 | ||
125 | if (defined('CIVICRM_MEMCACHE_PORT')) { | |
126 | $defaults['port'] = CIVICRM_MEMCACHE_PORT; | |
127 | } | |
128 | ||
129 | if (defined('CIVICRM_MEMCACHE_TIMEOUT')) { | |
130 | $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT; | |
131 | } | |
132 | ||
133 | if (defined('CIVICRM_MEMCACHE_PREFIX')) { | |
134 | $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX; | |
135 | } | |
136 | ||
137 | // Use new constants if possible | |
138 | if (defined('CIVICRM_DB_CACHE_HOST')) { | |
139 | $defaults['host'] = CIVICRM_DB_CACHE_HOST; | |
140 | } | |
141 | ||
142 | if (defined('CIVICRM_DB_CACHE_PORT')) { | |
143 | $defaults['port'] = CIVICRM_DB_CACHE_PORT; | |
144 | } | |
145 | ||
146 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { | |
147 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; | |
148 | } | |
149 | ||
150 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { | |
151 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; | |
152 | } | |
153 | ||
154 | break; | |
155 | ||
156 | case 'APCcache': | |
157 | $defaults = array(); | |
158 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { | |
159 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; | |
160 | } | |
161 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { | |
162 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; | |
163 | } | |
164 | break; | |
165 | } | |
166 | return $defaults; | |
167 | } | |
168 | } |