ApcuCache.php 2.2 KB

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