【发布时间】:2015-07-02 15:40:29
【问题描述】:
我编写了一个扩展,其中一个基本抽象类包含一个将提取产品集合的函数:
$cache = Mage::app()->getCache();
if(!$cache->load('itserv_feed_collection')) {
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToSelect('stock_status');
$_productCollection->addAttributeToSelect(Mage::getStoreConfig('feed_options/mappa_attributi/produttore'));
$_productCollection->addAttributeToSelect(Mage::getStoreConfig('feed_options/mappa_attributi/ean'));
$_productCollection->addAttributeToSelect(Mage::getStoreConfig('feed_options/mappa_attributi/mpn'));
$_productCollection->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
$cache->save(serialize($_productCollection), "itserv_feed_collection", array("itserv_feed_collection"), 120);
}
else {
$_productCollection = unserialize($cache->load('itserv_feed_collection'));
}
return $_productCollection;
从这个类扩展的每个子类都将使用同一个运行时堆栈中的同一个集合。我想将此集合保存在缓存中(正如您在查看代码时看到的那样),因此由于子类将第二次使用它,因此脚本不需要再次加载它。
问题是不可能使用缓存,因为缓存需要一个序列化的集合,在这种情况下,我不能这样做,因为集合包含无法序列化的 Mage_Core_Model_Config_Element(它会触发著名的错误“序列化'Mage_Core_Model_Config_Element' 是不允许的”)。
我尝试了不同的解决方案,甚至使用 json_encode/json_decode 代替 serialize/unserialize,但我无法解决问题。
你有什么解决办法吗?谢谢!
【问题讨论】:
-
您能否根据收藏中的其他信息再次正确设置 Mage_Core_Model_Config_Element ?如果是这样,每次反序列化对象时都会调用魔术方法
__wakup,这样就可以解决问题? -
首先,感谢您的评论。 “...再次正确设置 Mage_Core_Model_Config_Element...”是什么意思?
-
我的意思是:
Mage_Core_Model_Config_Element的目的是什么?是否需要它?难道您不能从中获取标识符,以便您可以通过__wakeup正确重新加载它吗?
标签: magento caching serialization magento-1.9