2010-08-17 141 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將確保您的主要連接保持當前連接。