AbstractAdapter.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Adapter;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. use Symfony\Component\Cache\ResettableInterface;
  16. use Symfony\Component\Cache\Traits\AbstractAdapterTrait;
  17. use Symfony\Component\Cache\Traits\ContractsTrait;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. /**
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
  23. {
  24. use AbstractAdapterTrait;
  25. use ContractsTrait;
  26. /**
  27. * @internal
  28. */
  29. protected const NS_SEPARATOR = ':';
  30. private static bool $apcuSupported;
  31. protected function __construct(string $namespace = '', int $defaultLifetime = 0)
  32. {
  33. $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR;
  34. $this->defaultLifetime = $defaultLifetime;
  35. if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
  36. throw new InvalidArgumentException(\sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));
  37. }
  38. self::$createCacheItem ??= \Closure::bind(
  39. static function ($key, $value, $isHit) {
  40. $item = new CacheItem();
  41. $item->key = $key;
  42. $item->value = $value;
  43. $item->isHit = $isHit;
  44. $item->unpack();
  45. return $item;
  46. },
  47. null,
  48. CacheItem::class
  49. );
  50. self::$mergeByLifetime ??= \Closure::bind(
  51. static function ($deferred, $namespace, &$expiredIds, $getId, $defaultLifetime) {
  52. $byLifetime = [];
  53. $now = microtime(true);
  54. $expiredIds = [];
  55. foreach ($deferred as $key => $item) {
  56. $key = (string) $key;
  57. if (null === $item->expiry) {
  58. $ttl = 0 < $defaultLifetime ? $defaultLifetime : 0;
  59. } elseif (!$item->expiry) {
  60. $ttl = 0;
  61. } elseif (0 >= $ttl = (int) (0.1 + $item->expiry - $now)) {
  62. $expiredIds[] = $getId($key);
  63. continue;
  64. }
  65. $byLifetime[$ttl][$getId($key)] = $item->pack();
  66. }
  67. return $byLifetime;
  68. },
  69. null,
  70. CacheItem::class
  71. );
  72. }
  73. /**
  74. * Returns the best possible adapter that your runtime supports.
  75. *
  76. * Using ApcuAdapter makes system caches compatible with read-only filesystems.
  77. */
  78. public static function createSystemCache(string $namespace, int $defaultLifetime, string $version, string $directory, ?LoggerInterface $logger = null): AdapterInterface
  79. {
  80. $opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory, true);
  81. if (null !== $logger) {
  82. $opcache->setLogger($logger);
  83. }
  84. if (!self::$apcuSupported ??= ApcuAdapter::isSupported()) {
  85. return $opcache;
  86. }
  87. if ('cli' === \PHP_SAPI && !filter_var(\ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOL)) {
  88. return $opcache;
  89. }
  90. $apcu = new ApcuAdapter($namespace, intdiv($defaultLifetime, 5), $version);
  91. if (null !== $logger) {
  92. $apcu->setLogger($logger);
  93. }
  94. return new ChainAdapter([$apcu, $opcache]);
  95. }
  96. public static function createConnection(#[\SensitiveParameter] string $dsn, array $options = []): mixed
  97. {
  98. if (str_starts_with($dsn, 'redis:') || str_starts_with($dsn, 'rediss:')) {
  99. return RedisAdapter::createConnection($dsn, $options);
  100. }
  101. if (str_starts_with($dsn, 'memcached:')) {
  102. return MemcachedAdapter::createConnection($dsn, $options);
  103. }
  104. if (str_starts_with($dsn, 'couchbase:')) {
  105. if (class_exists('CouchbaseBucket') && CouchbaseBucketAdapter::isSupported()) {
  106. return CouchbaseBucketAdapter::createConnection($dsn, $options);
  107. }
  108. return CouchbaseCollectionAdapter::createConnection($dsn, $options);
  109. }
  110. if (preg_match('/^(mysql|oci|pgsql|sqlsrv|sqlite):/', $dsn)) {
  111. return PdoAdapter::createConnection($dsn, $options);
  112. }
  113. throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:", "couchbase:", "mysql:", "oci:", "pgsql:", "sqlsrv:" nor "sqlite:".');
  114. }
  115. public function commit(): bool
  116. {
  117. $ok = true;
  118. $byLifetime = (self::$mergeByLifetime)($this->deferred, $this->namespace, $expiredIds, $this->getId(...), $this->defaultLifetime);
  119. $retry = $this->deferred = [];
  120. if ($expiredIds) {
  121. try {
  122. $this->doDelete($expiredIds);
  123. } catch (\Exception $e) {
  124. $ok = false;
  125. CacheItem::log($this->logger, 'Failed to delete expired items: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]);
  126. }
  127. }
  128. foreach ($byLifetime as $lifetime => $values) {
  129. try {
  130. $e = $this->doSave($values, $lifetime);
  131. } catch (\Exception $e) {
  132. }
  133. if (true === $e || [] === $e) {
  134. continue;
  135. }
  136. if (\is_array($e) || 1 === \count($values)) {
  137. foreach (\is_array($e) ? $e : array_keys($values) as $id) {
  138. $ok = false;
  139. $v = $values[$id];
  140. $type = get_debug_type($v);
  141. $message = \sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
  142. CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
  143. }
  144. } else {
  145. foreach ($values as $id => $v) {
  146. $retry[$lifetime][] = $id;
  147. }
  148. }
  149. }
  150. // When bulk-save failed, retry each item individually
  151. foreach ($retry as $lifetime => $ids) {
  152. foreach ($ids as $id) {
  153. try {
  154. $v = $byLifetime[$lifetime][$id];
  155. $e = $this->doSave([$id => $v], $lifetime);
  156. } catch (\Exception $e) {
  157. }
  158. if (true === $e || [] === $e) {
  159. continue;
  160. }
  161. $ok = false;
  162. $type = get_debug_type($v);
  163. $message = \sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
  164. CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]);
  165. }
  166. }
  167. return $ok;
  168. }
  169. }