CouchbaseCache.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use Couchbase;
  4. use function explode;
  5. use function time;
  6. /**
  7. * Couchbase cache provider.
  8. *
  9. * @deprecated Couchbase SDK 1.x is now deprecated. Use \Doctrine\Common\Cache\CouchbaseBucketCache instead.
  10. * https://developer.couchbase.com/documentation/server/current/sdk/php/compatibility-versions-features.html
  11. *
  12. * @link www.doctrine-project.org
  13. */
  14. class CouchbaseCache extends CacheProvider
  15. {
  16. /** @var Couchbase|null */
  17. private $couchbase;
  18. /**
  19. * Sets the Couchbase instance to use.
  20. *
  21. * @return void
  22. */
  23. public function setCouchbase(Couchbase $couchbase)
  24. {
  25. $this->couchbase = $couchbase;
  26. }
  27. /**
  28. * Gets the Couchbase instance used by the cache.
  29. *
  30. * @return Couchbase|null
  31. */
  32. public function getCouchbase()
  33. {
  34. return $this->couchbase;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doFetch($id)
  40. {
  41. return $this->couchbase->get($id) ?: false;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function doContains($id)
  47. {
  48. return $this->couchbase->get($id) !== null;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function doSave($id, $data, $lifeTime = 0)
  54. {
  55. if ($lifeTime > 30 * 24 * 3600) {
  56. $lifeTime = time() + $lifeTime;
  57. }
  58. return $this->couchbase->set($id, $data, (int) $lifeTime);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doDelete($id)
  64. {
  65. return $this->couchbase->delete($id);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function doFlush()
  71. {
  72. return $this->couchbase->flush();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function doGetStats()
  78. {
  79. $stats = $this->couchbase->getStats();
  80. $servers = $this->couchbase->getServers();
  81. $server = explode(':', $servers[0]);
  82. $key = $server[0] . ':11210';
  83. $stats = $stats[$key];
  84. return [
  85. Cache::STATS_HITS => $stats['get_hits'],
  86. Cache::STATS_MISSES => $stats['get_misses'],
  87. Cache::STATS_UPTIME => $stats['uptime'],
  88. Cache::STATS_MEMORY_USAGE => $stats['bytes'],
  89. Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
  90. ];
  91. }
  92. }