php存储在APC中的对象drop mySQL链接

php存储在APC中的对象drop mySQL链接

问题描述:

我真的无法提供确切的代码,所以我会尽我所能来说明问题。首先,我有几个数据库连接,可以连接并正常工作($ local,$ remote)。我路过那些连接到一个名为dbReader类,存储在他们的对象,最后存储APC对象:}php存储在APC中的对象drop mySQL链接

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链接被销毁。这是真的?有什么建议么?

+3

您无法在APC中存储连接 - 您需要重新建立链接。 – 2012-02-21 18:53:26

+1

一般而言,在串行化并将某些内容放入缓存之前,您应该断开连接,然后在您将某些内容从缓存中取出并反序列化时重新连接。 – 2012-02-21 18:54:25

+0

感谢您的输入。 Erik,如果你想回答你的评论,我会接受它。 – jreed121 2012-02-21 19:06:23

您不能序列化资源,并且存储在APC中会自动序列化对象。

这是什么神奇的方法__sleep()__wakeup()是:他们基本上都是预先序列化和反序列化后的钩子,你可以用它来取消设置和重置数据库连接,当你存储并检索来自APC的对象。

+0

很高兴知道,我会在将来的应用中牢记这一点。现在,我只是将连接存储在$ GLOBALS中,我知道这是亵渎,但它起作用,需要更少的代码,并且在这种情况下没有缺点。 – jreed121 2012-02-21 22:31:32