Skip to content

Commit

Permalink
Merge pull request #4697 from MGatner/filehandler-crash
Browse files Browse the repository at this point in the history
Cache robustness
  • Loading branch information
MGatner committed May 19, 2021
2 parents 8dceca2 + 9f77525 commit eb243d3
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,35 +285,13 @@ public function getMetaData(string $key)
{
$key = static::validateKey($key, $this->prefix);

if (! is_file($this->path . $key))
if (false === $data = $this->getItem($key))
{
return false; // This will return null in a future release
}

$data = @unserialize(file_get_contents($this->path . $key));

if (! is_array($data) || ! isset($data['ttl']))
{
return false; // This will return null in a future release
}

// Consider expired items as missing
$expire = $data['time'] + $data['ttl'];

// @phpstan-ignore-next-line
if ($data['ttl'] > 0 && time() > $expire)
{
// If the file is still there then remove it
if (is_file($this->path . $key))
{
unlink($this->path . $key);
}

return false; // This will return null in a future release
}

return [
'expire' => $expire,
'expire' => $data['time'] + $data['ttl'],
'mtime' => filemtime($this->path . $key),
'data' => $data['data'],
];
Expand Down Expand Up @@ -348,15 +326,19 @@ protected function getItem(string $filename)
return false;
}

$data = unserialize(file_get_contents($this->path . $filename));
$data = @unserialize(file_get_contents($this->path . $filename));
if (! is_array($data) || ! isset($data['ttl']))
{
return false;
}

// @phpstan-ignore-next-line
if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
{
// If the file is still there then remove it
// If the file is still there then try to remove it
if (is_file($this->path . $filename))
{
unlink($this->path . $filename);
@unlink($this->path . $filename);
}

return false;
Expand Down

0 comments on commit eb243d3

Please sign in to comment.