配置 Portlet 的高速缓存策略
Portlet 的片段高速缓存要求您在 cachespec.xml 文件中定义高速缓存策略(在 Portlet Web 应用程序归档 (WAR) 文件中或全局性地定义高速缓存策略)。如果没有定义适用于特定 Portlet 的高速缓存策略,那么不会高速缓存该 portlet。
WebSphere® Application Server 高速缓存策略在定义高速缓存标识方面具有很大的灵活性,并提供大量与各个 Portlet 的特定需求相匹配的失效规则。您可以定义的高速缓存策略未必要与按照 Java Portlet 规范定义的高速缓存行为一致。 以下章节给出了一些建议,指导您如何开发 cachespec.xml 文件的功能,以定义符合规范的高速缓存策略。
高速缓存到期。Portlet 在 portlet.xml 部署描述符的 <expiration-cache> 元素中定义高速缓存到期时间。如果不存在该此元素,或者包含零值,那么不高速缓存 portlet。只能在部署描述符中定义 Portlet 的高速缓存到期时间;在 cachespec.xml 文件中指定的所有高速缓存超时值均无效。
<component id="com.ibm.wsspi.portletcontainer.user_cache_scope" type="attribute"/>
此属性包含以下值:- 定义公共高速缓存范围的 Portlet 中的值 public。
- 定义专用高速缓存范围的 Portlet 中当前登录的用户标识。
- 如果定义专用高速缓存范围的 Portlet 中没有用户登录,那么是 Null(匿名)。
如果您要高速缓存 Portlet 内容以进行匿名访问(即使是在定义专用高速缓存范围的 Portlet 中),请向高速缓存关键组件添加 <required>false</required>。这表示所有的匿名浏览访问都将检索相同的高速缓存内容。
<component id="javax.portlet.lifecycle_phase" type="attribute">
<value>RENDER_PHASE</value>
</component>
此高速缓存关键组件只高速缓存到 Portlet 的呈递请求。通过添加 RESOURCE_PHASE 来高速缓存其他的资源请求。
在许多情况下,最好的方法是为资源请求定义单独的 <cache-id> 元素。资源标识可用于 com.ibm.wsspi.portletcontainer.resource_id 请求属性,用来高速缓存资源请求中的关键生成。<component id="com.ibm.wsspi.portletcontainer.all_parameters" type="attribute">
<required>false</required>
</component>
如果您为特定的 Portlet 编写高速缓存策略,并且准确地知道哪个请求参数定位哪个 Portlet 视图,那么较为有效的方法是使用高速缓存键中特定的
<parameter> 元素来仅高速缓存最重要的 Portlet 视图。其他高速缓存关键组件。根据您的使用方案,您可能需要在高速缓存键中包含其他信息(如果返回的内容与其相关,例如 Portlet 方式和窗口状态,或多语言门户网站中的键语言环境)。在支持不同标记类型的多设备门户网站中,返回的内容类型还应该作为高速缓存键的一部分。portlet 的内容类型可用于 com.ibm.wsspi.portletcontainer.response_contenttype 请求属性中。
高速缓存无效。操作和事件请求到 Portlet 的 Java Portlet 规范状态必须使所有当前高速缓存的内容无效。 portlet 高速缓存定义通常允许同一时间高速缓存某个 Portlet 的多个视图。要使它们全部无效,请使用 cachespec.xml 的依赖性标识机制。
<dependency-id>action
<component id="" type="portletWindowId"/>
<component id="com.ibm.wsspi.portletcontainer.user_cache_scope" type="attribute"/>
</dependency-id>
<invalidation>action
<component id="" type="portletWindowId"/>
<component id="com.ibm.wsspi.portletcontainer.user_cache_scope" type="attribute"/>
<component id="javax.portlet.lifecycle_phase" type="attribute" ignore-value="true">
<value>ACTION_PHASE</value>
<value>EVENT_PHASE</value>
</component>
</invalidation>
Sample cachespec.xml file
<?xml version="1.0" ?>
<!DOCTYPE cache SYSTEM "cachespec.dtd">
<cache>
<cache-entry>
<class>portlet</class>
<name>MyPortlet</name>
<property name="consume-subfragments">true</property>
<cache-id>
<component id="" type="portletWindowId"/>
<component id="com.ibm.wsspi.portletcontainer.user_cache_scope" type="attribute"/>
<component id="" type="portletWindowState">
<!-- minimized portlets are not cached -->
<not-value>minimized</not-value>
</component>
<component id="" type="portletMode"/>
<component id="" type="locale"/>
<component id="com.ibm.wsspi.portletcontainer.response_contenttype" type="attribute"/>
<component id="com.ibm.wsspi.portletcontainer.all_parameters" type="attribute">
<required>false</required>
</component>
<component id="javax.portlet.lifecycle_phase" type="attribute">
<value>RENDER_PHASE</value>
</component>
</cache-id>
<dependency-id>action
<component id="" type="portletWindowId"/>
<component id="com.ibm.wsspi.portletcontainer.user_cache_scope" type="attribute"/>
</dependency-id>
<invalidation>action
<component id="" type="portletWindowId"/>
<component id="com.ibm.wsspi.portletcontainer.user_cache_scope" type="attribute"/>
<component id="javax.portlet.lifecycle_phase" type="attribute" ignore-value="true">
<value>ACTION_PHASE</value>
<value>EVENT_PHASE </value>
</component>
</invalidation>
</cache-entry>
</cache>