Fabrizio Vanzani

ecommerce development, magento and tech tips

Use magento 2 cache

A quick tip to use magento 2 cache:

$objectManager=\Magento\Framework\App\ObjectManager::getInstance();
$cache=$objectManager->get('\Magento\Framework\App\Cache');
$serializer=$objectManager->get('\Magento\Framework\Serialize\SerializerInterface');

$identifier='my_identifier';

// cache HIT
if ($serializedData  = $this->cache->load($identifier);) {
	$data = $serializer->unserialize($serializedData)
// cache MISS
} else {
	$data ="my data to save";  // generate new data here 
	$tags=['my_data'];
	$lifetime=30;
       $serializedData = $serializer->serialize($data);
       $cache->save($serializedData, $identifier, $tags, $lifetime);
}


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *