数据库连接缓存问题
我正在使用脚本来静态缓存执行的 DB 和 PDO 语句。
在这里,
<?php
require_once 'ExceptionHandler.php';
final class Database {
private static $db = "test";
private static $host = "localhost";
private static $username = "root";
private static $password = "";
private static $dbConn = null;
private static $queryCatch = array();
private static function CONNECT()
{
if(self::$dbConn === null)
{
$connUrl = "mysql:host=".self::$host.";dbname=".self::$db;
self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT => true));
}
}
public static function query($sql)
{
Database::CONNECT();
if(isset(self::$queryCatch[$sql]) && is_object(self::$queryCatch[$sql]))
{
$query = self::$queryCatch[$sql];
}
else
{
$query = self::$dbConn->prepare($sql);
self::$queryCatch[$sql] = $query;
}
$numargs = func_num_args();
$arg_list = func_get_args();
//start from 1st parameter as 0th parameter is the query
for ($i = 1; $i < $numargs; $i++) {
if(is_int($arg_list[$i]))
{
$query->bindParam($i,$arg_list[$i],PDO::PARAM_INT);
}
else
{
$query->bindParam($i,$arg_list[$i],PDO::PARAM_STR);
}
}
$query->execute();
return $query;
}
}
?>
我想不仅在页面缓存中静态地进行此操作,而且在全局 $_SESSION 缓存中进行
此操作,但是对我的连接方法进行以下更改并不能帮助
private static function CONNECT()
{
if(self::$dbConn === null)
{
if(isset($_SESSION['X_DB_CONN']))
{
self::$dbConn = $_SESSION['X_DB_CONN'];echo "session cache hit";
}
else
{
$connUrl = "mysql:host=".self::$host.";dbname=".self::$db;
self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT => true));
$_SESSION['X_DB_CONN'] = self::$dbConn;
if(isset($_SESSION['test']))
{
echo ":)";
$_SESSION['test'] = "OO";
}
echo "session cache NOT hit";
}
}
}
我正确启动会话。 提供证据并说明我的问题:
$_SESSION['test'] 从另一个页面设置为“:)”。和 :) 是本页 if 语句的输出。
永远比德斯
会话缓存未命中
显示
这是标准 php 错误控制台的输出
致命错误:抛出异常而没有 第 0 行未知的堆栈帧
我已经包含了 session_start() 所以这不应该是问题
I am using a script to cache statically DB and PDO statements executed.
here it is
<?php
require_once 'ExceptionHandler.php';
final class Database {
private static $db = "test";
private static $host = "localhost";
private static $username = "root";
private static $password = "";
private static $dbConn = null;
private static $queryCatch = array();
private static function CONNECT()
{
if(self::$dbConn === null)
{
$connUrl = "mysql:host=".self::$host.";dbname=".self::$db;
self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT => true));
}
}
public static function query($sql)
{
Database::CONNECT();
if(isset(self::$queryCatch[$sql]) && is_object(self::$queryCatch[$sql]))
{
$query = self::$queryCatch[$sql];
}
else
{
$query = self::$dbConn->prepare($sql);
self::$queryCatch[$sql] = $query;
}
$numargs = func_num_args();
$arg_list = func_get_args();
//start from 1st parameter as 0th parameter is the query
for ($i = 1; $i < $numargs; $i++) {
if(is_int($arg_list[$i]))
{
$query->bindParam($i,$arg_list[$i],PDO::PARAM_INT);
}
else
{
$query->bindParam($i,$arg_list[$i],PDO::PARAM_STR);
}
}
$query->execute();
return $query;
}
}
?>
I wanted to make this not just statically in page caching but a global $_SESSION caching
But the following changed to my connect method is not helping
private static function CONNECT()
{
if(self::$dbConn === null)
{
if(isset($_SESSION['X_DB_CONN']))
{
self::$dbConn = $_SESSION['X_DB_CONN'];echo "session cache hit";
}
else
{
$connUrl = "mysql:host=".self::$host.";dbname=".self::$db;
self::$dbConn = new PDO($connUrl,self::$username,self::$password,array(PDO::ATTR_PERSISTENT => true));
$_SESSION['X_DB_CONN'] = self::$dbConn;
if(isset($_SESSION['test']))
{
echo ":)";
$_SESSION['test'] = "OO";
}
echo "session cache NOT hit";
}
}
}
I have started the session properly.
To Give proof and stating my problem:
The $_SESSION['test'] is set from another page to ":)". And
:)
is the output from the if statement on this page.
Beides always
session cache NOT hit
is displayed
This is the output by the standard php error console
Fatal error: Exception thrown without
a stack frame in Unknown on line 0
I have included the session_start() so that shouldn't be the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能以这种方式缓存对象、资源或处理程序。因为这是指向系统资源的指针(或链接),而不是实际的资源。所以当你缓存它的时候,你缓存了链接,当然刷新后链接就会被破坏。
PDO::ATTR_PERSISTENT => PDO::ATTR_PERSISTENT => true 就足够了。
You can't cache objects, resourses or handlers this way. Because this is pointer (or link) to the system resource, not an actual resource. So when when you cache it, you cache link, and of course after refresh link will be broken.
PDO::ATTR_PERSISTENT => true
would be really enough.