开发者

Zend Framework, Doctrine and Caching Problems

开发者 https://www.devze.com 2023-01-12 12:24 出处:网络
I\'m using Doctrine 1.2 with Zend Framework, and it\'s working just fine.Now I want to start using the results cache and query caching capabilities to reduce the load on DB server that powers the appl

I'm using Doctrine 1.2 with Zend Framework, and it's working just fine. Now I want to start using the results cache and query caching capabilities to reduce the load on DB server that powers the application. Oh, I'm also using Zend_Application and placin开发者_StackOverflowg code in modules.

Anyway, I setup my connections to Doctrine in the overall Bootstrap.php file for the application itself:

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"]);  
}

Now, I spent some time reading both the documentation for query and result caching for Doctrine and looking for examples of using Doctrine's caching. What I found appears to be very straightforward. I added this code to the Bootstrap.php file that contains the _initDoctrine() method, inside _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);

What I am now finding happening is that the entire connection for the application now thinks it is using Sqlite instead of MySQL like it is supposed to. Which seems odd, since I am only setting the ATTR_QUERY_CACHE attribute.

Tips on resolving this would be greatly appreciated.


There is a solution to this. Every time you use the static connection() method it will set that as the current connection. In order to stop this from happening you must use the following code:

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

The important part here is the third argument of the openConnection method. If set to true it will make it the current connection. Setting it to false will ensure your main connection stays as the current connection.


Decided to go with APC as the backend instead of SQLite, based on feedback from Twitter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号