* adjust for cache dao.

This commit is contained in:
wangyidong
2016-08-05 09:47:34 +08:00
parent e622618140
commit 2b4b2cb10e
2 changed files with 42 additions and 9 deletions
+40 -7
View File
@@ -684,18 +684,18 @@ class baseDAO
if($this->slaveDBH and $method == 'select')
{
if(isset(dao::$cache[$key])) return dao::$cache[$key];
if(isset(dao::$cache['query'][$key])) return dao::$cache['query'][$key];
$result = $this->slaveDBH->query($sql);
dao::$cache[$key] = $result;
dao::$cache['query'][$key] = $result;
return $result;
}
else
{
if($this->method == 'select')
{
if(isset(dao::$cache[$key])) return dao::$cache[$key];
if(isset(dao::$cache['query'][$key])) return dao::$cache['query'][$key];
$result = $this->slaveDBH->query($sql);
dao::$cache[$key] = $result;
dao::$cache['query'][$key] = $result;
return $result;
}
@@ -773,10 +773,23 @@ class baseDAO
*/
public function fetch($field = '')
{
if(empty($field)) return $this->query()->fetch();
$sql = $this->processSQL();
$key = md5($sql);
if(isset(dao::$cache['fetch'][$key][$field])) return dao::$cache['fetch'][$key][$field];
if(empty($field))
{
$result = $this->query()->fetch();
dao::$cache['fetch'][$key][$field] = $result;
return $result;
}
$this->setFields($field);
$result = $this->query()->fetch(PDO::FETCH_OBJ);
if($result) return $result->$field;
if($result)
{
dao::$cache['fetch'][$key][$field] = $result->$field;
return $result->$field;
}
}
/**
@@ -790,10 +803,20 @@ class baseDAO
*/
public function fetchAll($keyField = '')
{
$sql = $this->processSQL();
$key = md5($sql);
if(isset(dao::$cache['fetchAll'][$key][$keyField])) return dao::$cache['fetchAll'][$key][$keyField];
$stmt = $this->query();
if(empty($keyField)) return $stmt->fetchAll();
if(empty($keyField))
{
$result = $stmt->fetchAll();
dao::$cache['fetchAll'][$key][$keyField] = $result;
return $result;
}
$rows = array();
while($row = $stmt->fetch()) $rows[$row->$keyField] = $row;
dao::$cache['fetchAll'][$key][$keyField] = $rows;
return $rows;
}
@@ -808,12 +831,17 @@ class baseDAO
*/
public function fetchGroup($groupField, $keyField = '')
{
$sql = $this->processSQL();
$key = md5($sql);
if(isset(dao::$cache['fetchGroup'][$key][$groupField][$keyField])) return dao::$cache['fetchGroup'][$key][$groupField][$keyField];
$stmt = $this->query();
$rows = array();
while($row = $stmt->fetch())
{
empty($keyField) ? $rows[$row->$groupField][] = $row : $rows[$row->$groupField][$row->$keyField] = $row;
}
dao::$cache['fetchGroup'][$key][$groupField][$keyField] = $rows;
return $rows;
}
@@ -834,6 +862,10 @@ class baseDAO
$keyField = trim($keyField, '`');
$valueField = trim($valueField, '`');
$sql = $this->processSQL();
$key = md5($sql);
if(isset(dao::$cache['fetchPairs'][$key][$keyField][$valueField])) return dao::$cache['fetchPairs'][$key][$keyField][$valueField];
$pairs = array();
$ready = false;
$stmt = $this->query();
@@ -852,6 +884,7 @@ class baseDAO
$pairs[$row[$keyField]] = $row[$valueField];
}
dao::$cache['fetchPairs'][$key][$keyField][$valueField] = $pairs;
return $pairs;
}