| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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-2019 |
| 32 | */ |
| 33 | class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface { |
| 34 | |
| 35 | // TODO Consider native implementation. |
| 36 | use CRM_Utils_Cache_NaiveMultipleTrait; |
| 37 | |
| 38 | const DEFAULT_HOST = 'localhost'; |
| 39 | const DEFAULT_PORT = 11211; |
| 40 | const DEFAULT_TIMEOUT = 3600; |
| 41 | const DEFAULT_PREFIX = ''; |
| 42 | |
| 43 | /** |
| 44 | * If another process clears namespace, we'll find out in ~5 sec. |
| 45 | */ |
| 46 | const NS_LOCAL_TTL = 5; |
| 47 | |
| 48 | /** |
| 49 | * The host name of the memcached server. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $_host = self::DEFAULT_HOST; |
| 54 | |
| 55 | /** |
| 56 | * The port on which to connect on. |
| 57 | * |
| 58 | * @var int |
| 59 | */ |
| 60 | protected $_port = self::DEFAULT_PORT; |
| 61 | |
| 62 | /** |
| 63 | * The default timeout to use. |
| 64 | * |
| 65 | * @var int |
| 66 | */ |
| 67 | protected $_timeout = self::DEFAULT_TIMEOUT; |
| 68 | |
| 69 | /** |
| 70 | * The prefix prepended to cache keys. |
| 71 | * |
| 72 | * If we are using the same memcache instance for multiple CiviCRM |
| 73 | * installs, we must have a unique prefix for each install to prevent |
| 74 | * the keys from clobbering each other. |
| 75 | * |
| 76 | * @var string |
| 77 | */ |
| 78 | protected $_prefix = self::DEFAULT_PREFIX; |
| 79 | |
| 80 | /** |
| 81 | * The actual memcache object. |
| 82 | * |
| 83 | * @var Memcache |
| 84 | */ |
| 85 | protected $_cache; |
| 86 | |
| 87 | /** |
| 88 | * @var NULL|array |
| 89 | * |
| 90 | * This is the effective prefix. It may be bumped up whenever the dataset is flushed. |
| 91 | * |
| 92 | * @see https://github.com/memcached/memcached/wiki/ProgrammingTricks#deleting-by-namespace |
| 93 | */ |
| 94 | protected $_truePrefix = NULL; |
| 95 | |
| 96 | /** |
| 97 | * Constructor. |
| 98 | * |
| 99 | * @param array $config |
| 100 | * An array of configuration params. |
| 101 | * |
| 102 | * @return \CRM_Utils_Cache_Memcache |
| 103 | */ |
| 104 | public function __construct($config) { |
| 105 | if (isset($config['host'])) { |
| 106 | $this->_host = $config['host']; |
| 107 | } |
| 108 | if (isset($config['port'])) { |
| 109 | $this->_port = $config['port']; |
| 110 | } |
| 111 | if (isset($config['timeout'])) { |
| 112 | $this->_timeout = $config['timeout']; |
| 113 | } |
| 114 | if (isset($config['prefix'])) { |
| 115 | $this->_prefix = $config['prefix']; |
| 116 | } |
| 117 | |
| 118 | $this->_cache = new Memcache(); |
| 119 | |
| 120 | if (!$this->_cache->connect($this->_host, $this->_port)) { |
| 121 | // dont use fatal here since we can go in an infinite loop |
| 122 | echo 'Could not connect to Memcached server'; |
| 123 | CRM_Utils_System::civiExit(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param $key |
| 129 | * @param $value |
| 130 | * @param null|int|\DateInterval $ttl |
| 131 | * |
| 132 | * @return bool |
| 133 | */ |
| 134 | public function set($key, $value, $ttl = NULL) { |
| 135 | CRM_Utils_Cache::assertValidKey($key); |
| 136 | if (is_int($ttl) && $ttl <= 0) { |
| 137 | return $this->delete($key); |
| 138 | } |
| 139 | $expires = CRM_Utils_Date::convertCacheTtlToExpires($ttl, $this->_timeout); |
| 140 | return $this->_cache->set($this->getTruePrefix() . $key, serialize($value), FALSE, $expires); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param $key |
| 145 | * @param mixed $default |
| 146 | * |
| 147 | * @return mixed |
| 148 | */ |
| 149 | public function get($key, $default = NULL) { |
| 150 | CRM_Utils_Cache::assertValidKey($key); |
| 151 | $result = $this->_cache->get($this->getTruePrefix() . $key); |
| 152 | return ($result === FALSE) ? $default : unserialize($result); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param string $key |
| 157 | * |
| 158 | * @return bool |
| 159 | * @throws \Psr\SimpleCache\CacheException |
| 160 | */ |
| 161 | public function has($key) { |
| 162 | CRM_Utils_Cache::assertValidKey($key); |
| 163 | $result = $this->_cache->get($this->getTruePrefix() . $key); |
| 164 | return ($result !== FALSE); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param $key |
| 169 | * |
| 170 | * @return bool |
| 171 | */ |
| 172 | public function delete($key) { |
| 173 | CRM_Utils_Cache::assertValidKey($key); |
| 174 | $this->_cache->delete($this->getTruePrefix() . $key); |
| 175 | return TRUE; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @return bool |
| 180 | */ |
| 181 | public function flush() { |
| 182 | $this->_truePrefix = NULL; |
| 183 | $this->_cache->delete($this->_prefix); |
| 184 | return TRUE; |
| 185 | } |
| 186 | |
| 187 | public function clear() { |
| 188 | return $this->flush(); |
| 189 | } |
| 190 | |
| 191 | protected function getTruePrefix() { |
| 192 | if ($this->_truePrefix === NULL || $this->_truePrefix['expires'] < time()) { |
| 193 | $key = $this->_prefix; |
| 194 | $value = $this->_cache->get($key); |
| 195 | if ($value === FALSE) { |
| 196 | $value = uniqid(); |
| 197 | // Indefinite. |
| 198 | $this->_cache->set($key, $value, FALSE, 0); |
| 199 | } |
| 200 | $this->_truePrefix = [ |
| 201 | 'value' => $value, |
| 202 | 'expires' => time() + self::NS_LOCAL_TTL, |
| 203 | ]; |
| 204 | } |
| 205 | return $this->_prefix . $this->_truePrefix['value'] . '/'; |
| 206 | } |
| 207 | |
| 208 | } |