mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-71531 cachestore_file: Stop locking cache files to read
The cache file is only ever written to in the `write_file` function, where it does so by writing to a temp file and performing an atomic rename of that file. When writing, the target file is never locked. The cache file is only ever read in the `get` function, and there is no need for an exclusive lock in that situation. There is therefore no need to obtain any lock, shared or exclusive, to read the cache file. Doing so only affects performance of the file sytem as file locks must be needlessly obtained and written to disk for a read operation which does not benefit from them.
This commit is contained in:
parent
c6211a68b4
commit
d51d901215
12
cache/stores/file/lib.php
vendored
12
cache/stores/file/lib.php
vendored
@ -394,18 +394,20 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
|
||||
if (!$handle = fopen($file, 'rb')) {
|
||||
return false;
|
||||
}
|
||||
// Lock it up!
|
||||
// We don't care if this succeeds or not, on some systems it will, on some it won't, meah either way.
|
||||
flock($handle, LOCK_SH);
|
||||
|
||||
// Note: There is no need to perform any file locking here.
|
||||
// The cache file is only ever written to in the `write_file` function, where it does so by writing to a temp
|
||||
// file and performing an atomic rename of that file. The target file is never locked, so there is no benefit to
|
||||
// obtaining a lock (shared or exclusive) here.
|
||||
|
||||
$data = '';
|
||||
// Read the data in 1Mb chunks. Small caches will not loop more than once. We don't use filesize as it may
|
||||
// be cached with a different value than what we need to read from the file.
|
||||
do {
|
||||
$data .= fread($handle, 1048576);
|
||||
} while (!feof($handle));
|
||||
// Unlock it.
|
||||
flock($handle, LOCK_UN);
|
||||
$this->lastiobytes = strlen($data);
|
||||
|
||||
// Return it unserialised.
|
||||
return $this->prep_data_after_read($data);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user