PhpArrayCache.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\Simple;
  11. use Psr\SimpleCache\CacheInterface;
  12. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. use Symfony\Component\Cache\ResettableInterface;
  15. use Symfony\Component\Cache\Traits\PhpArrayTrait;
  16. /**
  17. * Caches items at warm up time using a PHP array that is stored in shared memory by OPCache since PHP 7.0.
  18. * Warmed up items are read-only and run-time discovered items are cached using a fallback adapter.
  19. *
  20. * @author Titouan Galopin <galopintitouan@gmail.com>
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class PhpArrayCache implements CacheInterface, PruneableInterface, ResettableInterface
  24. {
  25. use PhpArrayTrait;
  26. /**
  27. * @param string $file The PHP file were values are cached
  28. * @param CacheInterface $fallbackPool A pool to fallback on when an item is not hit
  29. */
  30. public function __construct(string $file, CacheInterface $fallbackPool)
  31. {
  32. $this->file = $file;
  33. $this->pool = $fallbackPool;
  34. $this->zendDetectUnicode = filter_var(ini_get('zend.detect_unicode'), FILTER_VALIDATE_BOOLEAN);
  35. }
  36. /**
  37. * This adapter takes advantage of how PHP stores arrays in its latest versions.
  38. *
  39. * @param string $file The PHP file were values are cached
  40. *
  41. * @return CacheInterface
  42. */
  43. public static function create($file, CacheInterface $fallbackPool)
  44. {
  45. // Shared memory is available in PHP 7.0+ with OPCache enabled
  46. if (filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
  47. return new static($file, $fallbackPool);
  48. }
  49. return $fallbackPool;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function get($key, $default = null)
  55. {
  56. if (!\is_string($key)) {
  57. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  58. }
  59. if (null === $this->values) {
  60. $this->initialize();
  61. }
  62. if (!isset($this->values[$key])) {
  63. return $this->pool->get($key, $default);
  64. }
  65. $value = $this->values[$key];
  66. if ('N;' === $value) {
  67. return null;
  68. }
  69. if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
  70. try {
  71. return unserialize($value);
  72. } catch (\Throwable $e) {
  73. return $default;
  74. }
  75. }
  76. return $value;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getMultiple($keys, $default = null)
  82. {
  83. if ($keys instanceof \Traversable) {
  84. $keys = iterator_to_array($keys, false);
  85. } elseif (!\is_array($keys)) {
  86. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  87. }
  88. foreach ($keys as $key) {
  89. if (!\is_string($key)) {
  90. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  91. }
  92. }
  93. if (null === $this->values) {
  94. $this->initialize();
  95. }
  96. return $this->generateItems($keys, $default);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function has($key)
  102. {
  103. if (!\is_string($key)) {
  104. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  105. }
  106. if (null === $this->values) {
  107. $this->initialize();
  108. }
  109. return isset($this->values[$key]) || $this->pool->has($key);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function delete($key)
  115. {
  116. if (!\is_string($key)) {
  117. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  118. }
  119. if (null === $this->values) {
  120. $this->initialize();
  121. }
  122. return !isset($this->values[$key]) && $this->pool->delete($key);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function deleteMultiple($keys)
  128. {
  129. if (!\is_array($keys) && !$keys instanceof \Traversable) {
  130. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  131. }
  132. $deleted = true;
  133. $fallbackKeys = [];
  134. foreach ($keys as $key) {
  135. if (!\is_string($key)) {
  136. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  137. }
  138. if (isset($this->values[$key])) {
  139. $deleted = false;
  140. } else {
  141. $fallbackKeys[] = $key;
  142. }
  143. }
  144. if (null === $this->values) {
  145. $this->initialize();
  146. }
  147. if ($fallbackKeys) {
  148. $deleted = $this->pool->deleteMultiple($fallbackKeys) && $deleted;
  149. }
  150. return $deleted;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function set($key, $value, $ttl = null)
  156. {
  157. if (!\is_string($key)) {
  158. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  159. }
  160. if (null === $this->values) {
  161. $this->initialize();
  162. }
  163. return !isset($this->values[$key]) && $this->pool->set($key, $value, $ttl);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function setMultiple($values, $ttl = null)
  169. {
  170. if (!\is_array($values) && !$values instanceof \Traversable) {
  171. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
  172. }
  173. $saved = true;
  174. $fallbackValues = [];
  175. foreach ($values as $key => $value) {
  176. if (!\is_string($key) && !\is_int($key)) {
  177. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  178. }
  179. if (isset($this->values[$key])) {
  180. $saved = false;
  181. } else {
  182. $fallbackValues[$key] = $value;
  183. }
  184. }
  185. if ($fallbackValues) {
  186. $saved = $this->pool->setMultiple($fallbackValues, $ttl) && $saved;
  187. }
  188. return $saved;
  189. }
  190. private function generateItems(array $keys, $default)
  191. {
  192. $fallbackKeys = [];
  193. foreach ($keys as $key) {
  194. if (isset($this->values[$key])) {
  195. $value = $this->values[$key];
  196. if ('N;' === $value) {
  197. yield $key => null;
  198. } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
  199. try {
  200. yield $key => unserialize($value);
  201. } catch (\Throwable $e) {
  202. yield $key => $default;
  203. }
  204. } else {
  205. yield $key => $value;
  206. }
  207. } else {
  208. $fallbackKeys[] = $key;
  209. }
  210. }
  211. if ($fallbackKeys) {
  212. foreach ($this->pool->getMultiple($fallbackKeys, $default) as $key => $item) {
  213. yield $key => $item;
  214. }
  215. }
  216. }
  217. }