How to Manually Clearing Your Magento Cache

Cheap Drupal 8.0.4 Hosting Recommendation

How to Manually Clearing Your Magento Cache

Sometimes when moving, migrating or changing domain names for your Magento installation you may run in to issues with the cache redirecting users to the old domain.

What Are the Benefits of Clearing the Cache?

Cache (/kæʃ/ kash, or /ˈkeɪʃ/ kaysh) is a hardware or software component that stores data so that future requests for that data can be served faster.

So it’s all about storing the requested data for easier access later. Both the browser and the server make a cache for faster performance.

There are two caching techniques: full-page caching and object (fragment) caching. The names speak for themselves: you can both cache the entire page or only separate objects in it, like heavy images.

You can’t even imagine how much your website speed impacts your traffic and conversions.

If you’re using cache correctly, then your cached website will perform faster, and with higher speed come high conversion rates.

Let’s see what are the two types of cache that you should know about.

Types of Web Caching

There are two main types of website caching.

1. Browser caching

The browser cache is a temporary storage space in the hard drive of the user’s computer. The storage contains files like HTML, JavaScript, CSS, images and all kinds of media.

Once a user visits your website, the browser saves some of the data to display it later without reaching out to your server again. Thus, browser cache speeds up your website for returning visitors.

Now you must wonder what happens with cache when you update your website. Do the visitors keep seeing the older version? The answer is no; not if you’re doing it right.

To avoid this situation, most websites use ETag and Expire Tag.

The first one — ETag — is a token that compares the cached version of the website with the one on the actual server. When they vary, the browser requests the updated version of the website.

The second one — Expire Tag — is there to set up the period after which the cached version is removed from the browser.

Website owners/administrators manage this tag and pick the average time they are having between updates.

2. Server caching

Server caching speeds up websites for anyone, not just the returning visitors. This is how it works: when browsers request a webpage, the server takes time to process those requests.

After the first request (from any user) is fulfilled, the server “remembers” it and next time it delivers the same data to anyone faster.

Lower server loads are one of the biggest benefits of caching: millions of users can open the website at the same time, and it won’t blow up.

On top of the hosting servers, there’s a caching system for DNS (domain name system).

See, DNS is often called the phonebook of the Internet. It connects web addresses like 10web.io to hosting servers’ IP addresses like 28.56.8.252.

The system operates through tons of servers called nameservers holding records about these addresses. Caching nameservers (also called DNS cache) store information about DNS queries for as long as the administrator decides. That period is called TTL (time to live) and is essential when you are changing your domain name or hosting.

Caching & CDN

The physical distance between a user and a server they request used to be a real issue for website load speed for a long time. Content delivery (distribution) network — CDN is the solution.

These networks consist of several servers located in different part of the world. They cache the commonly requested files of a web page (static content) and when a user enters a query, the nearest server responds with the ready-to-display content.

The servers are smart enough to recognize the device type, cookie settings, and other information about the user to cache and display the exact required pages.

CDN is also useful to deliver dynamic content — which is unique and not cached — faster.

Why should you clear the cache?

  • To get the newest version of a site. One common reason why you might want to clear your cache is if a website or application has made updates but they don’t show up on your browser because of the old cached website version.
  • To be more secure. If you’re sharing a computer with other people, clearing website cache will help you protect your privacy.
  • To improve performance. Even though caching is meant to improve website performance, if you don’t clear them a while, a lot of data accumulates and has the opposite effect.
  • To fix browser errors. If you see certain sites lagging or errors popping up, better clear the cache just to be sure.

This can make it difficult to login to the administration section to clear the cache, to make the site work. Often you will see that your site gets stuck in a redirect loop that won’t complete. In this quick and easy guide, we will show you how to clear your Magento Cache.

Steps to take

There are three simple steps that can be taken to solve this issue.

  1. Login to your FTP account. This can be done through your FTP Client or Webshell.
  2. Find the Magento installation located in your domain
  3. Remove the content of the following two folder’s
    • /var/cache
    • /var/session

As always we strongly recommend you take a backup of these files before removing them, even though they are only cache files.

How to Clear Cache Programmatically

In case of development, a developer or a request from merchant, he/she needs to clear / flush cache programmically. So today we will show you how to Clear Cache Programmically. Implement these lines of codes in Helper:

<?php
use Magento\Framework\App\PageCache\Version;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Cache\Frontend\Pool;
protected $cacheTypeList;
protected $cacheFrontendPool;
 
public function __construct(
    TypeListInterface $cacheTypeList, 
    Pool $cacheFrontendPool
){
    
    $this->cacheTypeList = $cacheTypeList;
    $this->cacheFrontendPool = $cacheFrontendPool;
 
}
 
public function flushCache(Version $subject)
{
  $_types = [
            'config',
            'layout',
            'block_html',
            'collections',
            'reflection',
            'db_ddl',
            'eav',
            'config_integration',
            'config_integration_api',
            'full_page',
            'translate',
            'config_webservice'
            ];
 
    foreach ($_types as $type) {
        $this->cacheTypeList->cleanType($type);
    }
    foreach ($this->cacheFrontendPool as $cacheFrontend) {
        $cacheFrontend->getBackend()->clean();
    }
}

Now call flushCache() function in a controller or model.

Anjali Punjab

error: Content is protected !!