MDL-79185 cache: don't throw exception if cache data is corrupted

This commit is contained in:
Dmitrii Metelkin 2023-09-01 13:15:20 +10:00
parent 94ad185d09
commit 402153c46e

View File

@ -441,8 +441,17 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
} while (!feof($handle));
$this->lastiobytes = strlen($data);
if ($this->lastiobytes == 0) {
// Potentially statcache is stale. File can be deleted, let's clear cache and recheck.
clearstatcache(true, $file);
if (!file_exists($file)) {
// It's a completely normal condition. Just ignore and keep going.
return false;
}
}
// Return it unserialised.
return $this->prep_data_after_read($data);
return $this->prep_data_after_read($data, $file);
}
/**
@ -548,13 +557,14 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
* Prepares the data it has been read from the cache. Undoing what was done in prep_data_before_save.
*
* @param string $data
* @param string $path
* @return mixed
* @throws coding_exception
*/
protected function prep_data_after_read($data) {
protected function prep_data_after_read($data, $path) {
$result = @unserialize($data);
if ($result === false && $data != serialize(false)) {
throw new coding_exception('Failed to unserialise data from file. Either failed to read, or failed to write.');
debugging('Failed to unserialise data from cache file: ' . $path . '. Data: ' . $data, DEBUG_DEVELOPER);
return false;
}
return $result;
}