A Complete Guide to Custom Caching in Magento 2 (Updated 2019)
blog-page-header-bg

A Complete Guide to Custom Caching in Magento 2 (Updated 2019)

COMMENTS (0)
Tweet

In this blog, I will explain how to create your own cache in Magento 2, and how to read and write data from that custom cache. I hope after reading this article you will find many ways to optimize your site’s performance.

What is Caching?

Caching is a high-speed layer of temporary data storage. In this process, relevant pieces of data are stored so that requests in the future for similar data can be served faster.

Caching helps applications work dramatically faster. By using Caching, you can efficiently reuse old retrieved and computed information. Retrieving data from cache is much faster than retrieving data directly from Databases

How does it work?

Initially, data is fetched from its primary source (e.g. Database) and stored in Cache. That piece of data can be retrieved and served from Cache rather than being fetched directly from the original source of data i.e. Database. Once the cache is flushed or cleaned, fresh or new data will be updated in the Cache.

Declare new cache

  • Create a file cache.xml inside following directory app/code/[Namespace]/[module]/etc/
  • Add the following content

Create Cache Model

  • Create file Type.php inside following directory app/code/[Namespace]/[module]/Model/Cache/
  • Add the following content:

  • After that, go to System -> Cache Management
  • You will be able to see your Folio3 Custom Cache
  • Your cache is now successfully created!

How to store data into custom cache?

You can store data in a serialized form into your custom cache in the following way

1. Pass this argument to constructor \Magento\Framework\App\CacheInterface $cache

 2. Inside constructor, add $this->_cache = $cache;

3. Then use the following snippet to store data

How to retrieve data from custom cache in Magento 2?

You can easily retrieve data from your custom cache using below snippet

How to invalidate custom cache?

    • You would definitely want to invalidate your custom cache after making any changes to the data stored in your custom cache
    • You can invalidate your custom cache in the following way
      1. Pass this argument to constructor \Magento\Framework\App\Cache\TypeListInterface $typeList
      2. Inside the constructor, add $this->_typeList = $typeList;
      3. Then use the following snippet to store data

How to flush custom cache ?

    • You can flush cache in the following ways
      1. Go to System -> Cache Management and flush Folio3 Custom Cache
      2. Programatically, you can flush in a following way
        • Pass this argument to constructor \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        • Inside the constructor, add $this->cacheTypeList = $cacheTypeList;
        • Then use the following snippet to flush your custom cache

Conclusion

Using caching mechanism in your application can drastically boost your site’s performance. This will help return much faster responses to users who are requesting data from the store. Instead of fetching from the databases, the requested data will be returned from the cache.

The concept is similar to Full Page Cache. FPC caches data like Product Detail Pages, Category Listing Pages. You can similarly use custom caching to store commonly requested data as well.

For example list of cities that are displayed at checkout’s shipping/billing address form can be cached. So that every time a user goes to the checkout page, a list of cities will be returned from the cache instead of the database directly. In this way, you can reduce a large number of requests from database

Don’t forget to share your feedback in the comments section. Thank you :)

 

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Latest From Our Blog

A Complete Guide to Custom Caching in Magento 2...

November 29, 2019
In this blog, I will explain how to create your own cache in Magento 2, and how to read and write data from that custom cache. I hope after reading...
Read more

React Hooks

September 3, 2019
React is a javascript framework for building interactive client side interfaces along with the integration with back-end server. React considers th...
Read more

What is Flutter and Why everyone is talking abo...

June 19, 2019
Getting Started With Flutter: You might have heard the word “Flutter”. If not, at least, you have read it by now. So what is Flutter? And why is ev...
Read more