ApcuTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Symfony\Component\Cache\CacheItem;
  12. use Symfony\Component\Cache\Exception\CacheException;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. *
  16. * @internal
  17. */
  18. trait ApcuTrait
  19. {
  20. public static function isSupported()
  21. {
  22. return \function_exists('apcu_fetch') && filter_var(\ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN);
  23. }
  24. private function init(string $namespace, int $defaultLifetime, ?string $version)
  25. {
  26. if (!static::isSupported()) {
  27. throw new CacheException('APCu is not enabled.');
  28. }
  29. if ('cli' === \PHP_SAPI) {
  30. ini_set('apc.use_request_time', 0);
  31. }
  32. parent::__construct($namespace, $defaultLifetime);
  33. if (null !== $version) {
  34. CacheItem::validateKey($version);
  35. if (!apcu_exists($version.'@'.$namespace)) {
  36. $this->doClear($namespace);
  37. apcu_add($version.'@'.$namespace, null);
  38. }
  39. }
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function doFetch(array $ids)
  45. {
  46. $unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
  47. try {
  48. $values = [];
  49. $ids = array_flip($ids);
  50. foreach (apcu_fetch(array_keys($ids), $ok) ?: [] as $k => $v) {
  51. if (!isset($ids[$k])) {
  52. // work around https://github.com/krakjoe/apcu/issues/247
  53. $k = key($ids);
  54. }
  55. unset($ids[$k]);
  56. if (null !== $v || $ok) {
  57. $values[$k] = $v;
  58. }
  59. }
  60. return $values;
  61. } catch (\Error $e) {
  62. throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR, $e->getFile(), $e->getLine());
  63. } finally {
  64. ini_set('unserialize_callback_func', $unserializeCallbackHandler);
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function doHave($id)
  71. {
  72. return apcu_exists($id);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function doClear($namespace)
  78. {
  79. return isset($namespace[0]) && class_exists(\APCUIterator::class, false) && ('cli' !== \PHP_SAPI || filter_var(\ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOLEAN))
  80. ? apcu_delete(new \APCUIterator(sprintf('/^%s/', preg_quote($namespace, '/')), \APC_ITER_KEY))
  81. : apcu_clear_cache();
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. protected function doDelete(array $ids)
  87. {
  88. foreach ($ids as $id) {
  89. apcu_delete($id);
  90. }
  91. return true;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function doSave(array $values, int $lifetime)
  97. {
  98. try {
  99. if (false === $failures = apcu_store($values, null, $lifetime)) {
  100. $failures = $values;
  101. }
  102. return array_keys($failures);
  103. } catch (\Throwable $e) {
  104. if (1 === \count($values)) {
  105. // Workaround https://github.com/krakjoe/apcu/issues/170
  106. apcu_delete(array_key_first($values));
  107. }
  108. throw $e;
  109. }
  110. }
  111. }