如何在Magento中缓存集合? [英] How to cache a collection in Magento?

查看:74
本文介绍了如何在Magento中缓存集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏集需要花费大量时间才能加载.我想要的是对其进行缓存(APC,Memcache).无法缓存整个对象(因为它不能反序列化并且超过1 MB).我在想缓存收集数据($ col-> getData())是可行的方法,但是我发现没有办法根据此数组重建对象.有任何线索吗?

I have a collection that takes significant time to load. What I would like is to cache it (APC, Memcache). It is not possible to cache the entire object (as it cannot be unserialized and it is over 1 MB). I'm thinking that caching the collection data ($col->getData() ) is the way to go, but I found no way to rebuild the object based on this array. Any clues?

推荐答案

集合已经内置了一些缓存,但是它们需要一些提示,因此请将其放入集合的构造函数中:

Collections already have some caching built in but they need a little prompting so put this in the constructor of a collection:

$cache = Mage::app()->getCacheInstance();
$prefix = "SomeUniqueValue";
$this->initCache($cache, $prefix, array(Mage_Catalog_Model_Product::CACHE_TAG));

选择适合于集合内容的标签,以便将其自动刷新.这种方法基于正在执行的查询来构建ID,当对集合进行过滤,排序或分页时最有用-避免版本冲突.

Choose tags appropriate to the content of the collection so that it will be flushed automatically. This way builds an ID based on the query being executed, it is most useful when the collection is filtered, ordered or paged - it avoids a version conflict.

通常这几乎不被使用,因为当您检索数据时,您几乎总是最终会以HTML形式显示它,因此有意义的是缓存输出.块缓存已得到广泛使用,并得到了更好的记录.

Generally this hardly gets used because when you retrieve data you almost always end up displaying it, probably as HTML, so it makes sense to cache the output instead. Block caching is widely used and better documented.

这篇关于如何在Magento中缓存集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆