| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 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 | */ |
| 47 | static private $_singleton = NULL; |
| 48 | |
| 49 | /** |
| 50 | * Constructor. |
| 51 | * |
| 52 | * @param array $config |
| 53 | * An array of configuration params. |
| 54 | * |
| 55 | * @return \CRM_Utils_Cache |
| 56 | */ |
| 57 | public 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 | */ |
| 66 | public static function &singleton() { |
| 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'; |
| 76 | } |
| 77 | elseif (defined('CIVICRM_USE_ARRAYCACHE') && CIVICRM_USE_ARRAYCACHE) { |
| 78 | $className = 'ArrayCache'; |
| 79 | } |
| 80 | elseif (defined('CIVICRM_DB_CACHE_CLASS') && CIVICRM_DB_CACHE_CLASS) { |
| 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; |
| 86 | $settings = self::getCacheSettings($className); |
| 87 | self::$_singleton = new $dbCacheClass($settings); |
| 88 | } |
| 89 | return self::$_singleton; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get cache relevant settings. |
| 94 | * |
| 95 | * @param $cachePlugin |
| 96 | * |
| 97 | * @return array |
| 98 | * associative array of settings for the cache |
| 99 | */ |
| 100 | public static function getCacheSettings($cachePlugin) { |
| 101 | switch ($cachePlugin) { |
| 102 | case 'ArrayCache': |
| 103 | case 'NoCache': |
| 104 | $defaults = array(); |
| 105 | break; |
| 106 | |
| 107 | case 'Memcache': |
| 108 | case 'Memcached': |
| 109 | $defaults = array( |
| 110 | 'host' => 'localhost', |
| 111 | 'port' => 11211, |
| 112 | 'timeout' => 3600, |
| 113 | 'prefix' => '', |
| 114 | ); |
| 115 | |
| 116 | // Use old constants if needed to ensure backward compatibility |
| 117 | if (defined('CIVICRM_MEMCACHE_HOST')) { |
| 118 | $defaults['host'] = CIVICRM_MEMCACHE_HOST; |
| 119 | } |
| 120 | |
| 121 | if (defined('CIVICRM_MEMCACHE_PORT')) { |
| 122 | $defaults['port'] = CIVICRM_MEMCACHE_PORT; |
| 123 | } |
| 124 | |
| 125 | if (defined('CIVICRM_MEMCACHE_TIMEOUT')) { |
| 126 | $defaults['timeout'] = CIVICRM_MEMCACHE_TIMEOUT; |
| 127 | } |
| 128 | |
| 129 | if (defined('CIVICRM_MEMCACHE_PREFIX')) { |
| 130 | $defaults['prefix'] = CIVICRM_MEMCACHE_PREFIX; |
| 131 | } |
| 132 | |
| 133 | // Use new constants if possible |
| 134 | if (defined('CIVICRM_DB_CACHE_HOST')) { |
| 135 | $defaults['host'] = CIVICRM_DB_CACHE_HOST; |
| 136 | } |
| 137 | |
| 138 | if (defined('CIVICRM_DB_CACHE_PORT')) { |
| 139 | $defaults['port'] = CIVICRM_DB_CACHE_PORT; |
| 140 | } |
| 141 | |
| 142 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { |
| 143 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; |
| 144 | } |
| 145 | |
| 146 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { |
| 147 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; |
| 148 | } |
| 149 | |
| 150 | break; |
| 151 | |
| 152 | case 'APCcache': |
| 153 | $defaults = array(); |
| 154 | if (defined('CIVICRM_DB_CACHE_TIMEOUT')) { |
| 155 | $defaults['timeout'] = CIVICRM_DB_CACHE_TIMEOUT; |
| 156 | } |
| 157 | if (defined('CIVICRM_DB_CACHE_PREFIX')) { |
| 158 | $defaults['prefix'] = CIVICRM_DB_CACHE_PREFIX; |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | return $defaults; |
| 163 | } |
| 164 | |
| 165 | } |