
package demoPkg;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

import java.io.PrintWriter;
import java.io.CharArrayWriter;

import oracle.jsp.jwcache.CachePolicy;
import oracle.jsp.jwcache.ExpirationPolicy;
import oracle.jsp.jwcache.StringSectionId;
import oracle.jsp.jwcache.CacheBlock;
import oracle.jsp.jwcache.CacheClientUtil;

public class DemoCacheServlet extends HttpServlet{

  public void service(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
  {
    // standard writer object from servlet engine
    PrintWriter out=response.getWriter();
    ServletConfig config=getServletConfig();

    try {
      CachePolicy cachePolicyObj = CacheClientUtil.lookupPolicy(config,request,
        "/WEB-INF/test-policy.cpd" );  // Note A
      StringSectionId sectionId=new StringSectionId("s1");
      CacheBlock cacheBlockObj=null;

      cacheBlockObj = cachePolicyObj.getCache(request,sectionId); // Note B
      if (!cachePolicyObj.isRecent(cacheBlockObj)) {  // Note C
        CharArrayWriter newOut=new CharArrayWriter();    
        PrintWriter pw=new PrintWriter(newOut);
        
        // actual logic within a cache block 
        pw.println("fragment#1");
        pw.println(new java.util.Date());
        // which generates content into the "out" object
        
        if (cacheBlockObj == null) {   // Note E
          cachePolicyObj.putCache(newOut.toCharArray(),request,sectionId);  // Note F
        }
        
        out.write(newOut.toCharArray());
        // writing out newly created data back to the original writer
      }
      else {
        out.write((char[])cacheBlockObj.getData());  
        // writing the existing cached data to the writer
      }


      sectionId=new StringSectionId("s2");
      long timeToLive = 15;   // now set TTL to 15 on this block
      ExpirationPolicy expirationPolicy = cachePolicyObj.getExpirationPolicy();
      expirationPolicy.setTTL(timeToLive);
      cachePolicyObj.setExpirationPolicy(expirationPolicy);
      cacheBlockObj = cachePolicyObj.getCache(request,sectionId); // Note B
      if (!cachePolicyObj.isRecent(cacheBlockObj)) {  // Note C
        CharArrayWriter newOut=new CharArrayWriter();    
        PrintWriter pw=new PrintWriter(newOut);
        
        // actual logic within a cache block 
        pw.println("fragment#2");
        pw.println(new java.util.Date());
        // which generates content into the "out" object
        
        if (cacheBlockObj == null) {   // Note E
          cachePolicyObj.putCache(newOut.toCharArray(),request,sectionId);  // Note F
        }
        
        out.write(newOut.toCharArray());
        // writing out newly created data back to the original writer
      }
      else {
        out.write((char[])cacheBlockObj.getData());  
        // writing the existing cached data to the writer
      }



    } catch (Throwable th) {
      // your exception handling code here
      th.printStackTrace(out);
    }
  }
}

