When developing custom functionalities for our clients, we try to utilize Magento features as much as possible. When working on our Magento2 shop for Iglu šport, we were searching for the most efficient way of utilizing Magento cache. What did we come up with? Read below! :)

One of the key things is to use Magento cache in a way that we can cache our rendered content on the frontend and flush only the specific blocks if needed. A custom cache type therefore enables us to specify what is cached and enables our clients to clear that specific cache type using the Cache Management option in the Administration.

Below, you can find an example on how we used a custom cache type for a Megamenu.

1. Create a new cache type

To create a new cache type, you will have to create app/code/Optiweb/MegaMenu/etc/cache.xml in your module.

<?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="megamenu" translate="label,description" instance="Optiweb\MegaMenu\Model\Cache\Type">
        <label>Megamenu</label>
        <description>Megamenu cache.</description>
    </type>
</config>