MemcachedCache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use Memcached;
  4. use function time;
  5. /**
  6. * Memcached cache provider.
  7. *
  8. * @link www.doctrine-project.org
  9. */
  10. class MemcachedCache extends CacheProvider
  11. {
  12. /** @var Memcached|null */
  13. private $memcached;
  14. /**
  15. * Sets the memcache instance to use.
  16. *
  17. * @return void
  18. */
  19. public function setMemcached(Memcached $memcached)
  20. {
  21. $this->memcached = $memcached;
  22. }
  23. /**
  24. * Gets the memcached instance used by the cache.
  25. *
  26. * @return Memcached|null
  27. */
  28. public function getMemcached()
  29. {
  30. return $this->memcached;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function doFetch($id)
  36. {
  37. return $this->memcached->get($id);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function doFetchMultiple(array $keys)
  43. {
  44. return $this->memcached->getMulti($keys) ?: [];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  50. {
  51. if ($lifetime > 30 * 24 * 3600) {
  52. $lifetime = time() + $lifetime;
  53. }
  54. return $this->memcached->setMulti($keysAndValues, $lifetime);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function doContains($id)
  60. {
  61. $this->memcached->get($id);
  62. return $this->memcached->getResultCode() === Memcached::RES_SUCCESS;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function doSave($id, $data, $lifeTime = 0)
  68. {
  69. if ($lifeTime > 30 * 24 * 3600) {
  70. $lifeTime = time() + $lifeTime;
  71. }
  72. return $this->memcached->set($id, $data, (int) $lifeTime);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function doDeleteMultiple(array $keys)
  78. {
  79. return $this->memcached->deleteMulti($keys)
  80. || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function doDelete($id)
  86. {
  87. return $this->memcached->delete($id)
  88. || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. protected function doFlush()
  94. {
  95. return $this->memcached->flush();
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. protected function doGetStats()
  101. {
  102. $stats = $this->memcached->getStats();
  103. $servers = $this->memcached->getServerList();
  104. $key = $servers[0]['host'] . ':' . $servers[0]['port'];
  105. $stats = $stats[$key];
  106. return [
  107. Cache::STATS_HITS => $stats['get_hits'],
  108. Cache::STATS_MISSES => $stats['get_misses'],
  109. Cache::STATS_UPTIME => $stats['uptime'],
  110. Cache::STATS_MEMORY_USAGE => $stats['bytes'],
  111. Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
  112. ];
  113. }
  114. }