Browse Source

Optimize updates in workerman and php-ngx (#5253)

* Optimize updates

* Add pdo

* Add array

* Small change to rerun travis

* Foreach correct
Joan Miquel 5 years ago
parent
commit
d8981fe09d
2 changed files with 16 additions and 10 deletions
  1. 8 5
      frameworks/PHP/php-ngx/app.php
  2. 8 5
      frameworks/PHP/workerman/app.php

+ 8 - 5
frameworks/PHP/php-ngx/app.php

@@ -39,7 +39,7 @@ function query()
 
 
 function update()
 function update()
 {
 {
-    global $random, $update;
+    global $pdo, $random, $update;
     ngx_header_set('Content-Type', 'application/json');
     ngx_header_set('Content-Type', 'application/json');
 
 
     $query_count = 1;
     $query_count = 1;
@@ -52,13 +52,16 @@ function update()
         $random->execute([$id]);
         $random->execute([$id]);
 
 
         $world = ['id' => $id, 'randomNumber' => $random->fetchColumn()];
         $world = ['id' => $id, 'randomNumber' => $random->fetchColumn()];
-        $update->execute(
-            [$world['randomNumber'] = mt_rand(1, 10000), $id]
-        );
-
+        $world['randomNumber'] = mt_rand(1, 10000);
         $arr[] = $world;
         $arr[] = $world;
     }
     }
 
 
+    $pdo->beginTransaction();
+    foreach($arr as $world) {
+        $update->execute([$world['randomNumber'], $world['id']]);
+    }
+    $pdo->commit();
+
     echo json_encode($arr, JSON_NUMERIC_CHECK);
     echo json_encode($arr, JSON_NUMERIC_CHECK);
 }
 }
 
 

+ 8 - 5
frameworks/PHP/workerman/app.php

@@ -26,7 +26,7 @@ function query()
 
 
 function updateraw()
 function updateraw()
 {
 {
-    global $random, $update;
+    global $pdo, $random, $update;
     $query_count = 1;
     $query_count = 1;
     if ($_GET['q'] > 1) {
     if ($_GET['q'] > 1) {
         $query_count = min($_GET['q'], 500);
         $query_count = min($_GET['q'], 500);
@@ -36,13 +36,16 @@ function updateraw()
         $id = mt_rand(1, 10000);
         $id = mt_rand(1, 10000);
         $random->execute([$id]);
         $random->execute([$id]);
         $world = ['id' => $id, 'randomNumber' => $random->fetchColumn()];
         $world = ['id' => $id, 'randomNumber' => $random->fetchColumn()];
-        
-        $update->execute(
-            [$world['randomNumber'] = mt_rand(1, 10000), $id]
-        );
+        $world['randomNumber'] = mt_rand(1, 10000);
 
 
         $arr[] = $world;
         $arr[] = $world;
     }
     }
+    
+    $pdo->beginTransaction();
+    foreach($arr as $world) {
+        $update->execute([$world['randomNumber'], $world['id']]);
+    }
+    $pdo->commit();
 
 
     return json_encode($arr, JSON_NUMERIC_CHECK);
     return json_encode($arr, JSON_NUMERIC_CHECK);
 }
 }