changeset 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 eb82a7ddf5bd
children ab06d838d916
files index.php
diffstat 1 files changed, 11 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Sat Jun 04 04:35:58 2011 +0300
+++ b/index.php	Mon Sep 05 10:54:47 2011 +0300
@@ -433,25 +433,17 @@
 
 // Dump the course data cache, but only if it has changed
 if ($cacheDirty) {
-  // First try append mode
-  $fp = @fopen($cacheFile, "rb+");
-
-  // If file didn't exist, try write mode
-  if (!$fp)
-    $fp = @fopen($cacheFile, "wb");
-
-  if ($fp) {
-    // Use locking to prevent concurrent access and dump data
-    if (flock($fp, LOCK_EX)) {
-      ftruncate($fp, 0);
-      fwrite($fp, "<?\n\$cache = array(\n");
-      foreach ($cache as $id => $data) {
-        fwrite($fp, "  \"".addslashes($id)."\" => array(\"desc\" => \"".
-        addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n");
-      }
-      fwrite($fp, ");\n?>");
-    }
-    fclose($fp);
+  // Create string containing the data
+  $str = "<?\n\$cache = array(\n";
+  foreach ($cache as $id => $data) {
+    $str .= "  \"".addslashes($id)."\" => array(\"desc\" => \"".
+            addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n";
+  }
+  $str .= ");\n?>";
+  
+  // Use locking to prevent concurrent access and dump data
+  if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) {
+    // Can't do much anything here ..
   }
 }