ApcCache.php 2.3 KB

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