Updating pointerUpdate feature/dataAbstractCaching
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 21 Feb 2019 21:53:27 +0000 (16:53 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 21 Feb 2019 21:53:27 +0000 (16:53 -0500)
setup caching

lib/GlmDataAbstract/DataAbstract.php

index 96b4a17..2b01924 100755 (executable)
@@ -592,7 +592,7 @@ abstract class GlmDataAbstract
     }
     private function pointerOutput($f, $d, $forEdit = false, $id = false, $idfield = 'id')
     {
-
+        static $pointerCache = array();
         /*
          * This function will only return the value of the pointer field unless one
          * of the following situations occurs.
@@ -647,14 +647,21 @@ abstract class GlmDataAbstract
 
             } else {
 
-                $sql = "
-                    SELECT ".$p_id." AS p_id,
-                           ".$f['p_field']." AS p_value
-                      FROM ".$f['p_table']."
-                           $where
-                     ORDER BY $order_by
-                ";
-                $p_list = $this->wpdb->get_results($sql, ARRAY_A);
+                // check the pointerCache
+                if ( isset( $pointerCache['p_list'][$f['p_table']][$f['p_field']][$p_id] ) ) {
+                    $p_list = $pointerCache['p_list'][$f['p_table']][$f['p_field']][$p_id];
+                } else {
+                    $sql = "
+                        SELECT ".$p_id." AS p_id,
+                               ".$f['p_field']." AS p_value
+                          FROM ".$f['p_table']."
+                               $where
+                         ORDER BY $order_by
+                    ";
+                    $p_list = $this->wpdb->get_results($sql, ARRAY_A);
+                }
+                // store results in cache
+                $pointerCache['p_list'][$f['p_table']][$f['p_field']][$p_id] = $p_list;
 
                 // Build pick select table
                 $pick_select = false;
@@ -679,10 +686,10 @@ abstract class GlmDataAbstract
                     foreach ($p_list as $p) {
 
                         $pick_list[$p['p_id']] = array(
-                                   'value'   => $p['p_id'],
-                                   'name'    => $p['p_value'],
-                                   'nameEsc' => addslashes($p['p_value']),
-                                   'default' => ($p['p_id'] == $p_value)
+                            'value'   => $p['p_id'],
+                            'name'    => $p['p_value'],
+                            'nameEsc' => addslashes($p['p_value']),
+                            'default' => ($p['p_id'] == $p_value)
                            );
 
                         // Check for selected option
@@ -701,10 +708,10 @@ abstract class GlmDataAbstract
                 }
 
                 $r = array(
-                    'value'     => $p_value,
-                    'name'      => $selected_name,
-                    'nameEsc'   => addslashes($selected_name),
-                    'list' => $pick_list
+                    'value'   => $p_value,
+                    'name'    => $selected_name,
+                    'nameEsc' => addslashes($selected_name),
+                    'list'    => $pick_list
                 );
 
                 return $r;
@@ -717,6 +724,12 @@ abstract class GlmDataAbstract
             // Get pointer value, ensure that's it's an integer
             $p_value = ($d - 0);
 
+            // check the pointerCache
+            if ( isset( $pointerCache['default'][$f['p_table']][$f['p_field']][$p_id][$p_value] ) ) {
+                $res = $pointerCache['default'][$f['p_table']][$f['p_field']][$p_id][$p_value];
+                return $res[$f['p_field']];
+            }
+
             $sql = "
                 SELECT ".$f['p_field']."
                   FROM ".$f['p_table']."
@@ -724,6 +737,8 @@ abstract class GlmDataAbstract
                    AND $p_id = $p_value
             ";
             $res = $this->wpdb->get_row($sql, ARRAY_A);
+            // store results in cache
+            $pointerCache['default'][$f['p_table']][$f['p_field']][$p_id][$p_value] = $res;
 
             return $res[$f['p_field']];
 
@@ -733,6 +748,7 @@ abstract class GlmDataAbstract
     }
     private function pointerInput($as, $f, $id, $idfield, $op)
     {
+        static $pointerCache = array();
 
         $this->inputFieldStatus = true;