我如何存储/序列化 php 返回的 mysqli_query 结果?
How do i store / serialize php returned mysqli_query results?
我正在尝试将从 mysqli_query 返回的结果存储为缓存,因此如果执行相同的“SELECT”语句,它将从缓存中获取而不是通过 mysql服务器但是下面的代码,无论我尝试使用序列化/json_encode、解码甚至只是将输出存储为变量,我都做不到
$row = $this->result->fetch_object();
代码如下。有谁知道为什么?我如何存储返回的结果并重新使用它?它不适用于 $this->result = mysqli_query($dbh, $query);
global $dbcache;
// $this->result = mysqli_query( $dbh, $query );
if (isset($dbcache[$query])){
$this->result = json_decode($dbcache[$query]);
// echo 'JSON? = '.$query.'<br>'.$dbcache[$query];
}else{
// echo ' === '.$dbcache[$query].' === '."\n";
$this->result = mysqli_query( $dbh, $query );
//$dbcache[$query] = serialize($this->result);
$dbcache[$query] = json_encode($this->result);
// echo 'Serialize? = '.$query."\n".hash('sha3-512' , $query).'<br>';
}
您存储 mysqli_result
对象的时间不应超过获取数据所需的时间。它仅用作从 MySQL 获取数据的临时对象。改为缓存值。
if (isset($dbcache[$query])) {
$this->result = $dbcache[$query];
} else {
$dbcache[$query] = $this->result = $dbh->query($query)->fetch_all(MYSQLI_ASSOC);
}
但是,我会担心这种方法对准备好的语句有多大用处。您可能希望将键作为查询和参数的组合。例如:
$key = serialize([$query, ...$params]);
if (isset($dbcache[$key])) {
$this->result = $dbcache[$key];
} else {
//prepare bind execute
$stmt = $dbh->prepare($query);
if ($params) {
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
}
$stmt->execute();
// Fetch results into multidimensional array
$dbcache[$key] = $this->result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
您真的应该从应用程序代码中使用 mysqli 函数中抽象出来。如果您正在构建某种抽象层,那么您应该只 return 将数据提供给应用程序并仅在内部使用 mysqli。使用我推荐的方法,您将以普通数组的形式访问数据;没有更多 fetch_object()
方法。当然,这需要您更改应用程序代码,但这样做是个好主意。我也强烈建议开始使用 PDO。
我正在尝试将从 mysqli_query 返回的结果存储为缓存,因此如果执行相同的“SELECT”语句,它将从缓存中获取而不是通过 mysql服务器但是下面的代码,无论我尝试使用序列化/json_encode、解码甚至只是将输出存储为变量,我都做不到
$row = $this->result->fetch_object();
代码如下。有谁知道为什么?我如何存储返回的结果并重新使用它?它不适用于 $this->result = mysqli_query($dbh, $query);
global $dbcache;
// $this->result = mysqli_query( $dbh, $query );
if (isset($dbcache[$query])){
$this->result = json_decode($dbcache[$query]);
// echo 'JSON? = '.$query.'<br>'.$dbcache[$query];
}else{
// echo ' === '.$dbcache[$query].' === '."\n";
$this->result = mysqli_query( $dbh, $query );
//$dbcache[$query] = serialize($this->result);
$dbcache[$query] = json_encode($this->result);
// echo 'Serialize? = '.$query."\n".hash('sha3-512' , $query).'<br>';
}
您存储 mysqli_result
对象的时间不应超过获取数据所需的时间。它仅用作从 MySQL 获取数据的临时对象。改为缓存值。
if (isset($dbcache[$query])) {
$this->result = $dbcache[$query];
} else {
$dbcache[$query] = $this->result = $dbh->query($query)->fetch_all(MYSQLI_ASSOC);
}
但是,我会担心这种方法对准备好的语句有多大用处。您可能希望将键作为查询和参数的组合。例如:
$key = serialize([$query, ...$params]);
if (isset($dbcache[$key])) {
$this->result = $dbcache[$key];
} else {
//prepare bind execute
$stmt = $dbh->prepare($query);
if ($params) {
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
}
$stmt->execute();
// Fetch results into multidimensional array
$dbcache[$key] = $this->result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
您真的应该从应用程序代码中使用 mysqli 函数中抽象出来。如果您正在构建某种抽象层,那么您应该只 return 将数据提供给应用程序并仅在内部使用 mysqli。使用我推荐的方法,您将以普通数组的形式访问数据;没有更多 fetch_object()
方法。当然,这需要您更改应用程序代码,但这样做是个好主意。我也强烈建议开始使用 PDO。