CouchbaseCollectionAdapter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. private MarshallerInterface $marshaller;
  28. public function __construct(
  29. private Collection $connection,
  30. string $namespace = '',
  31. int $defaultLifetime = 0,
  32. ?MarshallerInterface $marshaller = null,
  33. ) {
  34. if (!static::isSupported()) {
  35. throw new CacheException('Couchbase >= 3.0.5 < 4.0.0 is required.');
  36. }
  37. $this->maxIdLength = static::MAX_KEY_LENGTH;
  38. parent::__construct($namespace, $defaultLifetime);
  39. $this->enableVersioning();
  40. $this->marshaller = $marshaller ?? new DefaultMarshaller();
  41. }
  42. public static function createConnection(#[\SensitiveParameter] array|string $dsn, array $options = []): Bucket|Collection
  43. {
  44. if (\is_string($dsn)) {
  45. $dsn = [$dsn];
  46. }
  47. if (!static::isSupported()) {
  48. throw new CacheException('Couchbase >= 3.0.5 < 4.0.0 is required.');
  49. }
  50. set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
  51. $pathPattern = '/^(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?$/';
  52. $newServers = [];
  53. $protocol = 'couchbase';
  54. try {
  55. $username = $options['username'] ?? '';
  56. $password = $options['password'] ?? '';
  57. foreach ($dsn as $server) {
  58. if (!str_starts_with($server, 'couchbase:')) {
  59. throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
  60. }
  61. $params = parse_url($server);
  62. $username = isset($params['user']) ? rawurldecode($params['user']) : $username;
  63. $password = isset($params['pass']) ? rawurldecode($params['pass']) : $password;
  64. $protocol = $params['scheme'] ?? $protocol;
  65. if (isset($params['query'])) {
  66. $optionsInDsn = self::getOptions($params['query']);
  67. foreach ($optionsInDsn as $parameter => $value) {
  68. $options[$parameter] = $value;
  69. }
  70. }
  71. $newServers[] = $params['host'];
  72. }
  73. $option = isset($params['query']) ? '?'.$params['query'] : '';
  74. $connectionString = $protocol.'://'.implode(',', $newServers).$option;
  75. $clusterOptions = new ClusterOptions();
  76. $clusterOptions->credentials($username, $password);
  77. $client = new Cluster($connectionString, $clusterOptions);
  78. preg_match($pathPattern, $params['path'] ?? '', $matches);
  79. $bucket = $client->bucket($matches['bucketName']);
  80. $collection = $bucket->defaultCollection();
  81. if (!empty($matches['scopeName'])) {
  82. $scope = $bucket->scope($matches['scopeName']);
  83. $collection = $scope->collection($matches['collectionName']);
  84. }
  85. return $collection;
  86. } finally {
  87. restore_error_handler();
  88. }
  89. }
  90. public static function isSupported(): bool
  91. {
  92. return \extension_loaded('couchbase') && version_compare(phpversion('couchbase'), '3.0.5', '>=') && version_compare(phpversion('couchbase'), '4.0', '<');
  93. }
  94. private static function getOptions(string $options): array
  95. {
  96. $results = [];
  97. $optionsInArray = explode('&', $options);
  98. foreach ($optionsInArray as $option) {
  99. [$key, $value] = explode('=', $option);
  100. $results[$key] = $value;
  101. }
  102. return $results;
  103. }
  104. protected function doFetch(array $ids): array
  105. {
  106. $results = [];
  107. foreach ($ids as $id) {
  108. try {
  109. $resultCouchbase = $this->connection->get($id);
  110. } catch (DocumentNotFoundException) {
  111. continue;
  112. }
  113. $content = $resultCouchbase->value ?? $resultCouchbase->content();
  114. $results[$id] = $this->marshaller->unmarshall($content);
  115. }
  116. return $results;
  117. }
  118. protected function doHave($id): bool
  119. {
  120. return $this->connection->exists($id)->exists();
  121. }
  122. protected function doClear($namespace): bool
  123. {
  124. return false;
  125. }
  126. protected function doDelete(array $ids): bool
  127. {
  128. $idsErrors = [];
  129. foreach ($ids as $id) {
  130. try {
  131. $result = $this->connection->remove($id);
  132. if (null === $result->mutationToken()) {
  133. $idsErrors[] = $id;
  134. }
  135. } catch (DocumentNotFoundException) {
  136. }
  137. }
  138. return 0 === \count($idsErrors);
  139. }
  140. protected function doSave(array $values, $lifetime): array|bool
  141. {
  142. if (!$values = $this->marshaller->marshall($values, $failed)) {
  143. return $failed;
  144. }
  145. $upsertOptions = new UpsertOptions();
  146. $upsertOptions->expiry($lifetime);
  147. $ko = [];
  148. foreach ($values as $key => $value) {
  149. try {
  150. $this->connection->upsert($key, $value, $upsertOptions);
  151. } catch (\Exception) {
  152. $ko[$key] = '';
  153. }
  154. }
  155. return [] === $ko ? true : $ko;
  156. }
  157. }