{"id":24762,"date":"2022-01-11T11:06:53","date_gmt":"2022-01-11T11:06:53","guid":{"rendered":"https:\/\/www.webscale.com\/?post_type=blog&p=24762"},"modified":"2023-12-29T15:45:37","modified_gmt":"2023-12-29T20:45:37","slug":"3-ways-write-better-caching-modules-magento","status":"publish","type":"post","link":"https:\/\/www.webscale.com\/blog\/3-ways-write-better-caching-modules-magento\/","title":{"rendered":"3 Ways to Write Better Caching Modules in Magento"},"content":{"rendered":"

Paul Briscoe is Director of Development at Human Element<\/span><\/i><\/p>\n

In today\u2019s marketplace, online merchants are focused on the speed of their website as a key indicator for how they will perform. Outside of actual sales revenue, page speed can be the ultimate measure of success for a team on whether or not they are delivering the best solution for their clients. Development teams go through site speed audits, Google Page Speed reports, and a myriad of other tools to help them understand how they can get the scores of the pages up, all in an effort to make data load fast, retain customers, and increase conversions. This problem is becoming harder, especially when working within a large application like Adobe Commerce (Magento).\u00a0<\/span><\/p>\n

One of the tools a developer can use to drive this measure of success is caching. While caching is one of the two hard problems in Computer Science, doing it right, consistently, can yield great results for the performance of your website, and ultimately, its transactional success in the marketplace.<\/span><\/p>\n

To back it up, every Magento developer has, at some point in their career, experienced problems with caching. In some cases, developers were probably staring at the computer in consternation, wondering why on earth the content was not showing on the page after multiple refreshes and triple-checking the code just deployed.\u00a0<\/span><\/p>\n

To try and prevent developers from going down this rabbit hole again, this article will share 3 best practices that developers can use to improve the performance of their site, by incrementally utilizing the caching tools in their codebase, saving calls to MySQL, and avoiding unnecessary PHP processing.<\/span><\/p>\n

    \n
  1. Keep best practices for cache in mind
    \n<\/strong><\/p>\n

    Why is cache so important in Magento?<\/span><\/em><\/p>\n

    At the end of the day, users are concerned about how long it takes a site\u2019s pages to load. How long it takes the page to load affects Google search ranking and conversion to sales. Utilizing cache allows a developer to have a positive impact on these business goals.<\/span><\/p>\n

    Think about the desired outcome and how users will interact with the code.<\/span><\/em><\/p>\n

    At a high level, if developers are thinking about performance and how users will interact with the site, they\u2019re off to a good start. <\/span>Magento docs allude to this<\/span><\/a> when they state that best practices for cache performance involve <\/span>when and how<\/span><\/i> users clear and invalidate the cache, and are not always about saving the data itself.<\/span><\/p>\n

    For example, a business owner should be able to choose a set of products and then have them appear on a frontend page, but also be able to change them when they need to.\u00a0<\/span><\/p>\n

    The data needs to be retrieved only once.\u00a0<\/span><\/em><\/p>\n

    After the data needed is retrieved, save it into the cache with appropropriate tags so that Magento does not need to purge everything to refresh one piece of code.<\/span><\/p>\n

    \"\"
    \nIn this example, the array of cache tags that we pass to the \u201csave\u201d handler can be a custom tag or an existing type.<\/em><\/p>\n

    Use Redis as the caching backend.<\/span><\/em><\/p>\n

    Magento makes it very easy to save data by cache keys and invalidate by different cache types. There are three methods available by default.<\/span><\/p>\n

    \n