| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
| 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-2020 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { |
| 36 | |
| 37 | // TODO Consider native implementation. |
| 38 | use CRM_Utils_Cache_NaiveMultipleTrait; |
| 39 | // TODO Native implementation |
| 40 | use CRM_Utils_Cache_NaiveHasTrait; |
| 41 | |
| 42 | const DEFAULT_HOST = 'localhost'; |
| 43 | const DEFAULT_PORT = 6379; |
| 44 | const DEFAULT_TIMEOUT = 3600; |
| 45 | const DEFAULT_PREFIX = ''; |
| 46 | |
| 47 | /** |
| 48 | * The default timeout to use |
| 49 | * |
| 50 | * @var int |
| 51 | */ |
| 52 | protected $_timeout = self::DEFAULT_TIMEOUT; |
| 53 | |
| 54 | /** |
| 55 | * The prefix prepended to cache keys. |
| 56 | * |
| 57 | * If we are using the same redis instance for multiple CiviCRM |
| 58 | * installs, we must have a unique prefix for each install to prevent |
| 59 | * the keys from clobbering each other. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $_prefix = self::DEFAULT_PREFIX; |
| 64 | |
| 65 | /** |
| 66 | * The actual redis object |
| 67 | * |
| 68 | * @var Redis |
| 69 | */ |
| 70 | protected $_cache; |
| 71 | |
| 72 | /** |
| 73 | * Create a connection. If a connection already exists, re-use it. |
| 74 | * |
| 75 | * @param array $config |
| 76 | * @return Redis |
| 77 | */ |
| 78 | public static function connect($config) { |
| 79 | $host = isset($config['host']) ? $config['host'] : self::DEFAULT_HOST; |
| 80 | $port = isset($config['port']) ? $config['port'] : self::DEFAULT_PORT; |
| 81 | // Ugh. |
| 82 | $pass = CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD'); |
| 83 | $id = implode(':', ['connect', $host, $port /* $pass is constant */]); |
| 84 | if (!isset(Civi::$statics[__CLASS__][$id])) { |
| 85 | // Ideally, we'd track the connection in the service-container, but the |
| 86 | // cache connection is boot-critical. |
| 87 | $redis = new Redis(); |
| 88 | if (!$redis->connect($host, $port)) { |
| 89 | // dont use fatal here since we can go in an infinite loop |
| 90 | echo 'Could not connect to redisd server'; |
| 91 | CRM_Utils_System::civiExit(); |
| 92 | } |
| 93 | if ($pass) { |
| 94 | $redis->auth($pass); |
| 95 | } |
| 96 | Civi::$statics[__CLASS__][$id] = $redis; |
| 97 | } |
| 98 | return Civi::$statics[__CLASS__][$id]; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Constructor |
| 103 | * |
| 104 | * @param array $config |
| 105 | * An array of configuration params. |
| 106 | * |
| 107 | * @return \CRM_Utils_Cache_Redis |
| 108 | */ |
| 109 | public function __construct($config) { |
| 110 | if (isset($config['timeout'])) { |
| 111 | $this->_timeout = $config['timeout']; |
| 112 | } |
| 113 | if (isset($config['prefix'])) { |
| 114 | $this->_prefix = $config['prefix']; |
| 115 | } |
| 116 | |
| 117 | $this->_cache = self::connect($config); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param $key |
| 122 | * @param $value |
| 123 | * @param null|int|\DateInterval $ttl |
| 124 | * |
| 125 | * @return bool |
| 126 | * @throws Exception |
| 127 | */ |
| 128 | public function set($key, $value, $ttl = NULL) { |
| 129 | CRM_Utils_Cache::assertValidKey($key); |
| 130 | if (is_int($ttl) && $ttl <= 0) { |
| 131 | return $this->delete($key); |
| 132 | } |
| 133 | $ttl = CRM_Utils_Date::convertCacheTtl($ttl, self::DEFAULT_TIMEOUT); |
| 134 | if (!$this->_cache->setex($this->_prefix . $key, $ttl, serialize($value))) { |
| 135 | if (PHP_SAPI === 'cli' || (Civi\Core\Container::isContainerBooted() && CRM_Core_Permission::check('view debug output'))) { |
| 136 | throw new CRM_Utils_Cache_CacheException("Redis set ($key) failed: " . $this->_cache->getLastError()); |
| 137 | } |
| 138 | else { |
| 139 | Civi::log()->error("Redis set ($key) failed: " . $this->_cache->getLastError()); |
| 140 | throw new CRM_Utils_Cache_CacheException("Redis set ($key) failed"); |
| 141 | } |
| 142 | return FALSE; |
| 143 | } |
| 144 | return TRUE; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param $key |
| 149 | * @param mixed $default |
| 150 | * |
| 151 | * @return mixed |
| 152 | */ |
| 153 | public function get($key, $default = NULL) { |
| 154 | CRM_Utils_Cache::assertValidKey($key); |
| 155 | $result = $this->_cache->get($this->_prefix . $key); |
| 156 | return ($result === FALSE) ? $default : unserialize($result); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param $key |
| 161 | * |
| 162 | * @return bool |
| 163 | */ |
| 164 | public function delete($key) { |
| 165 | CRM_Utils_Cache::assertValidKey($key); |
| 166 | $this->_cache->delete($this->_prefix . $key); |
| 167 | return TRUE; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @return bool |
| 172 | */ |
| 173 | public function flush() { |
| 174 | // FIXME: Ideally, we'd map each prefix to a different 'hash' object in Redis, |
| 175 | // and this would be simpler. However, that needs to go in tandem with a |
| 176 | // more general rethink of cache expiration/TTL. |
| 177 | |
| 178 | $keys = $this->_cache->keys($this->_prefix . '*'); |
| 179 | $this->_cache->del($keys); |
| 180 | return TRUE; |
| 181 | } |
| 182 | |
| 183 | public function clear() { |
| 184 | return $this->flush(); |
| 185 | } |
| 186 | |
| 187 | } |