Skip to content

Magento2 : Create Custom Cache to Reduce Server Load

Intro from wikipedia: web cache (or HTTP cache) is an information technology for the temporary storage (caching) of Web documents, such as web pagesimages, and other types of Web multimedia, to reduce server lag. A web cache system stores copies of documents passing through it; subsequent requests may be satisfied from the cache if certain conditions are met.[1] A web cache system can refer either to an appliance, or to a computer program.

so…, to achive this in Magento 2, here step to implement custom cache on your custom module

  1. create file YourCompany\YourModule\etc\cache.xml 
    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
    	<type instance="YourCompany\YourModule\Model\Cache\YourCacheName" name="yourcachename_cache_tag" translate="label,description">
    		<label>YourCacheName</label>
    		<description>Your Cache Description</description>
    	</type>
    </config>

     

  2. Create Cache Model : YourCompany\YourModule\Model\Cache\YourCacheName.php
    <?php
    
    
    namespace YourCompany\YourModule\Model\Cache;
    
    class YourCacheName extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
    {
    
        const TYPE_IDENTIFIER = 'yourcachename_cache_tag';
        const CACHE_TAG = 'YOURCACHENAME_CACHE_TAG';
    
        /**
         * @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);
        }
    }

     

  3. Create a Helper to handle your cache : YourCompany\YourModule\Helper\Data.php
    <?php
    
    
    namespace YourCompany\YourModule\Helper;
    
    use Magento\Framework\App\Helper\AbstractHelper;
    
    class Data extends AbstractHelper
    {
        protected $customCache;
    
        /**
         * @param \Magento\Framework\App\Helper\Context $context
         */
        public function __construct(
            \Magento\Framework\App\Helper\Context $context,
            \YourCompany\YourModule\Model\Cache\YourCacheName $customCache
        ) {
            $this->customCache = $customCache;
            parent::__construct($context);
        }
    
       public function createCache($cacheKey,$cacheData)
       {
    		
    	return $this->customCache->save($cacheData, $cacheKey, [\Anz\ProductRelation\Model\Cache\RelationCache::CACHE_TAG], 86400);
       }
    	
       public function loadCache($cacheId)
       {
    		
    	return $this->customCache->load($cacheId);
    
       }
    }

     

  4. Example usage your custom cache
    <?php 
    
    $helperCache = $this->helper('YourCompany\YourModule\Helper\Data');
    
    $cacheId = 'anyIdYouWant';
    
    /* check if cache available, if yes then load */
    if($loadedCache = $helperCache->loadCache($cacheId))
    {
       $dataToDisplay =  json_decode($loadedCache,true);
       print_r($dataToDisplay);
    
    }else{
    /* if not then create */
    
    $data = $someCollectionData->load($id);
    
     $helperCache->createCache($cacheId,json_encode($data->getData()));
    
    }
    
    
    ?>

     

 

Comments are closed.