protected function doSet($key, $value, $timeout = 0)
{
$path = $this->getPath($key);
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
if ($this->useJson) {
$value = json_encode($value);
} else {
$value = '<' . "?php \nreturn " . var_export($value, true) . ";\n";
}
$h = fopen($path, 'w');
if (!$h) {
return false;
}
$maxTries = 400;
//wait max. 2 seconds, otherwise force it
$tries = 0;
while (!flock($h, LOCK_EX) and $tries <= $maxTries) {
usleep(1000 * 5);
//5ms
$tries++;
}
fwrite($h, $value);
flock($h, LOCK_UN);
fclose($h);
return true;
}