框架升级及修复升级后的bug
This commit is contained in:
+57
-50
@@ -18,7 +18,6 @@ use Psr\Log\LoggerAwareTrait;
|
||||
use Symfony\Component\Cache\CacheItem;
|
||||
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Cache\ResettableInterface;
|
||||
use Symfony\Component\VarExporter\DeepCloner;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\NamespacedPoolInterface;
|
||||
|
||||
@@ -42,11 +41,11 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
private static \Closure $createCacheItem;
|
||||
|
||||
/**
|
||||
* @param bool $deepClone Disabling deep-cloning can lead to cache corruptions when storing mutable values but increases performance otherwise
|
||||
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
|
||||
*/
|
||||
public function __construct(
|
||||
private int $defaultLifetime = 0,
|
||||
private bool $deepClone = true,
|
||||
private bool $storeSerialized = true,
|
||||
private float $maxLifetime = 0,
|
||||
private int $maxItems = 0,
|
||||
private ?ClockInterface $clock = null,
|
||||
@@ -60,11 +59,12 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
}
|
||||
|
||||
self::$createCacheItem ??= \Closure::bind(
|
||||
static function ($key, $value, $isHit, $tags, $expiry = null) {
|
||||
static function ($key, $value, $isHit, $tags, $expiry = null, $clock = null) {
|
||||
$item = new CacheItem();
|
||||
$item->key = $key;
|
||||
$item->value = $value;
|
||||
$item->isHit = $isHit;
|
||||
$item->clock = $clock;
|
||||
if (null !== $tags) {
|
||||
$item->metadata[CacheItem::METADATA_TAGS] = $tags;
|
||||
}
|
||||
@@ -79,9 +79,6 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param-immediately-invoked-callable $callback
|
||||
*/
|
||||
public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed
|
||||
{
|
||||
$item = $this->getItem($key);
|
||||
@@ -131,10 +128,10 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
$this->values[$key] = null;
|
||||
}
|
||||
} else {
|
||||
$value = $this->deepClone ? $this->unfreeze($key, $isHit) : $this->values[$key];
|
||||
$value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
|
||||
}
|
||||
|
||||
return (self::$createCacheItem)($key, $value, $isHit, $this->tags[$key] ?? null, $this->explicitExpiries[$key] ?? null);
|
||||
return (self::$createCacheItem)($key, $value, $isHit, $this->tags[$key] ?? null, $this->explicitExpiries[$key] ?? null, $this->clock);
|
||||
}
|
||||
|
||||
public function getItems(array $keys = []): iterable
|
||||
@@ -186,26 +183,8 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ($this->deepClone) {
|
||||
try {
|
||||
$cloner = new DeepCloner($value, null, true);
|
||||
} catch (\Exception $e) {
|
||||
if (!isset($this->expiries[$key])) {
|
||||
unset($this->values[$key]);
|
||||
}
|
||||
$type = get_debug_type($value);
|
||||
$message = \sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
|
||||
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// keep static values unwrapped for performance, except null (which must
|
||||
// stay distinguishable from a cache miss) and strings holding a colon
|
||||
// (which getValues() consumers must not confuse with a serialized value)
|
||||
if (!$cloner->isStaticValue() || null === $value || (\is_string($value) && str_contains($value, ':'))) {
|
||||
$value = $cloner;
|
||||
}
|
||||
if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
|
||||
return false;
|
||||
}
|
||||
if (null === $expiry && 0 < $this->defaultLifetime) {
|
||||
$expiry = $this->defaultLifetime;
|
||||
@@ -298,27 +277,20 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
|
||||
/**
|
||||
* Returns all cached values, with cache miss as null.
|
||||
*
|
||||
* @param bool $raw Whether to return the raw stored values (DeepCloner instances and unwrapped scalars) instead of serialized strings
|
||||
*/
|
||||
public function getValues(/* bool $raw = false */): array
|
||||
public function getValues(): array
|
||||
{
|
||||
$raw = \func_num_args() ? func_get_arg(0) : false;
|
||||
|
||||
if (!$this->deepClone || $raw) {
|
||||
if (!$this->storeSerialized) {
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
$values = $this->values;
|
||||
foreach ($values as $k => $v) {
|
||||
if (null === $v) {
|
||||
if (null === $v || 'N;' === $v) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$values[$k] = serialize($v instanceof DeepCloner ? $v->clone(null, true) : $v);
|
||||
} catch (\Exception) {
|
||||
// skip values that cannot be serialized, e.g. when they hold a Closure
|
||||
unset($values[$k]);
|
||||
if (!\is_string($v) || !isset($v[2]) || ':' !== $v[1]) {
|
||||
$values[$k] = serialize($v);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,11 +327,11 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
$this->values[$key] = $value;
|
||||
}
|
||||
|
||||
$value = $this->deepClone ? $this->unfreeze($key, $isHit) : $this->values[$key];
|
||||
$value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
|
||||
}
|
||||
unset($keys[$i]);
|
||||
|
||||
yield $key => $f($key, $value, $isHit, $this->tags[$key] ?? null, $this->explicitExpiries[$key] ?? null);
|
||||
yield $key => $f($key, $value, $isHit, $this->tags[$key] ?? null, $this->explicitExpiries[$key] ?? null, $this->clock);
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
@@ -367,22 +339,57 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, NamespacedPoolIn
|
||||
}
|
||||
}
|
||||
|
||||
private function freeze($value, string $key): string|int|float|bool|array|\UnitEnum|null
|
||||
{
|
||||
if (null === $value) {
|
||||
return 'N;';
|
||||
}
|
||||
if (\is_string($value)) {
|
||||
// Serialize strings if they could be confused with serialized objects or arrays
|
||||
if ('N;' === $value || (isset($value[2]) && ':' === $value[1])) {
|
||||
return serialize($value);
|
||||
}
|
||||
} elseif (!\is_scalar($value)) {
|
||||
try {
|
||||
$serialized = serialize($value);
|
||||
} catch (\Exception $e) {
|
||||
if (!isset($this->expiries[$key])) {
|
||||
unset($this->values[$key]);
|
||||
}
|
||||
$type = get_debug_type($value);
|
||||
$message = \sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
|
||||
CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
|
||||
|
||||
return null;
|
||||
}
|
||||
// Keep value serialized if it contains any objects or any internal references
|
||||
if ('C' === $serialized[0] || 'O' === $serialized[0] || preg_match('/;[OCRr]:[1-9]/', $serialized)) {
|
||||
return $serialized;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function unfreeze(string $key, bool &$isHit): mixed
|
||||
{
|
||||
$value = $this->values[$key];
|
||||
|
||||
if ($value instanceof DeepCloner) {
|
||||
if ('N;' === $value = $this->values[$key]) {
|
||||
return null;
|
||||
}
|
||||
if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
|
||||
try {
|
||||
return $value->clone(null, true);
|
||||
$value = unserialize($value, ['allowed_classes' => true]);
|
||||
} catch (\Exception $e) {
|
||||
CacheItem::log($this->logger, 'Failed to clone key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
|
||||
CacheItem::log($this->logger, 'Failed to unserialize key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
|
||||
$value = false;
|
||||
}
|
||||
if (false === $value) {
|
||||
$value = null;
|
||||
$isHit = false;
|
||||
|
||||
if (!$this->maxItems) {
|
||||
$this->values[$key] = null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user