kapok

垃圾桶,嘿嘿,我藏的这么深你们还能找到啊,真牛!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  455 随笔 :: 0 文章 :: 76 评论 :: 0 Trackbacks

http://dev2dev.bea.com.cn/techdoc/webplat/2005041303.html

摘要

  WebLogic Portal 8.1包含了大量允许开发人员最大限度地提高站点性能的功能。其中一个功能就是通过使用Render Cacheable参数实现portlet内容的缓存。虽然在帮助文档中可以找到对该参数的基本描述,但是该参数常常引起人们的误解。本文的目的是提供对该参数的详细分析,并讨论一下该参数的使用场合。

背景
  Render Cacheable参数是portlet定义的一部分。点击portlet定义,检查显示该参数以及与此密切相关的Cache Expires参数的属性编辑器。由于Render Cacheable属性是广义portlet定义的一部分,而不是一个特殊的portlet实例,这就说明该缓存适用于门户中portlet的所有实例。您不能仅为某些实例,而不为其他的实例指定缓存。
  即使缓存受全局控制。但是相同portlet的不同实例可以使用不同的缓存。为了证明这一点,我们将分析这样一种情况,即开发人员创建了一个名称为GenericDisplayFilePortlet的portlet。该portlet可以通过个性化设置,基于传递到portlet的属性显示来自文件系统的静态HTML文件。如果该portlet在门户主页上具有两个不同的实例,每一个实例显示一份不同的文件,则这两个实例的缓存将不发生冲突,每一个portlet实例维护它自己的缓存。
  Cache Expires参数是指缓存内容失效之前,缓存保持活动状态的秒数。如果Cache Expires设置为–1,则任何缓存都不会发生,不管Render Cacheable是否设置为true。

缓存作用域
  很多开发人员对portlet缓存产生的第一个误解是作用于应用。这是错误的。portlet cache作用于会话层,这说明每一位个体用户可以拥有portlet缓存的个体版本。
  这就使portlet缓存机制完美的适合缓存个性化的内容,但是portlet缓存不适合缓存静态内容,这些内容也会呈现给所有用户,并且很少会失效。举例来说,按月显示公司新闻简报(company newsletter)的portlet不应当使用Render Cacheable参数;而应当使用可以在应用层提供缓存的替代机制。
  如果已经为新闻简报设置了Render Cacheable参数,那么,您可以在会话作用域上为每一次独立会话缓存其内容。这将在多个缓存中重复不必要的内容,从而降低性能。
  一种简单的替代方法是将缓存移动到一个较低的层中,并且让portlet正在面对的JSP或者页面流在该层上执行缓存。在这种情况中,我们可以使用包含在WebLogic中的wl:cache标签在应用层上缓存内容。有关wl:cache 标签的详细文档请参考下面内容。
  另一种替代方法是在门户缓存中缓存信息;这允许管理员通过使用WebLogic门户包含的门户管理员(Portal Administrator)工具,轻松地调整这些缓存。您可以通过各种方式来使用门户缓存,包括:

  • 直接使用门户缓存API,您可以按需存储和检索显示信息。请参看参考资料一节了解更多信息。
  • 另一种方法是,您可以创建一个定制标签来替代wl:cache,该标签可以在门户缓存中缓存信息,以实现更好的受控于门户管理员。附录中包含了如何实现该操作的一个简单示例。

缓存生命周期
  我们将讨论的有关portlet的第二个误解涉及到缓存的生命周期。开发人员往往认为是否始终使用缓存取决于以前是否调用过portlet,是否还没有超过失效时间。事实上,有几个原因可以解释在转换portlet的时候为什么不使用缓存。了解缓存生命周期非常重要,可以确保从缓存中获得最大程度的优势。
  我们将执行一个非常有趣的实验,即创建一个使用小型JSP显示日期和时间的简单portlet,如下所示:

<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="netui-tags-databinding.tld" prefix="netui-data"%>
<%@ taglib uri="netui-tags-html.tld" prefix="netui"%>
<%@ taglib uri="netui-tags-template.tld" prefix="netui-template"%>
<p>
Date and time is:<%=new Date(System.currentTimeMillis())%>
</p>

  将portlet的Render Cacheable参数设置为true,并且将Cache Expires设置为30秒。将portlet存放在测试门户中,然后从WebLogic Workshop运行门户。您应当看到显示日期和时间的门户。现在,请按浏览器的刷新(Refresh)按钮,并观察日期和时间的变化;没有使用portlet缓存。此时,可以原谅您会认为portlet缓存已经中断,但是事实上,它工作正常,如下所示。
  为了深入分析该示例,接下来创建一个具有简单页面流的新portlet,该页面流由单个JSP页面组成。JSP包含一个简单的表格和Submit(提交)按钮,如下所示:

<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="netui-tags-databinding.tld" prefix="netui-data"%>
<%@ taglib uri="netui-tags-html.tld" prefix="netui"%>
<%@ taglib uri="netui-tags-template.tld" prefix="netui-template"%>
<p>Postback example</p>
<netui:form action="refresh">
<netui:button>Refresh</netui:button>
</netui:form>

  当点击了Refresh(刷新)按钮之后,该portlet简单的初始化一次服务器postback操作。在设置该示例的时候,请确保在页面流编辑器中链接刷新操作和JSP页面。现在,将该portlet存放在缓存先前日期和时间portlet的同一门户页面中。
  如果您现在从Workshop运行门户,将可以看到两种portlets:已经缓存的日期和时间portlet和刚刚创建的postback portlet。如果您刷新浏览器,就会出现与先前一样的行为,即没有使用缓存。现在,尝试点击postback portlet中的Refresh(刷新)按钮,然后观察postback执行时,缓存是否按照预期使用。
  该示例的要点是证明缓存仅用于portlet生命周期的某些特定部分,尤其是,当没有明确地证明文件时,缓存仅在portlet加载状态中使用。不调用该部分portlet生命周期的操作将不使用缓存,因此,缓存和portlet生命周期是密切联系的。下表综述了各种门户交互如何影响缓存。
交互 使用缓存
通过外部链接访问门户。
已经缓存的portlet初始化postback。
与已经缓存的portlet在同一页面中的Portlet初始化postback。
在门户中的程序块和页面之间导航。
_nfls=false参数通过URL传递到门户中。


  总而言之,为了最大限度地提高缓存的有效性,掌握与门户交互相关的缓存生命周期处理方式非常重要。如果一种利用缓存的情况涉及到大量没有调用缓存的交互,则应当仔细考虑portlet缓存的替代方案。

结束语
  在这里总结我们对portlet缓存机制的讨论。如您所见,portlet缓存机制不只是表面现象,其内涵丰富,开发人员应当仔细考虑对每一种情况的选择,以最大限度地获取缓存的性能优势。
  portlet cache的理想用例是专为每一位用户订制,并且经常失效的内容。其他用例应当考虑更加恰当的缓存替代方法,例如使用wl:cache标签或者门户缓存。

参考资料

  • Workshop文档
  • wl:cache文档
  • Portal Cache文档

附录。缓存标签
示例1. 定制标签
  这是一个简单的示例,说明了如何使用定制标签在门户缓存中缓存内容。它不是一款全面的缓存解决方案,而是证明我们如何把门户缓存作为Render Cacheable的一种替代来缓存信息。

package com.bea.ps.portal.tags.cache;

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 String CACHE_NAME = "tagContentCache";
  private String name;

  /**
  * Name to use to uniquely identify content within the cache
  */
  public String getName() {
    return name;
  }

  public void setName(String value) {
    name = value;
  }

  public int doStartTag() throws JspException {

    Cache cache = CacheFactory.getCache(CACHE_NAME);
    //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(pageContext.getRequest().getParameter("_cache_refresh"))) {
      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(CACHE_NAME);
    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;
  }
}
示例2. TLD文件
这是一份伴随的JSP标签描述符文件。
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>com.bea.ps.portal.tags.cache.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>
  </tag>
</taglib>
示例3. 示例缓存标签及使用
这是一个说明缓存标签如何用在JSP中的示例。
<%@ taglib uri="/WEB-INF/tld/example.tld" prefix="example" %>

<example:cache name="newsletter">
    <jsp:include page="newsletter.html">
</example:cache>
Gerald Nunn是BEA Systems Professional Services的一位业务原理咨询师。

原文出处
http://dev2dev.bea.com/products/wlportal81/articles/portlet_caching.jsp
posted on 2005-04-22 21:42 笨笨 阅读(332) 评论(0)  编辑  收藏 所属分类: J2EEALLWeblogic Portal

只有注册用户登录后才能发表评论。


网站导航: