MemcachedCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 \Memcached;
  21. /**
  22. * Memcached cache provider.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.2
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. * @author David Abdemoulaie <dave@hobodave.com>
  31. */
  32. class MemcachedCache extends CacheProvider
  33. {
  34. /**
  35. * @var Memcached|null
  36. */
  37. private $memcached;
  38. /**
  39. * Sets the memcache instance to use.
  40. *
  41. * @param Memcached $memcached
  42. *
  43. * @return void
  44. */
  45. public function setMemcached(Memcached $memcached)
  46. {
  47. $this->memcached = $memcached;
  48. }
  49. /**
  50. * Gets the memcached instance used by the cache.
  51. *
  52. * @return Memcached|null
  53. */
  54. public function getMemcached()
  55. {
  56. return $this->memcached;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function doFetch($id)
  62. {
  63. return $this->memcached->get($id);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function doFetchMultiple(array $keys)
  69. {
  70. return $this->memcached->getMulti($keys) ?: [];
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  76. {
  77. if ($lifetime > 30 * 24 * 3600) {
  78. $lifetime = time() + $lifetime;
  79. }
  80. return $this->memcached->setMulti($keysAndValues, $lifetime);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function doContains($id)
  86. {
  87. $this->memcached->get($id);
  88. return $this->memcached->getResultCode() === Memcached::RES_SUCCESS;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. protected function doSave($id, $data, $lifeTime = 0)
  94. {
  95. if ($lifeTime > 30 * 24 * 3600) {
  96. $lifeTime = time() + $lifeTime;
  97. }
  98. return $this->memcached->set($id, $data, (int) $lifeTime);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function doDelete($id)
  104. {
  105. return $this->memcached->delete($id)
  106. || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function doFlush()
  112. {
  113. return $this->memcached->flush();
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. protected function doGetStats()
  119. {
  120. $stats = $this->memcached->getStats();
  121. $servers = $this->memcached->getServerList();
  122. $key = $servers[0]['host'] . ':' . $servers[0]['port'];
  123. $stats = $stats[$key];
  124. return array(
  125. Cache::STATS_HITS => $stats['get_hits'],
  126. Cache::STATS_MISSES => $stats['get_misses'],
  127. Cache::STATS_UPTIME => $stats['uptime'],
  128. Cache::STATS_MEMORY_USAGE => $stats['bytes'],
  129. Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
  130. );
  131. }
  132. }