我应该在 Zend_Db 中的哪里实现缓存?
我正在寻找在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,
zf create db-table
将始终创建一个继承Zend_Db_Table_Abstract
的类,这将使您的提案难以管理。此外,您正在耦合缓存和缓存。 ZF 的 db 模块,因此有人可能会争辩说,将缓存机制放在 dbtable 范围下是不正确的。例如:你不应该知道数据从哪里获取,但仍然能够缓存它,所以过程变成这样:
因此,尽管您的解决方案现在有意义,因为您只使用 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 inheritingZend_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:
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.
在 bootstrap 文件中定义缓存数组,然后使用 zend DB 从数据库获取数据并将其存储在缓存中...
下次您不需要从 db 获取数据。您可以轻松快速地从缓存中访问该数据。
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.