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);
}
Leave a Reply