2010-08-17 51 views
1

我在Zend Framework中使用Doctrine 1.2,它工作得很好。现在我想开始使用结果缓存和查询缓存功能来减少为应用程序提供动力的数据库服务器上的负载。哦,我也在使用Zend_Application并将代码放在模块中。Zend Framework,Doctrine和缓存问题

反正我设置在应用程序本身整体的bootstrap.php文件我的教义连接:

protected function _initDoctrine() 
{ 
    Zend_Loader_Autoloader::getInstance()->registerNamespace('Doctrine')->pushAutoloader(array('Doctrine', 'autoload')); 

    $manager = Doctrine_Manager::getInstance(); 

    foreach ($this->_options['doctrine']['attr'] as $key => $val) { 
     $manager->setAttribute(constant("Doctrine::$key"), $val); 
    } 

    $conn = Doctrine_Manager::connection($this->_options['doctrine']['dsn'], 'doctrine'); 

    Doctrine::loadModels($this->_options["doctrine"]["module_directories"]); 
} 

现在,我花了一些时间阅读这两个查询和结果缓存的文档主义寻找使用Doctrine的缓存的例子。我发现的东西看起来非常简单。我将此代码添加到的bootstrap.php文件,其中包含_initDoctrine()方法,里面_initDoctrine()

// Set up some caching 
    $cacheConn = Doctrine_Manager::connection(new PDO('sqlite::memory:')); 
    $cacheDriver = new Doctrine_Cache_Db(array(
     'connection' => $cacheConn, 
     'tableName' => 'cache')); 
    $cacheDriver->createTable(); 
    $manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver); 

什么,我现在发现情况是,应用程序的整个连接现在认为它改为使用SQLite的MySQL像它应该的。这似乎很奇怪,因为我只设置了ATTR_QUERY_CACHE属性。

解决此问题的提示将不胜感激。

回答

0

根据Twitter的反馈,决定使用APC作为后端而不是SQLite。

1

有一个解决方案。每次使用静态连接()方法时,都会将其设置为当前连接。为了防止发生这种情况,您必须使用以下代码:

$manager = Doctrine_Manager::getInstance(); 
$cacheConn = $manager->openConnection(new PDO('sqlite::memory:'), 'cache', false); 

这里的重要部分是openConnection方法的第三个参数。如果设置为true,则会使其成为当前连接。将其设置为false将确保您的主要连接保持当前连接。