/** * Returns the best possible adapter that your runtime supports. * * Using ApcuAdapter makes system caches compatible with read-only filesystems. * * @param string $namespace * @param int $defaultLifetime * @param string $version * @param string $directory * * @return AdapterInterface */ public static function createSystemCache($namespace, $defaultLifetime, $version, $directory, LoggerInterface $logger = null) { $opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory, true); if (null !== $logger) { $opcache->setLogger($logger); } if (!self::$apcuSupported = self::$apcuSupported ?? ApcuAdapter::isSupported()) { return $opcache; } if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && !filter_var(ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOLEAN)) { return $opcache; } $apcu = new ApcuAdapter($namespace, intdiv($defaultLifetime, 5), $version); if (null !== $logger) { $apcu->setLogger($logger); } return new ChainAdapter([$apcu, $opcache]); } public static function createConnection($dsn, array $options = []) { if (!\is_string($dsn)) { throw new InvalidArgumentException(sprintf('The "%s()" method expect argument #1 to be string, "%s" given.', __METHOD__, \gettype($dsn))); } if (str_starts_with($dsn, 'redis:') || str_starts_with($dsn, 'rediss:')) { return RedisAdapter::createConnection($dsn, $options); } if (str_starts_with($dsn, 'memcached:')) { return MemcachedAdapter::createConnection($dsn, $options); } throw new InvalidArgumentException(sprintf('Unsupported DSN: "%s".', $dsn)); } /** * {@inheritdoc} * * @return bool */ public function commit() { $ok = true; $byLifetime = $this->mergeByLifetime; $byLifetime = $byLifetime($this->deferred, $this->namespace, $expiredIds); $retry = $this->deferred = []; if ($expiredIds) { $this->doDelete($expiredIds); } foreach ($byLifetime as $lifetime => $values) { try { $e = $this->doSave($values, $lifetime); } catch (\Exception $e) { } if (true === $e || [] === $e) { continue; } if (\is_array($e) || 1 === \count($values)) { foreach (\is_array($e) ? $e : array_keys($values) as $id) { $ok = false; $v = $values[$id]; $type = \is_object($v) ? \get_class($v) : \gettype($v); $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); } } else { foreach ($values as $id => $v) { $retry[$lifetime][] = $id; } } } // When bulk-save failed, retry each item individually foreach ($retry as $lifetime => $ids) { foreach ($ids as $id) { try { $v = $byLifetime[$lifetime][$id]; $e = $this->doSave([$id => $v], $lifetime); } catch (\Exception $e) { } if (true === $e || [] === $e) { continue; } $ok = false; $type = \is_object($v) ? \get_class($v) : \gettype($v); $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); } } return $ok; } }