Improving performance is a top priority for any Magento 2 store, especially when handling heavy database queries.

Collections, whether for products, orders, or custom entities, can become expensive if loaded repeatedly.

One of the most effective ways to improve performance is to caches the results of the collection queryso Magento doesn’t always go to the database.

Want to improve your store’s overall speed and performance? Check out our Magento 2 Speed ​​& Optimization service.

In this guide, we’ll cover why caching is important, how Magento handles caching internally, and how you can implement collection-level caching safely and efficiently.

Why Cache Collection Queries?

Magento collections often include:

  • Lots JOIN operation
  • Filter, group, or sort
  • EAV attribute loading
  • Big data sets (products, orders, customers)

Without caching, this query is executed on every request, which:

  • Increases database load
  • Slows down page creation
  • Affects overall site performance

By caching collection results, you reduce repetitive processing and provide faster responses to users—and APIs.

How Magento Handles Collection Caching

Magento 2 provides a built-in mechanism to perform cached database fetch operations. This is done using FetchStrategy class.

The default strategy is yes not cache data, but Magento includes a ready-to-use caching strategy here:

Magento\Framework\Data\Collection\Db\FetchStrategy\Cache

If enabled, this strategy stores the collection results in Magento’s cache and restores them instantly on the next load.

How to Enable Cache for Custom Collections

You can implement a collection cache using di.xml decisively FetchStrategy class for your collection.

Example: Enable Caching for Custom Collections

<type name="Vendor\Module\Model\ResourceModel\Custom\Collection">
    <arguments>
        <argument name="fetchStrategy" xsi:type="object">
            Magento\Framework\Data\Collection\Db\FetchStrategy\Cache
        </argument>
    </arguments>
</type>

Once added, Magento will automatically start caching the results of this collection.

But what if you want to delete cache set data using a custom cache tag?

By default, Magento stores collection results under generic COLLECTION_DATA marking. If you need to associate your own tags for more granular control, you can do so.
Here’s an example:

<virtualType name="CustomCollectionCacheFetchStrategy" type="Magento\Framework\Data\Collection\Db\FetchStrategy\Cache">
    <arguments>
        <argument name="cacheTags" xsi:type="array">
            <item name="custom" xsi:type="string">custom_collection</item>
        </argument>
    </arguments>
</virtualType>
<type name="Vendor\Module\Model\ResourceModel\Custom\Collection">
    <arguments>
        <argument name="fetchStrategy" xsi:type="object">CustomCollectionCacheFetchStrategy</argument>
    </arguments>
</type>

After enabling caching for Collections, Magento applies this cache globally.

It means every part of the system that contains this collection—including UI Components—will automatically use cached data.

To prevent this, we create a virtual class for the cache so that only the targeted context uses it, while the UI Component uses the native collection.

Below is an example showing how to enable caching for Collections exclusively within a module by updating di.xml:

<virtualType name="CustomCollectionCacheFetchStrategy" type="Magento\Framework\Data\Collection\Db\FetchStrategy\Cache">
    <arguments>
        <argument name="cacheTags" xsi:type="array">
            <item name="custom" xsi:type="string">custom_collection</item>
        </argument>
    </arguments>
</virtualType>
<virtualType name="VirtualCustomCollection" type="Vendor\Module\Model\ResourceModel\Custom\Collection">
    <arguments>
        <argument name="fetchStrategy" xsi:type="object">CustomCollectionCacheFetchStrategy</argument>
    </arguments>
</virtualType>
<virtualType name="VirtualTypeCustomCollectionFactory" type="Vendor\Module\Model\ResourceModel\Custom\CollectionFactory">
    <arguments>
        <argument name="instanceName" xsi:type="string">VirtualCustomCollection</argument>
    </arguments>
</virtualType>
<type name="Vendor\Module\Block\Custom">
    <arguments>
        <argument name="customCollectionFactory" xsi:type="object">VirtualTypeCustomCollectionFactory</argument>
    </arguments>
</type>

This way, you can have context-specific caches for your custom collections.

If you want to create a custom cache tag in Magento 2, please refer to our blog:
How to create custom cache types in Magento

Please check the image link for the difference between before and after implementing cache collection.
Before:

After:

after-cache-collection

Hopefully this makes caching workflows easier to implement and adapt to your needs.
Happy Coding 🙂

If you need technical assistance, please feel free to contact us at support@webkul.com.

You can also explore various solutions to enhance your Magento 2 store by visiting us Magento 2 Extensions page.

For expert guidance or custom feature development, you can do so hire our Magento 2 developers to support your project.


News
Berita
News Flash
Blog
Technology
Sports
Sport
Football
Tips
Finance
Berita Terkini
Berita Terbaru
Berita Kekinian
News
Berita Terkini
Olahraga
Pasang Internet Myrepublic
Jasa Import China
Jasa Import Door to Door

Kiriman serupa