MemcacheCache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 \Memcache;
  21. /**
  22. * Memcache cache provider.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.0
  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 MemcacheCache extends CacheProvider
  33. {
  34. /**
  35. * @var Memcache|null
  36. */
  37. private $memcache;
  38. /**
  39. * Sets the memcache instance to use.
  40. *
  41. * @param Memcache $memcache
  42. *
  43. * @return void
  44. */
  45. public function setMemcache(Memcache $memcache)
  46. {
  47. $this->memcache = $memcache;
  48. }
  49. /**
  50. * Gets the memcache instance used by the cache.
  51. *
  52. * @return Memcache|null
  53. */
  54. public function getMemcache()
  55. {
  56. return $this->memcache;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function doFetch($id)
  62. {
  63. return $this->memcache->get($id);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function doContains($id)
  69. {
  70. $flags = null;
  71. $this->memcache->get($id, $flags);
  72. //if memcache has changed the value of "flags", it means the value exists
  73. return ($flags !== null);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function doSave($id, $data, $lifeTime = 0)
  79. {
  80. if ($lifeTime > 30 * 24 * 3600) {
  81. $lifeTime = time() + $lifeTime;
  82. }
  83. return $this->memcache->set($id, $data, 0, (int) $lifeTime);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function doDelete($id)
  89. {
  90. // Memcache::delete() returns false if entry does not exist
  91. return $this->memcache->delete($id) || ! $this->doContains($id);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function doFlush()
  97. {
  98. return $this->memcache->flush();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function doGetStats()
  104. {
  105. $stats = $this->memcache->getStats();
  106. return array(
  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. }