ApcuCache.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use function apcu_cache_info;
  4. use function apcu_clear_cache;
  5. use function apcu_delete;
  6. use function apcu_exists;
  7. use function apcu_fetch;
  8. use function apcu_sma_info;
  9. use function apcu_store;
  10. use function count;
  11. /**
  12. * APCu cache provider.
  13. *
  14. * @link www.doctrine-project.org
  15. */
  16. class ApcuCache extends CacheProvider
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function doFetch($id)
  22. {
  23. return apcu_fetch($id);
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function doContains($id)
  29. {
  30. return apcu_exists($id);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function doSave($id, $data, $lifeTime = 0)
  36. {
  37. return apcu_store($id, $data, $lifeTime);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function doDelete($id)
  43. {
  44. // apcu_delete returns false if the id does not exist
  45. return apcu_delete($id) || ! apcu_exists($id);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function doDeleteMultiple(array $keys)
  51. {
  52. $result = apcu_delete($keys);
  53. return $result !== false && count($result) !== count($keys);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function doFlush()
  59. {
  60. return apcu_clear_cache();
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function doFetchMultiple(array $keys)
  66. {
  67. return apcu_fetch($keys) ?: [];
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  73. {
  74. $result = apcu_store($keysAndValues, null, $lifetime);
  75. return empty($result);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function doGetStats()
  81. {
  82. $info = apcu_cache_info(true);
  83. $sma = apcu_sma_info();
  84. return [
  85. Cache::STATS_HITS => $info['num_hits'],
  86. Cache::STATS_MISSES => $info['num_misses'],
  87. Cache::STATS_UPTIME => $info['start_time'],
  88. Cache::STATS_MEMORY_USAGE => $info['mem_size'],
  89. Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
  90. ];
  91. }
  92. }