1. Add a cache
1) Login to the portal administrator console.
2) Choose Configuration Settings > Service Administration.
3) Select the Cache Manager node in the tree.
4) In the Browse tab, click Add Cache.
5) Enter the name of the cache.
6) Optionally, enter or modify the default cache configuration settings.
7) Click Update.
2. Use the cache in JSP
To understand the cache easily, Please take the following sample。
1) Create a tag to use the cache.
Following is the source of the tag:
//CacheContentTag
import com.bea.p13n.cache.Cache;
import com.bea.p13n.cache.CacheFactory;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Caches content in body of tag in a portal cache called 'tagContentCache'.
* While not as full featured as wl:cache, caching content in the portal cache
* does have an advantage of allowing administrators to tune and control the
* cache from the portal administration tool.
*/
public class CacheContentTag extends BodyTagSupport {
private static final long serialVersionUID = 516362934781811427L;
private String cacheName;
private String name;
private String flush;
public int doStartTag() throws JspException {
Cache cache = CacheFactory.getCache(cacheName);
// If the request parameter _cache_refresh is set to true,
// we invalidate the cache for this entry. This is identical
// to the way wl:cache behaves.
if ("true".equals(this.getFlush())) {
pageContext.getRequest().setAttribute(
"_cache_refresh", "true");
cache.remove(name);
}
if (cache.get(name) != null)
return BodyTagSupport.SKIP_BODY;
else
return BodyTagSupport.EVAL_BODY_BUFFERED;
}
public int doEndTag() throws JspException {
Cache cache = CacheFactory.getCache(cacheName);
String content = (String) cache.get(name);
// If content is null then it has not been previously cached
if (content == null) {
// Get content of tag body
content = getBodyContent().getString();
// Put content in portal cache
cache.put(name, content);
}
try {
// Out put content to page
pageContext.getOut().print(content);
} catch (IOException e) {
throw new JspException(e);
}
return BodyTagSupport.EVAL_PAGE;
}
public void release() {
super.release();
name = null;
this.cacheName = null;
this.flush = null;
}
/**
* Name to use to uniquely identify content within the cache
*/
public String getName() {
return name;
}
public void setName(String value) {
name = value;
}
public String getFlush() {
return flush;
}
public void setFlush(String flush) {
this.flush = flush;
}
public String getCacheName() {
return cacheName;
}
public void setCacheName(String cacheName) {
this.cacheName = cacheName;
}
}
Define the TLD file:
//myTag.tld file<! DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
< taglib >
< tlib-version > 1.0 </ tlib-version >
< jsp-version > 1.2 </ jsp-version >
< short-name > example </ short-name >
< display-name > Cache Tag </ display-name >
< description >
A tag for caching content in a portal cache
</ description >
< tag >
< name > cache </ name >
< tag-class > ps.taglib.CacheContentTag </ tag-class >
< body-content > jsp </ body-content >
< display-name > Cache </ display-name >
< description > Caches content in the portal cache tagContentCache </ description >
< attribute >
< name > name </ name >
< required > true </ required >
< rtexprvalue > true </ rtexprvalue >
< description > The name to use to uniquely identify the content </ description >
</ attribute >
< attribute >
< name > flush </ name >
< required > false </ required >
< rtexprvalue > true </ rtexprvalue >
</ attribute >
< attribute >
< name > cacheName </ name >
< required > true </ required >
< rtexprvalue > true </ rtexprvalue >
</ attribute >
</ tag >
</ taglib >
Add this TLD file to /WEB-INF/tld/
Define the taglib to web.xml file
< taglib >
< taglib-uri > weblogic-tags.tld </ taglib-uri >
< taglib-location >
/WEB-INF/lib/weblogic-tags.jar
</ taglib-location >
</ taglib >
</ jsp-config >
2) Use the CacheContent Tag in JSP
Create the test.jsp in WebContent/
The source of test.jsp:
< %@ taglib uri ="/WEB-INF/tld/myTag.tld" prefix ="my" % >
< html >
< body >
< %String test = "test cache" ; % >
< my:cache name ="test" cacheName ="testCache" >
< %=test % >
</ my:cache >
</ body >
</ html >
Run this JSP in web server:
Change the value of test. <%String test = "change test cache value"; %> and refresh the JSP:
Change this line <my:cache name="test" cacheName="testCache"> to
<my:cache name="test" cacheName="testCache" flush="true"> and refresh the JSP:
3. Reference
You can use the Cache tag which is defined in weblogic-tags.jar by weblogic to implement cache.
Find the reference article by