How to create a custom cache type and how to manage it

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:

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

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:

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!

Was this article helpful?
Dislike 0
Views: 27
Ihor Klymchuk
Rate us
Please wait...