4

@Rakesh explains how to add custom cache in Magento 2 here.

My question is how to actually work with my custom cache in Magento 2?

In Magento 1 you would do something like:

$cacheId = 'example_id';
$cacheTag = 'block_html';
//check if our example_id cache contains any data - load() method will return false if cache is empty
if (($data_to_be_cached = Mage::app()->getCache()->load($cacheId))) {
    //if cache was found then unserialize it and assign to our variable
    $data_to_be_cached = unserialize($data_to_be_cached);
} else {
    //if not then normally assign data to the variable
    $data_to_be_cached = $exampleObject->exampleMethod();
    //then serialize and save it
    Mage::app()->getCache()->save(serialize($data_to_be_cached), $cacheId, array($cacheTag));
}

as explained here.

How can one handle checking, saving and loading cache inside a custom module in Magento 2?

9

As describe in the related post, you can inject your custom cache type : Vendor\Cachetype\Model\Cache\Type and use it.

public function __construct(\Vendor\Cachetype\Model\Cache\Type $cacheType)
{
    $this->_cacheType = $cacheType;
}

// Custom code Inside your function

$this->_cacheType->save(serialize($cacheData), $cacheKey, [\Vendor\Cachetype\Model\Cache\Type::CACHE_TAG], 86400);

$this->_cacheType->load($cacheKey);

$this->_cacheType->test($cacheKey);

Common methods :

  • load
  • save
  • test
  • remove
  • clean

All methods can be retrieved here : vendor/magento/framework/Cache/Frontend/Decorator/Bare.php

Parent class :

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Cache frontend decorator that attaches no additional responsibility to a decorated instance.
 * To be used as an ancestor for concrete decorators to conveniently override only methods of interest.
 */
namespace Magento\Framework\Cache\Frontend\Decorator;

class Bare implements \Magento\Framework\Cache\FrontendInterface
{
    /**
     * Cache frontend instance to delegate actual cache operations to
     *
     * @var \Magento\Framework\Cache\FrontendInterface
     */
    private $_frontend;

    /**
     * @param \Magento\Framework\Cache\FrontendInterface $frontend
     */
    public function __construct(\Magento\Framework\Cache\FrontendInterface $frontend)
    {
        $this->_frontend = $frontend;
    }

    /**
     * Set frontend
     *
     * @param \Magento\Framework\Cache\FrontendInterface $frontend
     * @return $this
     */
    protected function setFrontend(\Magento\Framework\Cache\FrontendInterface $frontend)
    {
        $this->_frontend = $frontend;
        return $this;
    }

    /**
     * Retrieve cache frontend instance being decorated
     *
     * @return \Magento\Framework\Cache\FrontendInterface
     */
    protected function _getFrontend()
    {
        return $this->_frontend;
    }

    /**
     * {@inheritdoc}
     */
    public function test($identifier)
    {
        return $this->_getFrontend()->test($identifier);
    }

    /**
     * {@inheritdoc}
     */
    public function load($identifier)
    {
        return $this->_getFrontend()->load($identifier);
    }

    /**
     * Enforce marking with a tag
     *
     * {@inheritdoc}
     */
    public function save($data, $identifier, array $tags = [], $lifeTime = null)
    {
        return $this->_getFrontend()->save($data, $identifier, $tags, $lifeTime);
    }

    /**
     * {@inheritdoc}
     */
    public function remove($identifier)
    {
        return $this->_getFrontend()->remove($identifier);
    }

    /**
     * {@inheritdoc}
     */
    public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
    {
        return $this->_getFrontend()->clean($mode, $tags);
    }

    /**
     * {@inheritdoc}
     */
    public function getBackend()
    {
        return $this->_getFrontend()->getBackend();
    }

    /**
     * {@inheritdoc}
     */
    public function getLowLevelFrontend()
    {
        return $this->_getFrontend()->getLowLevelFrontend();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.