Try the Demo

Magento Forum

   
Cache Data Collections in Magento. A first approach
 
raulsanchez
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2011-02-22
 

We can cache HTML output easily in Magento
http://www.magentocommerce.com/wiki/5_-_modules_and_development/block_cache_and_html_ouput

But… what about data collections?
This is a very first approach I want to share with the community, just a starting point ‘integrating’ this method
http://www.joeyrivera.com/2009/caching-using-phpzend_cache-and-mysql/ in Magento

I believe we could do easier (see _getProductCollection() method at Mage_Sales_Model_Entity_Quote_Item_Collection) and surely prettier, meanwhile…

STEP 1
/var/cache must have 777 permissions

STEP 2
Create Cache.php class in your module (in this example it is under /lib folder)

<?php
class Collection_Cache {
    
    
private $_instance;
    private 
$_folder 'var/cache/local/';

    public function 
__construct($time null{
        
        
try {
            mkdir
($this->_folder0777);
        
catch (Exception $e){
            
// no permissions
        
}
        
        $frontendOptions 
= array(
           
'lifetime' => $time,
           
'automatic_serialization' => true
        
);
        
$backendOptions = array(
            
'cache_dir' => $this->_folder
        
);
        
$this->_instance Zend_Cache::factory('Core''File'$frontendOptions$backendOptions);
        return;
    
}
    
    
public function load($id){
        
return $this->_instance->load($id);
    
}
    
    
public function save($data){
        
return $this->_instance->save($data);
    
}
    
    
public function remove($id){
        
return $this->_instance->remove($id);
    
}
}

STEP 3
Then wherever you’re loading your data collections…
At the top of the class…

require_once 'Mod_company/Mod_name/lib/Cache.php';
And then at the method where we are getting the collection…
$cache = new Collection_Cache();
$cacheId $uniqueId;
if(! 
$collection $cache->load($cacheId)) {
            
            
/* no cache found, build the collection... */
            /* at the end save it to cache */        
            
$cache->save($collection);

}

Now you’ll see your cached collection data is under /var/cache/local folder
If you don’t specify an expire time as parameter when calling new Collection_Cache() your data will never expire, letting you the chance to do it everytime you need

What do you think?
Best regards

 Signature 

http://www.viansolutions.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
raulsanchez
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2011-02-22
 

In fact, an easier way…

$cache Mage::app()->getCacheInstance();
$cacheId $uniqueId;
if(! 
$collection $cache->load($cacheId)) {
            
/* no cache found, build the collection... */
            
$cache->save(serialize($collection),$cacheId);
else {
            $collection 
unserialize($collection);
}

This way our cached data is included in Magento cache

 Signature 

http://www.viansolutions.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
metalim
Jr. Member
 
Total Posts:  1
Joined:  2011-08-18
 

Thanks!
Really helpful. smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
raulsanchez
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2011-02-22
 
metalim - 19 August 2011 01:01 AM

Thanks!
Really helpful. smile

I appreciate
I was starting to think anybody here cares about caching his data collections…

 Signature 

http://www.viansolutions.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
raulsanchez
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2011-02-22
 

Updated info from http://stackoverflow.com/questions/3755104/how-to-cache-a-collection-in-magento

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));

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.

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.

 Signature 

http://www.viansolutions.com

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top