Magento Custom Caching with admin switch

Magento Custom Caching with admin switch

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Custom Caching with admin switch

Enhancing Magento Cache Management: Custom Caching Switch from Admin

In Magento development, it’s common to require custom caching mechanisms that integrate seamlessly with the core system. If you’re building an extension that utilizes its own caching layer, you’ll want users to be able to clear these caches directly from the admin area. In this guide, we’ll explore how to create a custom cache type and enable/disable it specifically for your module.

Creating a Custom Cache Type

To begin, let’s define a new cache type in your Magento extension. This involves creating a new di.xml file under app/code/Vendor/Extension/etc.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManageretc:config.xsd">
    <type name="Magento\Framework\Cache\Type\FrontendPool">
        <arguments>
            <argument name="caches" xsi:type="array">
                <item name="custom_vendor_cache" xsi:type="array">
                    <item name="frontend" xsi:type="string">Magento\Framework\App\Cache\Type\FileCache</item>
                    <item name="backendName" xsi:type="string">vendor_module_cache</item>
                    <item name="backendOptions" xsi:type="array">
                        <item name="cache_dir" xsi:type="string">/tmp/magento-vendor-cache-1</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

In this example, we’ve created a new cache type named custom_vendor_cache with the backend name vendor_module_cache. The cache files will be stored in /tmp/magento-vendor-cache-1.

Integrating Custom Cache with Admin Area

To allow users to clear your custom cache from the admin area, you’ll need to create a new cache type configuration and integrate it into the existing Magento cache management system.

First, define your new cache type in etc/adminhtml/system.xml:

<config xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/schema.xsd">
    <system>
        <tab id="general" translate="label">
            <label>General</label>
            <section id="vendor_cache" showInDefault="1" showInWebsite="1" showInStore="0" sortOrder="30">
                <label>Custom Vendor Cache Configuration</label>
                <resource>Magento_All::config</resource>
                <class>adminhtml_system_config_form_fieldset</class>
                <group id="advanced" translate="label">
                    <field id="vendor_cache_enabled" type="select" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="0"
                          options="{'1': 'Enabled', '0': 'Disabled'}">
                        <backend_model>Magento\Config\Model\Config\Backend\Encrypt</backend_model>
                        <label>Enable Custom Vendor Cache:</label>
                    </field>
                </group>
            </section>
        </tab>
    </system>
</config>

This configuration adds a new section to the admin area’s Advanced settings, allowing you to enable or disable your custom cache.

Next, integrate your cache type into the existing Magento cache management system. You can achieve this by overriding the Magento\Backend\Controller\Adminhtml\Cache controller in your extension:

<?php

namespace Vendor\Extension\Controller\Adminhtml;

class Adminhtml extends \Magento\Backend\Controller\Adminhtml\Cache
{
    public function execute()
    {
        $this->cacheTypeList = [
            'config' => false,
            'layout' => true,
            'block_html' => true,
            'translations' => true,
            'collections' => true,
            'reflection' => true,
            'compiled_config' => true,
            'eav_type' => true,
            'db_ddl' => true,
            'customer_data' => false,
            'full_page' => true,
            'config_integration' => true,
            'config_integration_api' => true,
            'google_code_cache' => true,
            'compiled_handle' => true,
            'amazon_s3' => true,
            'amazon_ssw' => false,
            'amazon_session' => false,
            'amazon_msp' => false,
            'amazon_ups' => false,
            'custom_vendor_cache' => $this->getRequest()->getParam('custom_vendor_cache'), // Add your custom cache
        ];

        return parent::execute();
    }
}

In this override, we’ve added our custom_vendor_cache to the $cacheTypeList array. The value is set based on the parameter passed from the admin area.

Conditional Caching Based on Enable/Disable

To conditionally use your custom cache based on the admin area configuration, you can check if the vendor_cache_enabled setting is enabled:

<?php

namespace Vendor\Extension\Model;

class CustomLogic
{
    private $cacheManager;
    private $scopeConfig;

    public function __construct(
        \Magento\Framework\App\CacheInterface $cacheManager,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->cacheManager = $cacheManager;
        $this->scopeConfig = $scopeConfig;
    }

    public function performLogic()
    {
        if ($this->isVendorCacheEnabled()) {
            // Cache is enabled; proceed with caching logic
            return $this->doCaching();
        } else {
            // Cache is disabled; do not cache the data
            return $this->getDataWithoutCaching();
        }
    }

    private function isVendorCacheEnabled()
    {
        return (bool)$this->scopeConfig->getValue('vendor/extension/advanced/vendor_cache_enabled');
    }
}

In this CustomLogic model, we’re checking if the vendor_cache_enabled setting is enabled. If it’s enabled, we proceed with our caching logic; otherwise, we perform the operation without caching.

Conclusion

Integrating custom caching into Magento requires a deep understanding of the cache management system and careful integration to ensure compatibility with existing features. By following this guide, you can create a custom cache type that is easily managed from the admin area and conditionally used based on your module’s settings.

Remember to test your implementation thoroughly in different scenarios to ensure optimal performance and reliability. For more advanced Magento development techniques, consider exploring our resources at Laravel Company.

Happy coding!

Cache Extensions Themes Admin

Our network