使用动态高速缓存服务

通过将 Servlet , Web Service 和 WebSphere® Application Server 命令的输出高速缓存到内存中,使用动态高速缓存服务来提高应用程序性能。

准备工作

为应用程序开发高速缓存策略。 高速缓存策略为响应高速缓存的内容和响应应该在高速缓存中持续的时间量定义规则。 有关更多信息,请参阅“使用 cachespec.xml 文件来配置可高速缓存的对象”一文。

有关此任务

缺省情况下,动态高速缓存服务处于启用状态。 您可以配置缺省高速缓存实例,如下所示:

过程

  1. 单击 服务器> 服务器类型> WebSphere® 应用程序服务器> server_name > 容器服务> 动态高速缓存服务
  2. 配置缺省高速缓存实例,或者沿链接跳转以启用 Servlet 或 Portlet 高速缓存。
    有关缺省高速缓存设置的更多信息,请参阅“动态高速缓存服务设置”一文。

示例

本示例将使用 cachespec.xml 文件配置动态高速缓存服务的步骤以及显示使用高速缓存标识生成规则、依赖项标识和无效规则的所有步骤都放在一起。

假定 Servlet 管理一个简单的新闻站点。 此 Servlet 使用查询参数“action”来确定请求查看(查询参数“view”)新闻还是更新(查询参数“update”)新闻(由管理员使用)。 另一个查询参数“category”用于选择新闻类别。 假设该站点支持可选的定制布局,该布局存储在使用属性名“布局”的用户会话中。 这里是到该 Servlet 的示例 URL 请求:
  • http://yourhost/yourwebapp/newscontroller?action=view&category=sports (Returns a news page for the sports category )
  • http://yourhost/yourwebapp/newscontroller?action=view&category=money (Returns a news page for the money category)
  • http://yourhost/yourwebapp/newscontroller?action=update&category=fashion (Allows the administrator to update news in the fashion category)

以下步骤说明如何使用 cachespec.xml 文件为此示例配置动态高速缓存服务:

  1. 定义标识 Servlet 所必需的 <cache-entry> 元素。 在这种情况下,Servlet 的 URI 是“newscontroller”,因此这是高速缓存条目 <name> 元素。 因为该示例高速缓存 Servlet 或 Java Server Pages (JSP) 文件,所以高速缓存条目类是“servlet”。
    <cache-entry> 
    <name> /newscontroller </name>
    <class>servlet  </class>  
     </cache-entry>
    
  2. 定义高速缓存标识生成规则。 仅当 action=view 时才高速缓存此 servlet,因此当值等于“view”时,高速缓存标识的某个组件是参数“action”。 新闻类别也是高速缓存标识的必不可少的部分。 该高速缓存标识中包括用户布局的可选会话属性。 现在,高速缓存条目为:
    <cache-entry> 
    	<name> /newscontroller </name>
    	<class>servlet  </class>  
     	<cache-id>
    		<component id="action" type="parameter">
    			<value>view</value>
    			<required>true</required>
    		</component>
    		<component id="category" type="parameter">
    			<required>true</required>
    		</component>
    		<component id="layout" type="session">
    			<required>false</required>
    		</component>
    	</cache-id>
    </cache-entry>
    
  3. 定义依赖项标识规则,对于此 servlet,为该类别添加依赖项标识。 对于此 servlet,将为类别添加依赖项标识。 以后,由于更新事件而使该类别无效时,那么该新闻类别的所有视图都无效。 以下是添加依赖项标识后的高速缓存条目的示例:
    <cache-entry> 
    	<name>newscontroller </name>
    	<class>servlet  </class>  
     	<cache-id>
    		<component id="action" type="parameter">
    			<value>view</value>
    			<required>true</required>
    		</component>
    		<component id="category" type="parameter">
    			<required>true</required>
    		</component>
    		<component id="layout" type="session">
    			<required>false</required>
    		</component>
    	</cache-id>
    	<dependency-id>category
    		<component id="category" type="parameter">
    			<required>true</required>
    		</component>
    	</dependency-id>
    </cache-entry>
    
  4. 定义无效规则。 由于已经定义了类别依赖项标识,因此定义无效规则以在 action=update 时使该类别无效。 要合并条件逻辑,将“ignore-value”组件添加到无效规则中。 这些组件将不添加到无效标识的输出中,而是仅确定是否创建和运行该无效标识。 现在,最后的高速缓存条目如下所示:
    <cache-entry> 
    	<name>newscontroller </name>
    	<class>servlet  </class>  
     	<cache-id>
    		<component id="action" type="parameter">
    			<value>view</value>
    			<required>true</required>
    		</component>
    		<component id="category" type="parameter">
    			<required>true</required>
    		</component>
    		<component id="layout" type="session">
    			<required>false</required>
    		</component>
    	</cache-id>
    	<dependency-id>category
    		<component id="category" type="parameter">
    			<required>true</required>
    		</component>
    	</dependency-id>
    	<invalidation>category
    		<component id="action" type="parameter" ignore-value="true">
    			<value>update</value>
    			<required>true</required>
    		</component>
    		<component id="category" type="parameter">
    			<required>true</required>
         </component>
    	</invalidation>
    </cache-entry>
    

下一步做什么?

您可能要启用动态高速缓存磁盘卸载。 此选项将内存中到期的高速缓存条目移动到磁盘中,以便将来有可能要访问它们。 有关启用磁盘卸载的更多信息,请参阅“配置动态高速缓存磁盘卸载”。