Skip to content

Commit 7fd7a2e

Browse files
committed
Merge pull request #163 from arcfieldOSS/final
Remove race by journaling writes; misc
2 parents 1f20474 + 77485e3 commit 7fd7a2e

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

phpfastcache/3.0.0/abstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,4 @@ protected function __setChmodAuto() {
428428
}
429429

430430

431-
}
431+
}

phpfastcache/3.0.0/drivers/files.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private function getFilePath($keyword, $skip = false) {
7070

7171
function driver_set($keyword, $value = "", $time = 300, $option = array() ) {
7272
$file_path = $this->getFilePath($keyword);
73+
$tmp_path = $file_path . ".tmp";
7374
// echo "<br>DEBUG SET: ".$keyword." - ".$value." - ".$time."<br>";
7475
$data = $this->encode($value);
7576

@@ -86,22 +87,33 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) {
8687
}
8788
}
8889

89-
if($toWrite == true) {
90+
$written = true;
91+
/*
92+
* write to intent file to prevent race during read; race during write is ok
93+
* because first-to-lock wins and the file will exist before the writer attempts
94+
* to write.
95+
*/
96+
if($toWrite == true && !@file_exists($tmp_path && !@file_exists($file_path))) {
9097
try {
91-
$f = @fopen($file_path, "c");
92-
if (flock($f,LOCK_EX| LOCK_NB)) { //got a lock to write;
93-
fwrite($f, $data);
94-
fflush($f);
95-
flock($f,LOCK_UN);
98+
$f = @fopen($tmp_path, "c");
99+
if ($f) {
100+
if (flock($f,LOCK_EX| LOCK_NB)) {
101+
$written = ($written && fwrite($f, $data));
102+
$written = ($written && fflush($f));
103+
$written = ($written && flock($f, LOCK_UN));
96104
} else {
97105
//arguably the file is being written to so the job is done
106+
$written = false;
107+
}
108+
$written = ($written && @fclose($f));
109+
$written = ($written && @rename($tmp_path,$file_path));
98110
}
99-
fclose($f);
100111
} catch (Exception $e) {
101112
// miss cache
102-
return false;
113+
$written = false;
103114
}
104115
}
116+
return $written;
105117
}
106118

107119
function driver_get($keyword, $option = array()) {

phpfastcache/3.0.0/phpfastcache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public static function getPath($skip_create_path = false, $config) {
159159
$path = $config['path'];
160160
}
161161

162-
$securityKey = $config['securityKey'];
162+
$securityKey = array_key_exists('securityKey',$config) ? $config['securityKey'] : "";
163163
if($securityKey == "" || $securityKey == "auto") {
164164
$securityKey = self::$config['securityKey'];
165165
if($securityKey == "auto" || $securityKey == "") {
@@ -193,7 +193,7 @@ public static function getPath($skip_create_path = false, $config) {
193193

194194

195195
self::$tmp[$full_pathx] = true;
196-
self::htaccessGen($full_path, $config['htaccess']);
196+
self::htaccessGen($full_path, array_key_exists('htaccess',$config) ? $config['htaccess'] : false);
197197
}
198198

199199
return realpath($full_path);

0 commit comments

Comments
 (0)