OSCache 缓存框架使用,方便应用到java开发的网站中

   OSCache标记库由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。 OSCache是个一个广泛采用的高性能的J2EE缓存框架,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");
			}
		}

	}

}


完毕,进行应用测试。

你可能感兴趣的:(java,框架,Web,jsp,cache)