CouchbaseCollectionAdapter.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Couchbase\Bucket;
  12. use Couchbase\Cluster;
  13. use Couchbase\ClusterOptions;
  14. use Couchbase\Collection;
  15. use Couchbase\DocumentNotFoundException;
  16. use Couchbase\UpsertOptions;
  17. use Symfony\Component\Cache\Exception\CacheException;
  18. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  19. use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
  20. use Symfony\Component\Cache\Marshaller\MarshallerInterface;
  21. /**
  22. * @author Antonio Jose Cerezo Aranda <aj.cerezo@gmail.com>
  23. */
  24. class CouchbaseCollectionAdapter extends AbstractAdapter
  25. {
  26. private const MAX_KEY_LENGTH = 250;
  27. /** @var Collection */
  28. private $connection;
  29. private $marshaller;
  30. public function __construct(Collection $connection, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
  31. {
  32. if (!static::isSupported()) {
  33. throw new CacheException('Couchbase >= 3.0.0 < 4.0.0 is required.');
  34. }
  35. $this->maxIdLength = static::MAX_KEY_LENGTH;
  36. $this->connection = $connection;
  37. parent::__construct($namespace, $defaultLifetime);
  38. $this->enableVersioning();
  39. $this->marshaller = $marshaller ?? new DefaultMarshaller();
  40. }
  41. /**
  42. * @param array|string $dsn
  43. *
  44. * @return Bucket|Collection
  45. */
  46. public static function createConnection($dsn, array $options = [])
  47. {
  48. if (\is_string($dsn)) {
  49. $dsn = [$dsn];
  50. } elseif (!\is_array($dsn)) {
  51. throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be array or string, "%s" given.', __METHOD__, get_debug_type($dsn)));
  52. }
  53. if (!static::isSupported()) {
  54. throw new CacheException('Couchbase >= 3.0.0 < 4.0.0 is required.');
  55. }
  56. set_error_handler(function ($type, $msg, $file, $line): bool { throw new \ErrorException($msg, 0, $type, $file, $line); });
  57. $dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
  58. .'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))'
  59. .'(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?(?:\?(?<options>.*))?$/i';
  60. $newServers = [];
  61. $protocol = 'couchbase';
  62. try {
  63. $username = $options['username'] ?? '';
  64. $password = $options['password'] ?? '';
  65. foreach ($dsn as $server) {
  66. if (0 !== strpos($server, 'couchbase:')) {
  67. throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
  68. }
  69. preg_match($dsnPattern, $server, $matches);
  70. $username = $matches['username'] ?: $username;
  71. $password = $matches['password'] ?: $password;
  72. $protocol = $matches['protocol'] ?: $protocol;
  73. if (isset($matches['options'])) {
  74. $optionsInDsn = self::getOptions($matches['options']);
  75. foreach ($optionsInDsn as $parameter => $value) {
  76. $options[$parameter] = $value;
  77. }
  78. }
  79. $newServers[] = $matches['host'];
  80. }
  81. $option = isset($matches['options']) ? '?'.$matches['options'] : '';
  82. $connectionString = $protocol.'://'.implode(',', $newServers).$option;
  83. $clusterOptions = new ClusterOptions();
  84. $clusterOptions->credentials($username, $password);
  85. $client = new Cluster($connectionString, $clusterOptions);
  86. $bucket = $client->bucket($matches['bucketName']);
  87. $collection = $bucket->defaultCollection();
  88. if (!empty($matches['scopeName'])) {
  89. $scope = $bucket->scope($matches['scopeName']);
  90. $collection = $scope->collection($matches['collectionName']);
  91. }
  92. return $collection;
  93. } finally {
  94. restore_error_handler();
  95. }
  96. }
  97. public static function isSupported(): bool
  98. {
  99. return \extension_loaded('couchbase') && version_compare(phpversion('couchbase'), '3.0.5', '>=') && version_compare(phpversion('couchbase'), '4.0', '<');
  100. }
  101. private static function getOptions(string $options): array
  102. {
  103. $results = [];
  104. $optionsInArray = explode('&', $options);
  105. foreach ($optionsInArray as $option) {
  106. [$key, $value] = explode('=', $option);
  107. $results[$key] = $value;
  108. }
  109. return $results;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function doFetch(array $ids): array
  115. {
  116. $results = [];
  117. foreach ($ids as $id) {
  118. try {
  119. $resultCouchbase = $this->connection->get($id);
  120. } catch (DocumentNotFoundException $exception) {
  121. continue;
  122. }
  123. $content = $resultCouchbase->value ?? $resultCouchbase->content();
  124. $results[$id] = $this->marshaller->unmarshall($content);
  125. }
  126. return $results;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. protected function doHave($id): bool
  132. {
  133. return $this->connection->exists($id)->exists();
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. protected function doClear($namespace): bool
  139. {
  140. return false;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. protected function doDelete(array $ids): bool
  146. {
  147. $idsErrors = [];
  148. foreach ($ids as $id) {
  149. try {
  150. $result = $this->connection->remove($id);
  151. if (null === $result->mutationToken()) {
  152. $idsErrors[] = $id;
  153. }
  154. } catch (DocumentNotFoundException $exception) {
  155. }
  156. }
  157. return 0 === \count($idsErrors);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. protected function doSave(array $values, $lifetime)
  163. {
  164. if (!$values = $this->marshaller->marshall($values, $failed)) {
  165. return $failed;
  166. }
  167. $upsertOptions = new UpsertOptions();
  168. $upsertOptions->expiry($lifetime);
  169. $ko = [];
  170. foreach ($values as $key => $value) {
  171. try {
  172. $this->connection->upsert($key, $value, $upsertOptions);
  173. } catch (\Exception $exception) {
  174. $ko[$key] = '';
  175. }
  176. }
  177. return [] === $ko ? true : $ko;
  178. }
  179. }