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')) {
|
if (!$h = @fopen($file, 'rb')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
flock($h, LOCK_SH);
|
|
||||||
if ($now >= (int) $expiresAt = fgets($h)) {
|
if ($now >= (int) $expiresAt = fgets($h)) {
|
||||||
flock($h, LOCK_UN);
|
|
||||||
fclose($h);
|
fclose($h);
|
||||||
if (isset($expiresAt[0])) {
|
if (isset($expiresAt[0])) {
|
||||||
@unlink($file);
|
@unlink($file);
|
||||||
@ -73,7 +71,6 @@ class FilesystemAdapter extends AbstractAdapter
|
|||||||
} else {
|
} else {
|
||||||
$i = rawurldecode(rtrim(fgets($h)));
|
$i = rawurldecode(rtrim(fgets($h)));
|
||||||
$value = stream_get_contents($h);
|
$value = stream_get_contents($h);
|
||||||
flock($h, LOCK_UN);
|
|
||||||
fclose($h);
|
fclose($h);
|
||||||
if ($i === $id) {
|
if ($i === $id) {
|
||||||
$values[$id] = unserialize($value);
|
$values[$id] = unserialize($value);
|
||||||
@ -130,13 +127,15 @@ class FilesystemAdapter extends AbstractAdapter
|
|||||||
{
|
{
|
||||||
$ok = true;
|
$ok = true;
|
||||||
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX;
|
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX;
|
||||||
|
$tmp = $this->directory.uniqid('', true);
|
||||||
|
|
||||||
foreach ($values as $id => $value) {
|
foreach ($values as $id => $value) {
|
||||||
$file = $this->getFile($id, true);
|
$file = $this->getFile($id, true);
|
||||||
|
|
||||||
$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
|
$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
|
||||||
if (false !== @file_put_contents($file, $value, LOCK_EX)) {
|
if (false !== @file_put_contents($tmp, $value)) {
|
||||||
@touch($file, $expiresAt);
|
@touch($tmp, $expiresAt);
|
||||||
|
$ok = @rename($tmp, $file) && $ok;
|
||||||
} else {
|
} else {
|
||||||
$ok = false;
|
$ok = false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user