From 44f6d478bb95e3249f4bd3fb96d47ccb79e1e6dc Mon Sep 17 00:00:00 2001 From: liugang Date: Wed, 15 May 2024 18:08:49 +0800 Subject: [PATCH] * baseDAO: refactor the fetchPairs method to use cache. --- lib/base/dao/dao.class.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/base/dao/dao.class.php b/lib/base/dao/dao.class.php index 3e25d422a0..7e00a02fd9 100644 --- a/lib/base/dao/dao.class.php +++ b/lib/base/dao/dao.class.php @@ -1063,19 +1063,24 @@ class baseDAO */ public function fetchPairs($keyField = '', $valueField = '') { + $sql = $this->processSQL(); + $key = 'fetchPairs-' . md5($sql); + + $rows = $this->getCache($key, $sql); + if($rows === false) + { + $rows = $this->query($sql)->fetchAll(); + $this->setCache($key, $rows); + } + + $ready = false; $keyField = trim($keyField, '`'); $valueField = trim($valueField, '`'); - $sql = $this->processSQL(); - $table = $this->table; - $key = 'fetchPairs-' . md5($sql . $keyField . $valueField); - if(isset(dao::$cache[$table][$key])) return dao::$cache[$table][$key]; - - $pairs = array(); - $ready = false; - $stmt = $this->query($sql); - while($row = $stmt->fetch(PDO::FETCH_ASSOC)) + $result = array(); + foreach($rows as $row) { + $row = (array)$row; if(!$ready) { if(empty($keyField)) $keyField = key($row); @@ -1087,11 +1092,10 @@ class baseDAO $ready = true; } - $pairs[$row[$keyField]] = $row[$valueField]; + $result[$row[$keyField]] = $row[$valueField]; } - dao::$cache[$table][$key] = $pairs; - return $pairs; + return $result; } /**