CouchbaseBucketCache.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Common\Cache;
  4. use Couchbase\Bucket;
  5. use Couchbase\Document;
  6. use Couchbase\Exception;
  7. use RuntimeException;
  8. use function phpversion;
  9. use function serialize;
  10. use function sprintf;
  11. use function substr;
  12. use function time;
  13. use function unserialize;
  14. use function version_compare;
  15. /**
  16. * Couchbase ^2.3.0 cache provider.
  17. *
  18. * @deprecated Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0
  19. */
  20. final class CouchbaseBucketCache extends CacheProvider
  21. {
  22. private const MINIMUM_VERSION = '2.3.0';
  23. private const KEY_NOT_FOUND = 13;
  24. private const MAX_KEY_LENGTH = 250;
  25. private const THIRTY_DAYS_IN_SECONDS = 2592000;
  26. /** @var Bucket */
  27. private $bucket;
  28. public function __construct(Bucket $bucket)
  29. {
  30. if (version_compare(phpversion('couchbase'), self::MINIMUM_VERSION) < 0) {
  31. // Manager is required to flush cache and pull stats.
  32. throw new RuntimeException(sprintf('ext-couchbase:^%s is required.', self::MINIMUM_VERSION));
  33. }
  34. $this->bucket = $bucket;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doFetch($id)
  40. {
  41. $id = $this->normalizeKey($id);
  42. try {
  43. $document = $this->bucket->get($id);
  44. } catch (Exception $e) {
  45. return false;
  46. }
  47. if ($document instanceof Document && $document->value !== false) {
  48. return unserialize($document->value);
  49. }
  50. return false;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function doContains($id)
  56. {
  57. $id = $this->normalizeKey($id);
  58. try {
  59. $document = $this->bucket->get($id);
  60. } catch (Exception $e) {
  61. return false;
  62. }
  63. if ($document instanceof Document) {
  64. return ! $document->error;
  65. }
  66. return false;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function doSave($id, $data, $lifeTime = 0)
  72. {
  73. $id = $this->normalizeKey($id);
  74. $lifeTime = $this->normalizeExpiry($lifeTime);
  75. try {
  76. $encoded = serialize($data);
  77. $document = $this->bucket->upsert($id, $encoded, [
  78. 'expiry' => (int) $lifeTime,
  79. ]);
  80. } catch (Exception $e) {
  81. return false;
  82. }
  83. if ($document instanceof Document) {
  84. return ! $document->error;
  85. }
  86. return false;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. protected function doDelete($id)
  92. {
  93. $id = $this->normalizeKey($id);
  94. try {
  95. $document = $this->bucket->remove($id);
  96. } catch (Exception $e) {
  97. return $e->getCode() === self::KEY_NOT_FOUND;
  98. }
  99. if ($document instanceof Document) {
  100. return ! $document->error;
  101. }
  102. return false;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function doFlush()
  108. {
  109. $manager = $this->bucket->manager();
  110. // Flush does not return with success or failure, and must be enabled per bucket on the server.
  111. // Store a marker item so that we will know if it was successful.
  112. $this->doSave(__METHOD__, true, 60);
  113. $manager->flush();
  114. if ($this->doContains(__METHOD__)) {
  115. $this->doDelete(__METHOD__);
  116. return false;
  117. }
  118. return true;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. protected function doGetStats()
  124. {
  125. $manager = $this->bucket->manager();
  126. $stats = $manager->info();
  127. $nodes = $stats['nodes'];
  128. $node = $nodes[0];
  129. $interestingStats = $node['interestingStats'];
  130. return [
  131. Cache::STATS_HITS => $interestingStats['get_hits'],
  132. Cache::STATS_MISSES => $interestingStats['cmd_get'] - $interestingStats['get_hits'],
  133. Cache::STATS_UPTIME => $node['uptime'],
  134. Cache::STATS_MEMORY_USAGE => $interestingStats['mem_used'],
  135. Cache::STATS_MEMORY_AVAILABLE => $node['memoryFree'],
  136. ];
  137. }
  138. private function normalizeKey(string $id): string
  139. {
  140. $normalized = substr($id, 0, self::MAX_KEY_LENGTH);
  141. if ($normalized === false) {
  142. return $id;
  143. }
  144. return $normalized;
  145. }
  146. /**
  147. * Expiry treated as a unix timestamp instead of an offset if expiry is greater than 30 days.
  148. *
  149. * @src https://developer.couchbase.com/documentation/server/4.1/developer-guide/expiry.html
  150. */
  151. private function normalizeExpiry(int $expiry): int
  152. {
  153. if ($expiry > self::THIRTY_DAYS_IN_SECONDS) {
  154. return time() + $expiry;
  155. }
  156. return $expiry;
  157. }
  158. }