Monday 3 June 2019

Custom Deployer Extension in SDL WEB 8.5 - Purge Cache Part - 2

In the previous post, we set up the custom deployer project, discussed its requirement and the use of deployer extension. Today, we are going to continue to use that same project and implement a real-life use-case.

Background:- In our current project we are using SDL WEB 8.5 and .NET DXA 1.8 and to improve the performance of the web application we implemented the custom caching with cache expires after 15 mins, but the business team has this requirement that they want to see the content "Real-time", no delay whenever the publish any new content.

Based on their requirement we came up with this approach and decided to write a custom deployer extension. So, whenever the author publish the page(s), the deployer extension will trigger and clear the cache of that particular page based on the URL extracted from PageMetaData.

In the below code snippet we are reading the page relative URL retrieved from PageMetaData and with the help of string manipulation we've created the absolute URL and based on the Query String parameter we trigger the code to purge the cache. Check the HTTP response code it should be 200 in the logs and refresh the web page to check the latest content.

 // This is called once for each Transport Package that is deployed.  
      @Override  
      public void process(TransportPackage data) throws ProcessingException   
      {  
            log.debug("This is custom logs");  
            String USER_AGENT = "Mozilla/5.0";  
            log.debug("PublicationId : " + String.valueOf(data.getProcessorInstructions().getPublicationId().getItemId()));  
           try  
           {  
            log.debug("Custom Code :- Entered in the try block");  
            PageMetaData pageFile = (PageMetaData)data.getMetaDataFile("Pages");  
            log.debug("Custom code :- Reading PageMetaData");  
            if (pageFile != null)  
      {  
        List<Page> pages = pageFile.getPages();  
        for ( Page page : pages )  
        {  
             log.debug("Custom code :- For loop to get the page url ");  
             log.debug("http://localhost:92"+page.getURLPath()+"?ClearCache=true");   
             String URL = "http://localhost:92"+page.getURLPath()+"?ClearCache=true";  
             URL obj = new URL(URL);  
             HttpURLConnection con = (HttpURLConnection) obj.openConnection();  
             con.setConnectTimeout(5000);  
             log.debug("Custom code :- timeout");  
             con.setRequestMethod("GET");  
             con.setRequestProperty("User-Agent", USER_AGENT);  
             int responseCode = con.getResponseCode();  
             log.debug("Response Code : " + responseCode);  
             if (responseCode == HttpURLConnection.HTTP_OK)  
             {  
              log.debug("Custom Deployer, Cache has been removed and the responseCode is : " + responseCode + "- for URL " + URL);  
             }  
             log.debug("Page ID :" + String.valueOf(page.getId().getItemId()));  
        }  
      }  
            log.debug("Purging is done !! Please check the web page!!");  
           }  
           catch (Exception e)  
     {  
        log.error("Could not get path for publication.", e);  
        return;  
     }  
  }  

Logs generated by the deployer.

Deployer Logs


With the help of the deployer extension, we were able to achieve and deliver the real-time experience and still managing the performance of the site using cache.


Happy Coding and Keep Sharing !!!



No comments:

Post a Comment