【问题标题】:Zend Framework, Doctrine and Caching ProblemsZend 框架、原则和缓存问题
【发布时间】:2010-08-17 18:43:31
【问题描述】:

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

无论如何,我在整个 Bootstrap.php 文件中为应用程序本身设置了与 Doctrine 的连接:

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 的查询和结果缓存文档寻找使用 Doctrine 缓存的示例。我发现的似乎非常简单。我将此代码添加到包含 _initDoctrine() 方法的 Bootstrap.php 文件中,在 _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 属性。

我们将不胜感激有关解决此问题的提示。

【问题讨论】:

    标签: zend-framework caching doctrine


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-28
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        相关资源
        最近更新 更多