我应该在 Zend_Db 中的哪里实现缓存?

发布于 2024-09-27 06:44:29 字数 953 浏览 12 评论 0原文

我正在寻找在 Zend_Db 中实现缓存,没有任何本机方法可以向 Zend_Db 提供缓存,所以我想知道应该在哪里实现。

我查看了 Zend_Db_Table_Abstract (我在自定义 App_Model_DbTable_Abstract 中扩展了它),发现了一个受保护的方法 _fetch() ,它直接采用 Zend_Db_Table_Select 实例,看起来是适配器之前的最后一步。

我正在考虑重写此方法,序列化 $select 对象,散列它,最后缓存它,并检查提供的每个 $select 对象以返回缓存或最新的行集。

这是正确的做法吗?

这是我刚刚所做的:

class App_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
    protected function _fetch(Zend_Db_Table_Select $select)
    {
        $hashedQuery = sha1(serialize($select->__toString()));
        $cacheManager = Zend_Registry::get('Zend_Cache_Manager');
        $cache = $cacheManager->getCache('database');
        if (!($data = $cache->load($hashedQuery))) {
            $data = parent::_fetch($select);
            $cache->save($data, $hashedQuery);
        }
        return $data;
    }
}

I'm looking to implement a cache within Zend_Db, there isn't any native method to provide a cache to Zend_Db, so I'm wondering where should I do it.

I took a look to the Zend_Db_Table_Abstract (I'm extending it in a custom App_Model_DbTable_Abstract) and I found a protected method _fetch() which directly take a Zend_Db_Table_Select instance and looks like to be the last step before the adapter.

I was thinking override this method, serialize the $select object, hash it, and finally cache it, and check against each $select object provided to return the cache or an up-to-date rowset.

Is it a correct way to do?

Here is what I just did:

class App_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
    protected function _fetch(Zend_Db_Table_Select $select)
    {
        $hashedQuery = sha1(serialize($select->__toString()));
        $cacheManager = Zend_Registry::get('Zend_Cache_Manager');
        $cache = $cacheManager->getCache('database');
        if (!($data = $cache->load($hashedQuery))) {
            $data = parent::_fetch($select);
            $cache->save($data, $hashedQuery);
        }
        return $data;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2024-10-04 06:44:29

据我所知, zf create db-table 将始终创建一个继承 Zend_Db_Table_Abstract 的类,这将使您的提案难以管理。

此外,您正在耦合缓存和缓存。 ZF 的 db 模块,因此有人可能会争辩说,将缓存机制放在​​ dbtable 范围下是不正确的。例如:你不应该知道数据从哪里获取,但仍然能够缓存它,所以过程变成这样:

  1. 检查缓存中的数据,如果找到则提供服务
  2. 从X获取数据(可以是dbtable,也可以是Service ,一个 XML 文件,一些 JSON 等)
  3. 将数据保存在缓存中并提供数据

因此,尽管您的解决方案现在有意义,因为您只使用 dbtable 模型,但它可以放置在更合适的层中。我会查看 http://www.slideshare.net/weierophinney/playdoh -modelling-your-objects-1766001(围绕幻灯片#35)以获得实用的解决方案。

摘要: dbtable 模块和与之相关的代码应该始终与使用 db.

As far as I know, the zf create db-table <name> will always create a class inheriting Zend_Db_Table_Abstract which would make your proposal difficult to manage.

Besides, you're coupling the cache & db modules of ZF so one could argue that it's not right to place cache mechanisms under the dbtable scope. For example: you should not know where the data is being fetched from but still be able to cache it, so the process becomes this:

  1. check cache for data, serve if found
  2. fetch data from X (could be dbtable, could also be a Service, an XML-file, some JSON etc)
  3. save data in cache and serve data

So although your solution makes sense now since you're only using dbtable models, it could be placed in a more suiting layer. I would check out http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects-1766001 (around slide #35) for a practical solution.

Summary: the dbtable module and your code concerning it should always be about using the db.

§普罗旺斯的薰衣草 2024-10-04 06:44:29
    public function indexAction()
        {
            // action body
            $this->_helper->layout->setLayout('layout');

            $db = new Zend_Db_Adapter_Pdo_Mysql(array('host' => 'localhost',
                                  'username' => 'root',
                                  'password' => '',
                                      'dbname' => 'zendtest'));
            $sql  = "SELECT SQL_CALC_FOUND_ROWS "
                      . "       register.firstname, "
                      . "       register.lastname, "
                      . "       register.username, "
                      . "       register.password, "
                      . "       register.email, "
                      . "       register.city, "
                      . "       register.state, "
                      . "       register.contactno "     
                      . "  FROM register register "
                      . "  WHERE register.id = ? ";                               

                $result = $db->fetchall($sql,1);    

                        $result1 = "";
               $cache = Zend_Registry::get('cache');

                if(!$result1 = $cache->load('mydata2')) {
                    echo 'caching the data…..';
                    $cache->save($result, 'mydata2');
                   } else {
                    echo 'retrieving cache data…….';
                    Zend_Debug::dump($result1);
                  }
}

在 bootstrap 文件中定义缓存数组,然后使用 zend DB 从数据库获取数据并将其存储在缓存中...
下次您不需要从 db 获取数据。您可以轻松快速地从缓存中访问该数据。

    public function indexAction()
        {
            // action body
            $this->_helper->layout->setLayout('layout');

            $db = new Zend_Db_Adapter_Pdo_Mysql(array('host' => 'localhost',
                                  'username' => 'root',
                                  'password' => '',
                                      'dbname' => 'zendtest'));
            $sql  = "SELECT SQL_CALC_FOUND_ROWS "
                      . "       register.firstname, "
                      . "       register.lastname, "
                      . "       register.username, "
                      . "       register.password, "
                      . "       register.email, "
                      . "       register.city, "
                      . "       register.state, "
                      . "       register.contactno "     
                      . "  FROM register register "
                      . "  WHERE register.id = ? ";                               

                $result = $db->fetchall($sql,1);    

                        $result1 = "";
               $cache = Zend_Registry::get('cache');

                if(!$result1 = $cache->load('mydata2')) {
                    echo 'caching the data…..';
                    $cache->save($result, 'mydata2');
                   } else {
                    echo 'retrieving cache data…….';
                    Zend_Debug::dump($result1);
                  }
}

define cache array in bootstrap file then using zend DB get the data from the database and store it in cache...
next time you don't need to get data from db . you can easily and fast access that data from the cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文