bug #18724 [Cache] Lock-less FilesystemAdapter (nicolas-grekas)
This PR was merged into the 3.1-dev branch.
Discussion
----------
[Cache] Lock-less FilesystemAdapter
| Q | A
| ------------- | ---
| Branch? | 3.1
| Bug fix? | yes (perf)
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
53b1ee5
[Cache] Lock-less FilesystemAdapter
This commit is contained in:
commit
8f6c3b0711
@ -63,9 +63,7 @@ class FilesystemAdapter extends AbstractAdapter
|
||||
if (!$h = @fopen($file, 'rb')) {
|
||||
continue;
|
||||
}
|
||||
flock($h, LOCK_SH);
|
||||
if ($now >= (int) $expiresAt = fgets($h)) {
|
||||
flock($h, LOCK_UN);
|
||||
fclose($h);
|
||||
if (isset($expiresAt[0])) {
|
||||
@unlink($file);
|
||||
@ -73,7 +71,6 @@ class FilesystemAdapter extends AbstractAdapter
|
||||
} else {
|
||||
$i = rawurldecode(rtrim(fgets($h)));
|
||||
$value = stream_get_contents($h);
|
||||
flock($h, LOCK_UN);
|
||||
fclose($h);
|
||||
if ($i === $id) {
|
||||
$values[$id] = unserialize($value);
|
||||
@ -130,13 +127,15 @@ class FilesystemAdapter extends AbstractAdapter
|
||||
{
|
||||
$ok = true;
|
||||
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX;
|
||||
$tmp = $this->directory.uniqid('', true);
|
||||
|
||||
foreach ($values as $id => $value) {
|
||||
$file = $this->getFile($id, true);
|
||||
|
||||
$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
|
||||
if (false !== @file_put_contents($file, $value, LOCK_EX)) {
|
||||
@touch($file, $expiresAt);
|
||||
if (false !== @file_put_contents($tmp, $value)) {
|
||||
@touch($tmp, $expiresAt);
|
||||
$ok = @rename($tmp, $file) && $ok;
|
||||
} else {
|
||||
$ok = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user