我有自定义模块。在模型中,我有一个类XXX和方法YYY。在这个方法中,我使用cURL请求远程服务器。我想使用magento缓存来缓存此结果。我怎么能这样做?
答案 0 :(得分:0)
你可以做这样的事情,只需用你的cUrl调用替换rss
$url = 'http://www.site.com';
$cache = Mage::getSingleton('core/cache');
$key = md5($url);
if(! $rss = $cache->load($key)){
$objDOM = new DOMDocument();
$objDOM->load($url);
$rssXml = $objDOM->getElementsByTagName("item");
$rss = array();
foreach( $rssXml as $value ){
$data['title'] = $value->getElementsByTagName("title")->item(0)->nodeValue;
$data['description'] = $value->getElementsByTagName("description")->item(0)->nodeValue;
$rss[] = $data;
}
$cache->save(urlencode(serialize($rss)), $key, array("homepage_cache"), 60*60);
}
else{
$rss = unserialize(urldecode($rss));
}
print_r($rss);
了解更多@ http://www.nicksays.co.uk/2010/07/developers-guide-magento-cache/