Here is the script (in a shipping module->carrier method, is the cache function can be used in checkout page?), in my custom module

$cacheKey = $code;  //e.g ABC

$cacheData = $request->getPackageWeight().'_'.$request->getDestCountryId().'_'.$code.'='.$totalPostage;

$this->_cacheType->save($cacheData, $cacheKey, [\Testcache\Test\Model\Cache\Type::CACHE_TAG], 86400);
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testcache.log');
$logger = new \Zend\Log\Logger();
$logger->info('cacheSave:' . $this->_cacheType->load($cacheKey));


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
    <type name="test_cache" translate="label,description" instance="Testcache\Test\Model\Cache\Type">
        <label>Test Cache type</label>
        <description>Test cache description.</description>


namespace Testcache\Test\Model\Cache;

 * System / Cache Management / Cache type "Custom Cache Tag"
class Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
     * Cache type code unique among all cache types
    const TYPE_IDENTIFIER = 'test_cache_tag';

     * Cache tag used to distinguish the cache type from all other cache
    const CACHE_TAG = 'test_cache';

     * @param \Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool
    public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
        parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);

Anyone know what is the problem?

-------------update-------------- new testing script

    public function collectRates(RateRequest $request)

        $cacheKey = '123123123123123';                
        //$cacheData = $request->getPackageWeight().'_'.$request->getDestCountryId().'_'.$code.'='.$totalPostage;
        $cacheData = '123123123132123123123123123';
        $this->_cacheType->save($this->serializer->serialize($cacheData), $cacheKey, [\Testcache\Test\Model\Cache\Type::CACHE_TAG], 86400);
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testcache.log');
        $logger = new \Zend\Log\Logger();
        $logger->info('cacheSave:' . print_r($this->_cacheType->load($cacheKey), true));


1 Answer 1


You should save your data in serialize form.

Inject your custom cache type : Testcache\Test\Model\Cache\Type and use it.

public function __construct(\Testcache\Test\Model\Cache\Type $cacheType)
    $this->_cacheType = $cacheType;

// Custom code Inside your function

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



$this->_cacheType->load($cacheKey) will give you the object / array, so use this is in log as

$logger->info(print_r($this->_cacheType->load($cacheKey), true));

Hope it will help you.

  • Donot use serialize() use `\Magento\Framework\Serialize\SerializerInterface:serialize
    – Amit Bera
    Commented Sep 6, 2019 at 11:58
  • Yes use the magento serialize class \Magento\Framework\Serialize\SerializerInterface:serialize If this answers is useful then please accept this answer.
    – DJ Dev
    Commented Sep 6, 2019 at 12:12
  • @DJDev it didn't work
    – hkguile
    Commented Sep 6, 2019 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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