|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Adapter; |
| 13 | + |
| 14 | +use Symfony\Component\Cache\Exception\CacheException; |
| 15 | +use Symfony\Component\Cache\Exception\InvalidArgumentException; |
| 16 | +use Symfony\Component\Cache\Marshaller\DefaultMarshaller; |
| 17 | +use Symfony\Component\Cache\Marshaller\MarshallerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Antonio Jose Cerezo Aranda <aj.cerezo@gmail.com> |
| 21 | + */ |
| 22 | +class CouchbaseBucketAdapter extends AbstractAdapter |
| 23 | +{ |
| 24 | + private const THIRTY_DAYS_IN_SECONDS = 2592000; |
| 25 | + private const MAX_KEY_LENGTH = 250; |
| 26 | + private const KEY_NOT_FOUND = 13; |
| 27 | + private const VALID_DSN_OPTIONS = [ |
| 28 | + 'operationTimeout', |
| 29 | + 'configTimeout', |
| 30 | + 'configNodeTimeout', |
| 31 | + 'n1qlTimeout', |
| 32 | + 'httpTimeout', |
| 33 | + 'configDelay', |
| 34 | + 'htconfigIdleTimeout', |
| 35 | + 'durabilityInterval', |
| 36 | + 'durabilityTimeout', |
| 37 | + ]; |
| 38 | + |
| 39 | + private $bucket; |
| 40 | + private $marshaller; |
| 41 | + |
| 42 | + public function __construct(\CouchbaseBucket $bucket, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null) |
| 43 | + { |
| 44 | + if (!static::isSupported()) { |
| 45 | + throw new CacheException('Couchbase >= 2.6.0 is required.'); |
| 46 | + } |
| 47 | + |
| 48 | + $this->maxIdLength = static::MAX_KEY_LENGTH; |
| 49 | + |
| 50 | + $this->bucket = $bucket; |
| 51 | + |
| 52 | + parent::__construct($namespace, $defaultLifetime); |
| 53 | + $this->enableVersioning(); |
| 54 | + $this->marshaller = $marshaller ?? new DefaultMarshaller(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param array|string $servers |
| 59 | + */ |
| 60 | + public static function createConnection($servers, array $options = []): \CouchbaseBucket |
| 61 | + { |
| 62 | + if (\is_string($servers)) { |
| 63 | + $servers = [$servers]; |
| 64 | + } elseif (!\is_array($servers)) { |
| 65 | + throw new \TypeError(sprintf('Argument 1 passed to %s() must be array or string, %s given.', __METHOD__, \gettype($servers))); |
| 66 | + } |
| 67 | + |
| 68 | + if (!static::isSupported()) { |
| 69 | + throw new CacheException('Couchbase >= 2.6.0 is required.'); |
| 70 | + } |
| 71 | + |
| 72 | + set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); }); |
| 73 | + |
| 74 | + $dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?' |
| 75 | + .'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\?]+))(?:\?(?<options>.*))?$/i'; |
| 76 | + |
| 77 | + $newServers = []; |
| 78 | + $protocol = 'couchbase'; |
| 79 | + try { |
| 80 | + $options = self::initOptions($options); |
| 81 | + $username = $options['username']; |
| 82 | + $password = $options['password']; |
| 83 | + |
| 84 | + foreach ($servers as $dsn) { |
| 85 | + if (0 !== strpos($dsn, 'couchbase:')) { |
| 86 | + throw new InvalidArgumentException(sprintf('Invalid Couchbase DSN: %s does not start with "couchbase:".', $dsn)); |
| 87 | + } |
| 88 | + |
| 89 | + preg_match($dsnPattern, $dsn, $matches); |
| 90 | + |
| 91 | + $username = $matches['username'] ?: $username; |
| 92 | + $password = $matches['password'] ?: $password; |
| 93 | + $protocol = $matches['protocol'] ?: $protocol; |
| 94 | + |
| 95 | + if (isset($matches['options'])) { |
| 96 | + $optionsInDsn = self::getOptions($matches['options']); |
| 97 | + |
| 98 | + foreach ($optionsInDsn as $parameter => $value) { |
| 99 | + $options[$parameter] = $value; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $newServers[] = $matches['host']; |
| 104 | + } |
| 105 | + |
| 106 | + $connectionString = $protocol.'://'.implode(',', $newServers); |
| 107 | + |
| 108 | + $client = new \CouchbaseCluster($connectionString); |
| 109 | + $client->authenticateAs($username, $password); |
| 110 | + |
| 111 | + $bucket = $client->openBucket($matches['bucketName']); |
| 112 | + |
| 113 | + unset($options['username'], $options['password']); |
| 114 | + foreach ($options as $option => $value) { |
| 115 | + if (!empty($value)) { |
| 116 | + $bucket->$option = $value; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return $bucket; |
| 121 | + } finally { |
| 122 | + restore_error_handler(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public static function isSupported(): bool |
| 127 | + { |
| 128 | + return \extension_loaded('couchbase') && version_compare(phpversion('couchbase'), '2.6.0', '>='); |
| 129 | + } |
| 130 | + |
| 131 | + private static function getOptions(string $options): array |
| 132 | + { |
| 133 | + $results = []; |
| 134 | + $optionsInArray = explode('&', $options); |
| 135 | + |
| 136 | + foreach ($optionsInArray as $option) { |
| 137 | + list($key, $value) = explode('=', $option); |
| 138 | + |
| 139 | + if (\in_array($key, static::VALID_DSN_OPTIONS, true)) { |
| 140 | + $results[$key] = $value; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return $results; |
| 145 | + } |
| 146 | + |
| 147 | + private static function initOptions(array $options): array |
| 148 | + { |
| 149 | + $options['username'] = $options['username'] ?? ''; |
| 150 | + $options['password'] = $options['password'] ?? ''; |
| 151 | + $options['operationTimeout'] = $options['operationTimeout'] ?? 0; |
| 152 | + $options['configTimeout'] = $options['configTimeout'] ?? 0; |
| 153 | + $options['configNodeTimeout'] = $options['configNodeTimeout'] ?? 0; |
| 154 | + $options['n1qlTimeout'] = $options['n1qlTimeout'] ?? 0; |
| 155 | + $options['httpTimeout'] = $options['httpTimeout'] ?? 0; |
| 156 | + $options['configDelay'] = $options['configDelay'] ?? 0; |
| 157 | + $options['htconfigIdleTimeout'] = $options['htconfigIdleTimeout'] ?? 0; |
| 158 | + $options['durabilityInterval'] = $options['durabilityInterval'] ?? 0; |
| 159 | + $options['durabilityTimeout'] = $options['durabilityTimeout'] ?? 0; |
| 160 | + |
| 161 | + return $options; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * {@inheritdoc} |
| 166 | + */ |
| 167 | + protected function doFetch(array $ids) |
| 168 | + { |
| 169 | + $resultsCouchbase = $this->bucket->get($ids); |
| 170 | + |
| 171 | + $results = []; |
| 172 | + foreach ($resultsCouchbase as $key => $value) { |
| 173 | + if (null !== $value->error) { |
| 174 | + continue; |
| 175 | + } |
| 176 | + $results[$key] = $this->marshaller->unmarshall($value->value); |
| 177 | + } |
| 178 | + |
| 179 | + return $results; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * {@inheritdoc} |
| 184 | + */ |
| 185 | + protected function doHave($id): bool |
| 186 | + { |
| 187 | + return false !== $this->bucket->get($id); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * {@inheritdoc} |
| 192 | + */ |
| 193 | + protected function doClear($namespace): bool |
| 194 | + { |
| 195 | + if ('' === $namespace) { |
| 196 | + $this->bucket->manager()->flush(); |
| 197 | + |
| 198 | + return true; |
| 199 | + } |
| 200 | + |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * {@inheritdoc} |
| 206 | + */ |
| 207 | + protected function doDelete(array $ids): bool |
| 208 | + { |
| 209 | + $results = $this->bucket->remove(array_values($ids)); |
| 210 | + |
| 211 | + foreach ($results as $key => $result) { |
| 212 | + if (null !== $result->error && static::KEY_NOT_FOUND !== $result->error->getCode()) { |
| 213 | + continue; |
| 214 | + } |
| 215 | + unset($results[$key]); |
| 216 | + } |
| 217 | + |
| 218 | + return 0 === \count($results); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * {@inheritdoc} |
| 223 | + */ |
| 224 | + protected function doSave(array $values, $lifetime) |
| 225 | + { |
| 226 | + if (!$values = $this->marshaller->marshall($values, $failed)) { |
| 227 | + return $failed; |
| 228 | + } |
| 229 | + |
| 230 | + $lifetime = $this->normalizeExpiry($lifetime); |
| 231 | + |
| 232 | + $ko = []; |
| 233 | + foreach ($values as $key => $value) { |
| 234 | + $result = $this->bucket->upsert($key, $value, ['expiry' => $lifetime]); |
| 235 | + |
| 236 | + if (null !== $result->error) { |
| 237 | + $ko[$key] = $result; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + return [] === $ko ? true : $ko; |
| 242 | + } |
| 243 | + |
| 244 | + private function normalizeExpiry(int $expiry): int |
| 245 | + { |
| 246 | + if ($expiry && $expiry > static::THIRTY_DAYS_IN_SECONDS) { |
| 247 | + $expiry += time(); |
| 248 | + } |
| 249 | + |
| 250 | + return $expiry; |
| 251 | + } |
| 252 | +} |
0 commit comments