CouchbaseCache.php 2.3 KB

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