简体   繁体   中英

Magento work with cache, can't serialize collection

I'm learning how to use magento cache and I'm a bit stuck trying to serialize a collection.

Actually this is my code:

class Feliu_Featuredcategories_Block_Topcategories extends Mage_Core_Block_Template
{

protected function _construct()
{
    $storeId = Mage::app()->getStore()->getId();
    $this->addData(array(
        'cache_lifetime'            => 3600,
        'cache_tags'                => array(Mage_Catalog_Model_Product::CACHE_TAG),
        'cache_key'                 => 'homepage-most-view-' . $storeId,
    ));
}

public function setData()
{
    $storeId = Mage::app()->getStore()->getId();
    $cache = Mage::app()->getCache();
    $key = 'homepage-most-view-' . $storeId;
    $cached_categories = $cache->load($key);

    if (! $cached_categories) {
        $categories = Mage::getModel('catalog/category')
            ->getCollection()
            ->addAttributeToSelect(array('data', 'name', 'add_to_top_categories'))
            ->addAttributeToFilter('add_to_top_categories', array('eq' => '1'));
        $categories->load();

        $cache->save(serialize($categories), $key);

    } else {
        $categories = unserialize($cached_categories);
    }

    return $categories;
}
}

At first I tried to $cache->save($categories, $key); directly, but I read that collections can't be saved directly and I got an error that said: 'automatic_serialization must be on' when I tried to set automatic_serialization to true then I received a message saying that it can't be activated for security reasons.

Then I tried to serialize, just as the above code shows, but it did not work neither. It seems that magento protects collections from being serialized because they can be really big.

So finally I tried to urlencode() before serializing serialize(urlencode($categories)) and urldecode(unserialize($categories)) but I got the string "N;" serializing with this aproach and an empty string when unserialize.

I'm using magento 1.9.3 and I followed this documentation and previous questions:

https://www.nicksays.co.uk/developers-guide-magento-cache/

http://inchoo.net/magento/magento-block-caching/

Magento: serialization error on caching Collection

Magento how to cache a productCollection

And some other questions about this, but maybe there is no need to write too much links, I don't want to spam.

Edit: If instead a collection I use an array like

$categories = array('banana', 'apple', 'kiwi', 'strawberry', 'pomelo', 'melon');

then the code seems to work correctly

Finally I solved it, the answer it's easiest than I though at the beginning but I write it here because maybe it will help somebody in the future.

As collections cannot be cached nor serialized, I made an array with the data I need from the collection.

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToFilter('add_to_top_categories', array('eq' => '1'))
    ->addAttributeToSelect(array('data', 'name'));

I make the collection adding only the fields I need, and selecting the data I want.

    $array = array();
    foreach ($categories as $_category)
    {
        array_push($array, array('url' => $_category->getUrl(), 'name' => $_category->getName()));
    }

Now I make an array that holds objects with the data I wanted. Next step is serialize the array I just made and save it on the cache.

    $cache->save(serialize($array), $key, array('custom_home_cache'), 60*60);

and retrieve the data is as easy as $cache->load(unserialize($key))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM