我无法真正提供确切的代码,因此我会尽力说明问题。首先,我有几个可以正常连接和工作的数据库连接($local、$remote)。我将这些连接传递到一个名为 dbReader 的类中,将它们存储在对象中,最后将对象存储在 APC 中:
class dbReader {
public $APC;
public $local;//Local DB
public $remote;//Remote DB
public function __construct($local,$remote){
ePrint($IMX);
$this->APC = new CacheAPC();//APC Object
$this->local = $local;
$this->remote = $remote;
//Store dbreader in APC for reference:
$this->storeDBReader();
}
public function getRunTypes(){
//Query APC:
$array = $this->APC->getData('RunTypes');
//Populate APC:
if(!$array){
$array = array();
$query = ...;
$result = mysql_query($query,$this->remote);
while($row = mysql_fetch_array($result)){
$array[$row['ID']] = $row['Val'];
}
$this->APC->setData('RunTypes',$array);
}
return $array;
}
public function storeDBReader(){
//Store dbReader in APC for reference:
$this->APC->setData('dbReader',$this);
}
}
整个过程中连接链接都很好,直到我从 APC 恢复 dbReader 对象并尝试调用 getRunTypes() 函数。我认为当对象存储在 APC 中时,mySQL 链接会被破坏。这是真的?有什么建议吗?
最佳答案
您无法序列化资源,并且存储在 APC 中会自动序列化对象。
这就是神奇的方法__sleep()和 __wakeup()用于:它们基本上是预序列化和后反序列化 Hook ,当您存储然后从 apc 检索对象时,您可以使用它们来取消设置和重置数据库连接。
关于存储在 APC 中的 php 对象删除 mySQL 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9383352/