CouchbaseCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Cache;
  20. use \Couchbase;
  21. /**
  22. * Couchbase cache provider.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.4
  26. * @author Michael Nitschinger <michael@nitschinger.at>
  27. */
  28. class CouchbaseCache extends CacheProvider
  29. {
  30. /**
  31. * @var Couchbase|null
  32. */
  33. private $couchbase;
  34. /**
  35. * Sets the Couchbase instance to use.
  36. *
  37. * @param Couchbase $couchbase
  38. *
  39. * @return void
  40. */
  41. public function setCouchbase(Couchbase $couchbase)
  42. {
  43. $this->couchbase = $couchbase;
  44. }
  45. /**
  46. * Gets the Couchbase instance used by the cache.
  47. *
  48. * @return Couchbase|null
  49. */
  50. public function getCouchbase()
  51. {
  52. return $this->couchbase;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function doFetch($id)
  58. {
  59. return $this->couchbase->get($id) ?: false;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function doContains($id)
  65. {
  66. return (null !== $this->couchbase->get($id));
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function doSave($id, $data, $lifeTime = 0)
  72. {
  73. if ($lifeTime > 30 * 24 * 3600) {
  74. $lifeTime = time() + $lifeTime;
  75. }
  76. return $this->couchbase->set($id, $data, (int) $lifeTime);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function doDelete($id)
  82. {
  83. return $this->couchbase->delete($id);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function doFlush()
  89. {
  90. return $this->couchbase->flush();
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function doGetStats()
  96. {
  97. $stats = $this->couchbase->getStats();
  98. $servers = $this->couchbase->getServers();
  99. $server = explode(":", $servers[0]);
  100. $key = $server[0] . ":" . "11210";
  101. $stats = $stats[$key];
  102. return array(
  103. Cache::STATS_HITS => $stats['get_hits'],
  104. Cache::STATS_MISSES => $stats['get_misses'],
  105. Cache::STATS_UPTIME => $stats['uptime'],
  106. Cache::STATS_MEMORY_USAGE => $stats['bytes'],
  107. Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
  108. );
  109. }
  110. }