comparison index.php @ 27:7655db03ea60

Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Sep 2011 10:54:47 +0300
parents b4b86915ae2c
children ab06d838d916
comparison
equal deleted inserted replaced
26:eb82a7ddf5bd 27:7655db03ea60
431 printPageFooter(); 431 printPageFooter();
432 432
433 433
434 // Dump the course data cache, but only if it has changed 434 // Dump the course data cache, but only if it has changed
435 if ($cacheDirty) { 435 if ($cacheDirty) {
436 // First try append mode 436 // Create string containing the data
437 $fp = @fopen($cacheFile, "rb+"); 437 $str = "<?\n\$cache = array(\n";
438 438 foreach ($cache as $id => $data) {
439 // If file didn't exist, try write mode 439 $str .= " \"".addslashes($id)."\" => array(\"desc\" => \"".
440 if (!$fp) 440 addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n";
441 $fp = @fopen($cacheFile, "wb"); 441 }
442 442 $str .= ");\n?>";
443 if ($fp) { 443
444 // Use locking to prevent concurrent access and dump data 444 // Use locking to prevent concurrent access and dump data
445 if (flock($fp, LOCK_EX)) { 445 if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) {
446 ftruncate($fp, 0); 446 // Can't do much anything here ..
447 fwrite($fp, "<?\n\$cache = array(\n");
448 foreach ($cache as $id => $data) {
449 fwrite($fp, " \"".addslashes($id)."\" => array(\"desc\" => \"".
450 addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n");
451 }
452 fwrite($fp, ");\n?>");
453 }
454 fclose($fp);
455 } 447 }
456 } 448 }
457 449
458 ?> 450 ?>