PhpArrayCache.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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($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 should only be used on PHP 7.0+ to take advantage of how PHP
  38. * stores arrays in its latest versions. This factory method decorates the given
  39. * fallback pool with this adapter only if the current PHP version is supported.
  40. *
  41. * @param string $file The PHP file were values are cached
  42. *
  43. * @return CacheInterface
  44. */
  45. public static function create($file, CacheInterface $fallbackPool)
  46. {
  47. // Shared memory is available in PHP 7.0+ with OPCache enabled and in HHVM
  48. if ((\PHP_VERSION_ID >= 70000 && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) || \defined('HHVM_VERSION')) {
  49. return new static($file, $fallbackPool);
  50. }
  51. return $fallbackPool;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function get($key, $default = null)
  57. {
  58. if (!\is_string($key)) {
  59. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  60. }
  61. if (null === $this->values) {
  62. $this->initialize();
  63. }
  64. if (!isset($this->values[$key])) {
  65. return $this->pool->get($key, $default);
  66. }
  67. $value = $this->values[$key];
  68. if ('N;' === $value) {
  69. $value = null;
  70. } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
  71. try {
  72. $e = null;
  73. $value = unserialize($value);
  74. } catch (\Error $e) {
  75. } catch (\Exception $e) {
  76. }
  77. if (null !== $e) {
  78. return $default;
  79. }
  80. }
  81. return $value;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getMultiple($keys, $default = null)
  87. {
  88. if ($keys instanceof \Traversable) {
  89. $keys = iterator_to_array($keys, false);
  90. } elseif (!\is_array($keys)) {
  91. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  92. }
  93. foreach ($keys as $key) {
  94. if (!\is_string($key)) {
  95. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  96. }
  97. }
  98. if (null === $this->values) {
  99. $this->initialize();
  100. }
  101. return $this->generateItems($keys, $default);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function has($key)
  107. {
  108. if (!\is_string($key)) {
  109. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  110. }
  111. if (null === $this->values) {
  112. $this->initialize();
  113. }
  114. return isset($this->values[$key]) || $this->pool->has($key);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function delete($key)
  120. {
  121. if (!\is_string($key)) {
  122. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  123. }
  124. if (null === $this->values) {
  125. $this->initialize();
  126. }
  127. return !isset($this->values[$key]) && $this->pool->delete($key);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function deleteMultiple($keys)
  133. {
  134. if (!\is_array($keys) && !$keys instanceof \Traversable) {
  135. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  136. }
  137. $deleted = true;
  138. $fallbackKeys = array();
  139. foreach ($keys as $key) {
  140. if (!\is_string($key)) {
  141. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  142. }
  143. if (isset($this->values[$key])) {
  144. $deleted = false;
  145. } else {
  146. $fallbackKeys[] = $key;
  147. }
  148. }
  149. if (null === $this->values) {
  150. $this->initialize();
  151. }
  152. if ($fallbackKeys) {
  153. $deleted = $this->pool->deleteMultiple($fallbackKeys) && $deleted;
  154. }
  155. return $deleted;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function set($key, $value, $ttl = null)
  161. {
  162. if (!\is_string($key)) {
  163. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  164. }
  165. if (null === $this->values) {
  166. $this->initialize();
  167. }
  168. return !isset($this->values[$key]) && $this->pool->set($key, $value, $ttl);
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function setMultiple($values, $ttl = null)
  174. {
  175. if (!\is_array($values) && !$values instanceof \Traversable) {
  176. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
  177. }
  178. $saved = true;
  179. $fallbackValues = array();
  180. foreach ($values as $key => $value) {
  181. if (!\is_string($key) && !\is_int($key)) {
  182. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  183. }
  184. if (isset($this->values[$key])) {
  185. $saved = false;
  186. } else {
  187. $fallbackValues[$key] = $value;
  188. }
  189. }
  190. if ($fallbackValues) {
  191. $saved = $this->pool->setMultiple($fallbackValues, $ttl) && $saved;
  192. }
  193. return $saved;
  194. }
  195. private function generateItems(array $keys, $default)
  196. {
  197. $fallbackKeys = array();
  198. foreach ($keys as $key) {
  199. if (isset($this->values[$key])) {
  200. $value = $this->values[$key];
  201. if ('N;' === $value) {
  202. yield $key => null;
  203. } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
  204. try {
  205. yield $key => unserialize($value);
  206. } catch (\Error $e) {
  207. yield $key => $default;
  208. } catch (\Exception $e) {
  209. yield $key => $default;
  210. }
  211. } else {
  212. yield $key => $value;
  213. }
  214. } else {
  215. $fallbackKeys[] = $key;
  216. }
  217. }
  218. if ($fallbackKeys) {
  219. foreach ($this->pool->getMultiple($fallbackKeys, $default) as $key => $item) {
  220. yield $key => $item;
  221. }
  222. }
  223. }
  224. }