<%@ page import="oracle.jsp.jwcache.*,java.io.*" errorPage="error.jsp" contentType="text/html;charset=UTF-8"%>
<title>servletcode.jsp</title>
<pre>
servletcode.jsp
<%
  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 (expires in 25 seconds as per TTL value test-policy)");
    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 (expires in 15 seconds because TTL overrides test-policy value)");
    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
  }


%>
</pre>

