ApcuCache.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  21. * APCu cache provider.
  22. *
  23. * @link www.doctrine-project.org
  24. * @since 1.6
  25. * @author Kévin Dunglas <dunglas@gmail.com>
  26. */
  27. class ApcuCache extends CacheProvider
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function doFetch($id)
  33. {
  34. return apcu_fetch($id);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doContains($id)
  40. {
  41. return apcu_exists($id);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function doSave($id, $data, $lifeTime = 0)
  47. {
  48. return apcu_store($id, $data, $lifeTime);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function doDelete($id)
  54. {
  55. // apcu_delete returns false if the id does not exist
  56. return apcu_delete($id) || ! apcu_exists($id);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function doFlush()
  62. {
  63. return apcu_clear_cache();
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function doFetchMultiple(array $keys)
  69. {
  70. return apcu_fetch($keys) ?: [];
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  76. {
  77. $result = apcu_store($keysAndValues, null, $lifetime);
  78. return empty($result);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. protected function doGetStats()
  84. {
  85. $info = apcu_cache_info(true);
  86. $sma = apcu_sma_info();
  87. return array(
  88. Cache::STATS_HITS => $info['num_hits'],
  89. Cache::STATS_MISSES => $info['num_misses'],
  90. Cache::STATS_UPTIME => $info['start_time'],
  91. Cache::STATS_MEMORY_USAGE => $info['mem_size'],
  92. Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
  93. );
  94. }
  95. }