6 Replies Latest reply on Jun 9, 2011 5:19 AM by Khoi Nguyen

    ETag : Use Cached Contents

    Umesh Pathak Newbie

      I tried the response.getCacheControl().setUseCachedContent(true). but it does not work.

       

       

       

      Here is my portlet class

       

      -

      package com.mycompany.frs.portlet;

      public class CacheDemo extends GenericPortlet

      public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException

      {

      System.out.println("**---- " + this.getClass().getName() + "'s doView is started");

       

      if (request.getETag() != null)

      {

      if (request.getETag().startsWith("MY_ETAG"))

      {

      System.out.println("/////// MARKUP IS VALID");

      response.getCacheControl().setExpirationTime(30);

      response.getCacheControl().setUseCachedContent(true);

      return;

      }

      }

      else

      {

      response.getCacheControl().setETag("MY_ETAG" + (new Date()));

      response.getCacheControl().setExpirationTime(30);

      PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/CacheDemo.jsp");

      dispatcher.include(request, response);

      }

       

      }

       

       

      portlet.xml =

       

      <portlet>

              <description>CacheDemo</description>

              <portlet-name>CacheDemo</portlet-name>

               <display-name>CacheDemo</display-name>

               <portlet-class>com.mycompany.frs.portlet.CacheDemo</portlet-class>      

               <supports>

                   <mime-type>text/html</mime-type>

                   <portlet-mode>VIEW</portlet-mode>

               </supports>

               <supports>

                   <mime-type>text/html</mime-type>

                   <portlet-mode>EDIT</portlet-mode>

               </supports>

               <supports>

                   <mime-type>text/html</mime-type>

                   <portlet-mode>HELP</portlet-mode>

               </supports>

               <portlet-info>

                   <title>CacheDemo</title>

                   <short-title>CacheDemo</short-title>

               </portlet-info>

               <expiration-cache>30</expiration-cache>          

           </portlet>

       

      CacheDemo.jsp

       

       

      <%@page import="java.util.Date"%>

      <H1> Hello Friends, I am cached contents !! </H1>

      <%= new Date() %>

       

      Platform : Jboss GateIn 3.1

       

      Problem :

       

      When I refresh the page after 30 seconds, the portlet vanishes from the Portal page.

      When I again refresh the page immediately, the portlet appears, with the cached contents. That is, it does not show the current date

      -----------------

      Thanks

       

      Umesh Pathak

        • 1. ETag : Use Cached Contents
          Khoi Nguyen Novice

          I confirmed that this's a bug from GateIn, UIPortletLifecycle hadn't cared the case PortletInvocationResponse is instance of RevalidateMarkupResponse.

           

          Please create an issue at https://jira.jboss.org/browse/GTNPORTAL

           

          Thank in advance

          • 2. ETag : Use Cached Contents
            Khoi Nguyen Novice

            I've just created an issue for your problem, u can watch its if you want https://issues.jboss.org/browse/GTNPORTAL-1780

            • 3. ETag : Use Cached Contents
              Umesh Pathak Newbie

              Thanks Khoi !!

              • 4. ETag : Use Cached Contents
                Minh Hoang TO Novice

                The exception thrown as you made the second render request is due to a bug of GateIn Portlet Container as mentionned by Khoi.

                 

                The second issue ( the portlet window still kept the outdate content) is caused by the ETag validation in your portlet

                 

                if (request.getETag().startsWith("MY_ETAG"))

                {

                 

                }

                ...........

                response.getCacheControl().setETag("MY_ETAG" + (new Date()));

                • 5. ETag : Use Cached Contents
                  Minh Hoang TO Novice

                  If you clear the browser history, then refresh the page (should put the portlet window on the Home page and access as annonymous user), the content of portlet is updated

                  • 6. Re: ETag : Use Cached Contents
                    Khoi Nguyen Novice

                    In your example, the if (request.getETag().startsWith("MY_ETAG")) clause alway returns true, so the cached value is rendered instead of get the fresh content.

                     

                    The cached portlet is used when you want to have a portlet which display news, information.. for instance. And the ETag is set as a validation token to indicate that the content has been changed.

                     

                    The following code is an example for this case:

                     

                     

                    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
                    {
                       if (request.getETag() != null)
                       {
                          if (markupIsStillValid(request))
                          {
                             System.out.println("\n\nMARKUP IS VALID");
                             response.getCacheControl().setExpirationTime(30);
                             response.getCacheControl().setUseCachedContent(true);
                             return;
                          }
                       }
                    
                       response.getCacheControl().setETag(String.valueOf(System.currentTimeMillis()));
                       response.getCacheControl().setExpirationTime(30);
                       getPortletContext().getRequestDispatcher("/WEB-INF/CacheDemo.jsp").include(request, response);
                    }
                    
                    private boolean markupIsStillValid(RenderRequest request)
                    {
                       boolean valid = false;
                       if (Math.abs((Long.valueOf(request.getETag()) - System.currentTimeMillis())) < 100000)
                       {
                          valid = true;
                       }
                       return valid;
                    }