ApcCache.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use const PHP_VERSION_ID;
  4. use function apc_cache_info;
  5. use function apc_clear_cache;
  6. use function apc_delete;
  7. use function apc_exists;
  8. use function apc_fetch;
  9. use function apc_sma_info;
  10. use function apc_store;
  11. /**
  12. * APC cache provider.
  13. *
  14. * @deprecated since version 1.6, use ApcuCache instead
  15. *
  16. * @link www.doctrine-project.org
  17. */
  18. class ApcCache extends CacheProvider
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function doFetch($id)
  24. {
  25. return apc_fetch($id);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function doContains($id)
  31. {
  32. return apc_exists($id);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function doSave($id, $data, $lifeTime = 0)
  38. {
  39. return apc_store($id, $data, $lifeTime);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function doDelete($id)
  45. {
  46. // apc_delete returns false if the id does not exist
  47. return apc_delete($id) || ! apc_exists($id);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function doFlush()
  53. {
  54. return apc_clear_cache() && apc_clear_cache('user');
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function doFetchMultiple(array $keys)
  60. {
  61. return apc_fetch($keys) ?: [];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  67. {
  68. $result = apc_store($keysAndValues, null, $lifetime);
  69. return empty($result);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function doGetStats()
  75. {
  76. $info = apc_cache_info('', true);
  77. $sma = apc_sma_info();
  78. // @TODO - Temporary fix @see https://github.com/krakjoe/apcu/pull/42
  79. if (PHP_VERSION_ID >= 50500) {
  80. $info['num_hits'] = $info['num_hits'] ?? $info['nhits'];
  81. $info['num_misses'] = $info['num_misses'] ?? $info['nmisses'];
  82. $info['start_time'] = $info['start_time'] ?? $info['stime'];
  83. }
  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. }