Skip to content
intermediate Phase 68 · Cache System

Cache Types — Configuration, Block HTML, and Custom Caches

Understanding Magento 2 cache types: configuration cache, layout cache, block HTML cache, page cache, and creating custom cache types

45m
1 problems
Topic Progress 0%

Magento Cache Types Overview

All Cache Types

php bin/magento cache:status
Cache Type Code Purpose
Configuration config Module configuration
Layout layout Compiled layout XML
Block HTML Output block_html Rendered block HTML
Collections Data collections Collection query results
DDL Data ddl Schema structure
Entity Attribute Value eav EAV metadata
Full Page Cache fpc Full page HTML
Integration API integration_api REST/SOAP API
Web Services Configuration webservices_xml WSDL
Translations translate Parsed translations
Compiled Config compiled_config Compiled DI config

Cache Management

# Check status
php bin/magento cache:status

# Clean specific type
php bin/magento cache:clean config

# Flush all cache
php bin/magento cache:flush

# Disable specific type
php bin/magento cache:disable block_html

# Enable specific type
php bin/magento cache:enable block_html

Configuration Cache

What Configuration Cache Stores

  • Module configuration (config.xml)
  • System configuration (system.xml)
  • di.xml compiled configuration
  • Layout XML files
  • Theme configuration

When Configuration Cache Clears

# After module install/upgrade
php bin/magento setup:upgrade

# After config change
php bin/magento app:config:import

# Manual clean
php bin/magento cache:clean config

Configuration Cache Impact

// Configuration loaded from cache in production
// Without cache: every request loads all XML files
// With cache: XML loaded once, served from cache

// Performance impact:
// Without config cache: +200-500ms per request
// With config cache: ~10ms

Clearing Strategy

# Production: only clear when needed
php bin/magento cache:clean config  # Only when config changes

# Development: clear more frequently
php bin/magento cache:flush  # Clear everything

Block HTML Cache

What Block HTML Cache Stores

Rendered HTML output of blocks with cacheable=true.

// Block with caching
public class ProductList extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }
}

Cacheable Blocks

// Cacheable block (default)
public function getCacheKey()
{
    return 'product_list_' . $this->getCategory()->getId();
}

public function getCacheLifetime()
    return 3600; // 1 hour
}

Non-Cacheable Blocks

// Non-cacheable block
public function __construct(
    // ...
    array $data = []
) {
    $data['cacheable'] = false;
    parent::__construct($context, $data);
}

Block Cache Tags

public function getCacheTags()
{
    return ['catalog_product_' . $this->getProduct()->getId()];
}

Custom Cache Types

Register Custom Cache Type

<!-- etc/cache.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
    <type id="vendor_module_cache"
          translate="label"
          label="Vendor Module Cache"
          instance="Magento\Framework\Cache\Backend\Redis"/>
</config>

Using Custom Cache

namespace Vendor\Module\Service;

use Magento\Framework\Cache\CacheInterface;

class DataCache
{
    public function __construct(
        private CacheInterface $cache
    ) {}

    public function getData(string $key): ?string
    {
        $cacheKey = 'vendor_module_' . $key;
        $data = $this->cache->load($cacheKey);
        
        if ($data === false) {
            $data = $this->fetchData($key);
            $this->cache->save(serialize($data), $cacheKey, ['vendor_module'], 3600);
        }
        
        return unserialize($data);
    }

    public function cleanCache(): void
    {
        $this->cache->clean('tags', ['vendor_module']);
    }
}

Cache Configuration in config.php

// app/etc/config.php
'cache_types' => [
    'config' => 1,
    'layout' => 1,
    'block_html' => 1,
    'collections' => 1,
    'vendor_module_cache' => 1
]

Practice Problems

0 / 1 solved
Cache Type Strategy

A custom module needs its own cache type. Design the cache strategy including tags, lifetime, and invalidation.

Quiz

1. What cache type stores compiled layout XML?

Question 1 options

2. How do you disable a specific cache type?

Question 2 options

3. What does cache:flush do differently from cache:clean?

Question 3 options

4. How do you create a non-cacheable block?

Question 4 options

Flashcards

Question

What cache type stores module configuration?

Answer

config cache type

Question

How to check cache status?

Answer

php bin/magento cache:status

Question

What is the difference between cache:clean and cache:flush?

Answer

clean invalidates entries; flush removes all entries

Question

How to make a block non-cacheable?

Answer

Set cacheable=false in constructor $data array

Question

Where are custom cache types registered?

Answer

etc/cache.xml in the module directory

Revision Notes

Key Takeaways

  • 1. Magento has 11+ built-in cache types for different data categories
  • 2. config cache stores XML configuration; clearing it requires setup:upgrade
  • 3. block_html cache stores rendered block HTML with cache tags for invalidation
  • 4. Custom cache types registered in etc/cache.xml
  • 5. Use cache:clean for invalidation, cache:flush for full removal
  • 6. Non-cacheable blocks bypass block HTML cache entirely

Interview Tips

  • List the main Magento cache types and their purposes
  • Explain when to use cache:clean vs cache:flush
  • Describe how block caching works with cache tags
  • Know how to create custom cache types

Cheat Sheet

Cache Types Cheat Sheet

Main types:

  • config: module/system XML
  • layout: compiled layout
  • block_html: rendered blocks
  • collections: query results
  • fpc: full page cache
  • eav: EAV metadata

Commands:

  • cache:status — check all
  • cache:clean type — invalidate
  • cache:flush — remove all
  • cache:disable type — disable
  • cache:enable type — enable

Custom cache:
etc/cache.xml

Non-cacheable block:
$cacheable = false in constructor