OSCacheキャッシュフレームワークが使用され、javaが開発したウェブサイトに簡単に適用できます.


OSCacheタグライブラリはOpenSymphonyによって設計され、既存のJSPページ内で高速メモリバッファを実現する機能を提供する画期的なJSPカスタムタグアプリケーションです.OSCacheは広く採用されている高性能J 2 EEキャッシュフレームワークであり、OSCacheはJavaアプリケーションの一般的なキャッシュソリューションに使用できます.
   使用方法:
1、oscacheのjarパッケージをwebプロジェクトのlibの下にコピーします.
2、WEB-INFでoscacheのtldファイルを追加し、oscache.tld

<?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.6</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>oscache</short-name>
    <uri>http://www.opensymphony.com/oscache</uri>
    <display-name>OSCache Tag Library</display-name>
    <description>OSCache - see http://www.opensymphony.com/oscache</description>

    <tag>
        <name>cache</name>
        <tag-class>com.opensymphony.oscache.web.tag.CacheTag</tag-class>
        <body-content>JSP</body-content>
        <description>A tag to cache post-processed JSP contents</description>

        <attribute>
            <name>time</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>duration</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>cron</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>refreshpolicyclass</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>refreshpolicyparam</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>scope</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>refresh</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>mode</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>key</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>groups</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>

        <attribute>
            <name>language</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

    <tag>
        <name>usecached</name>
        <tag-class>com.opensymphony.oscache.web.tag.UseCachedTag</tag-class>
        <description>A tag to tell the cache to use the cached version</description>

        <attribute>
            <name>use</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

    <tag>
        <name>flush</name>
        <tag-class>com.opensymphony.oscache.web.tag.FlushTag</tag-class>
        <description>A tag to flush the cache</description>

        <attribute>
            <name>scope</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>key</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>group</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>language</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>pattern</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

    <tag>
        <name>addgroup</name>
        <tag-class>com.opensymphony.oscache.web.tag.GroupTag</tag-class>
        <description>A tag to add a group to an ancestor cache tag</description>
        <attribute>
            <name>group</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

    <tag>
        <name>addgroups</name>
        <tag-class>com.opensymphony.oscache.web.tag.GroupsTag</tag-class>
        <description>A tag to add a comma-delimited list of groups to an ancestor cache tag</description>
        <attribute>
            <name>groups</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
</taglib>

3、web.xmlファイルにoscache.tldを登録する

<!--        -->
  <jsp-config>
  	<!-- oscache web     -->
<taglib> 
    <taglib-uri>oscache</taglib-uri> 
    <taglib-location>/WEB-INF/oscache.tld</taglib-location> 
</taglib>
  </jsp-config>

4、srcでファイルoscache.propertiesを追加する

cache.capacity=1000

5、ページ使用キャッシュラベルtest.jsp

<%@ taglib uri="oscache" prefix="cache" %>
//...

<cache:cache key="key1" time="-1">
//      	 
</cache:cache>


6.キャッシュされたデータを除去する(データが更新された場合)

OSCacheHandler cacheUtil = new OSCacheHandler(request,Constants.OSCACHE_APPLICATION);
		cacheUtil.remove(key);//key="key1"

OSCacheHandler.java

package cn.changtusoft.publicplatform.util.oscache;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import cn.changtusoft.publicplatform.service.common.SystemException;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;

/**
 * oscache web        
 * 
 */
public class OSCacheHandler {
	private ServletCacheAdministrator admin = null;
	/**
	 * A cache group. If specified, all content in that group will be flushed
	 */
	String group = null;

	/**
	 * Tag key.
	 */
	String key = null;

	/**
	 * if pattern value is specified, all keys that contain the pattern are
	 * flushed.
	 */
	String pattern = null;
	String scope = null;
	int cacheScope = -1;

	/**
	 * The ISO-639 language code to distinguish different pages in application
	 * scope.
	 */
	private String language = null;
	private HttpServletRequest request;

	public void setScope(String value) {
		if (value != null) {
			if (value
					.equalsIgnoreCase(ServletCacheAdministrator.SESSION_SCOPE_NAME)) {
				cacheScope = PageContext.SESSION_SCOPE;
			} else if (value
					.equalsIgnoreCase(ServletCacheAdministrator.APPLICATION_SCOPE_NAME)) {
				cacheScope = PageContext.APPLICATION_SCOPE;
			}
		}
	}

	public ServletCacheAdministrator getAdmin() {
		return admin;
	}

	public void setAdmin(ServletCacheAdministrator admin) {
		this.admin = admin;
	}

	public int getCacheScope() {
		return cacheScope;
	}

	public void setCacheScope(int cacheScope) {
		this.cacheScope = cacheScope;
	}

	public String getScope() {
		return scope;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	private static final long serialVersionUID = 2005659972627911969L;

	public OSCacheHandler(HttpServletRequest request, String scope) {
		this.request = request;
		if (admin == null)
			admin = ServletCacheAdministrator.getInstance(request.getSession()
					.getServletContext());
		setScope(scope);
	}

	//         ;
	public void remove(String key) {
		if (group != null) // We're flushing a group
		{
			if (cacheScope >= 0) {
				Cache cache = admin.getCache(request, cacheScope);
				cache.flushGroup(group);
			} else {
				throw new SystemException(
						"A cache group was specified for flushing, but the scope wasn't supplied or was invalid");
			}
		} else if (pattern != null) // We're flushing keys which contain the
									// pattern
		{
			if (cacheScope >= 0) {
				Cache cache = admin.getCache(request, cacheScope);
				cache.flushPattern(pattern);
			} else {
				throw new SystemException(
						"A pattern was specified for flushing, but the scope wasn't supplied or was invalid");
			}
		} else if (key == null) // we're flushing a whole scope
		{
			if (cacheScope >= 0) {
				admin.setFlushTime(cacheScope);
			} else {
				admin.flushAll();
			}
		} else // we're flushing just one key
		{
			if (cacheScope >= 0) {
				String actualKey = admin.generateEntryKey(key, request,
						cacheScope, null);
				Cache cache = admin.getCache(request, cacheScope);
				cache.flushEntry(actualKey);
			} else {
				throw new SystemException(
						"A cache key was specified for flushing, but the scope wasn't supplied or was invalid");
			}
		}

	}

}

完了し、応用テストを行います.