Hi am having a controller Search.php
every time I do operation it loads all the collection and takes much time to load. So I wanted to cache the custom collection and optimize the loading time.
this is my collection how to put this in a cache.
below is my controller
use Magento\Framework\Serialize\SerializerInterface;
class Search extends \Magento\Framework\App\Action\Action
{
private cacheId = 'IdForCachingPurposes';
private $serializer;
public function __construct(
SerializerInterface $serializer,
array $data = []
) {
parent::__construct($context, $data);
$this->serializer = $serializer;
}
public function execute()
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/PRAVEEN.log');
$logger = new \Zend\Log\Logger(); $logger->addWriter($writer);
$collection = "My data";
if($this->cache->load($this->cacheId)) {
$logger->info('cached');
//Get unserialized data from Cache
$data = unserialize($this->cache->load($this->cacheId));
} else {
$logger->info('cach saved');
//Store Data in Cache
$storeData = $this->cache->save(
serialize($collection), $this->cacheId, array(\Vendor\Search\Model\Cache\Type::CACHE_TAG),
86400);
}
}
}
Where am doing wrong. Can I get Help? Thank yo