Magento uses a cache to optimize page loads. If we go to the admin panel in System -> Cache manager, then we will see the different types of caches that Magento uses.
Sometimes there is a need to download custom data in Magento. Let’s look at how we can create our own cache and how to manage it.
1.In our custom module, we create a new file etc/cache.xml with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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="custom_cache" translate="label,description" instance="MageDirect\Custom\Model\Cache\Custom"> <label>Custom Cache type</label> <description>Custom cache description.</description> </type> </config> |
For the instance attribute, we need to set our model for cache control.
2. Create a new file app/code/MageDirect/Custom/Model/Cache/Custom.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?php namespace MageDirect\Custom\Model\Cache; <?php namespace MageDirect\Custom\Model\Cache; /** * System / Cache Management / Cache type "Custom Cache Tag" */ class Custom extends \Magento\Framework\Cache\Frontend\Decorator\TagScope { /** * Cache type code unique among all cache types */ const TYPE_IDENTIFIER = 'custom_cache'; /** * Cache tag used to distinguish the cache type from all other cache */ const CACHE_TAG = 'CUSTOM_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); } } |
Note that the TYPE_IDENTIFIER constant must be named in the same way as the cache in the cache.xml file.
That’s all. We created a cache and now it is displayed in the admin panel:
3. However, simply displaying the admin panel is not enough, you also need to be able to manage and use the cache somehow. Here’s an example of a custom model that uses our cache:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <?php namespace MageDirect\Custom\Model; use MageDirect\Custom\Model\Cache\Custom as CustomCache; class CustomModel { /** * Cache State * * @var \Magento\Framework\App\Cache\StateInterface */ protected $cacheState; /** * @var \Magento\Framework\App\CacheInterface */ protected $cache; /** * Custom constructor. * @param \Magento\Framework\App\CacheInterface $cache * @param \Magento\Framework\App\Cache\StateInterface $cacheState */ public function __construct( \Magento\Framework\App\CacheInterface $cache, \Magento\Framework\App\Cache\StateInterface $cacheState ) { $this->cacheState = $cacheState; $this->cache = $cache; } /** * @return bool|int|string */ public function getBigData() { // Try to load data from cache $data = $this->loadFromCache(); if (!$data) { //Do something; $data = “Some big data here”; //Save some data to cache $this->saveToCache($data); } return $data; } /** * Save to cache if enabled; * * @param $data * @return $this */ protected function saveToCache($data) { if ($this->cacheState->isEnabled(CustomCache::TYPE_IDENTIFIER)) { $this->cache->save($data, CustomCache::CACHE_TAG); } return $this; } /** * Try to retrieve data from cache * * @return bool|string */ protected function loadFromCache() { //Check is cache enabled if ($this->cacheState->isEnabled(CustomCache::TYPE_IDENTIFIER)) { return $this->cache->load(CustomCache::CACHE_TAG); } return false; } } |
Let’s imagine that our getBigData method returns the data you want to cache. In the same method, we check whether the data is in the cache with a help of the method $this->loadFromCache(). If there are no data, we will download them and save them again to the cache using the method $this->saveToCache()
That’s all. Our custom cache is ready!
Ihor KlymchukTechnical Lead
- Follow this tutorial to clear Magento cache with no efforts. Also, you will...
- This easy and effortless tutorial on how to create the new admin account in...
- SSL is very important for every online store as it initiates a secure session...
- The sitemap is highly important in case you want to optimize your website for...
- Want to manage email templates in your Magento 2 store? Check our article and...
- Do you know how to create custom product type? We know! And would be glad to...
- Want to have a Magento 2 Multistore? Read how our team setups...
- Want to use the latest trend Progressive Web Apps? Read how to install PWA on...