MemcachedCache.php 3.3KB

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