`
java_mike
  • 浏览: 83687 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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");
			}
		}

	}

}


完毕,进行应用测试。
0
0
分享到:
评论

相关推荐

    OsCache缓存框架使用示例

    这里结合 天气预报的webservice 展示了OsCache框架的具体使用方法 项目可直接运行 ,代码简洁清晰

    oscache-java缓存框架

    oscache-java缓存框架插件和安装教程,使用教程一步到位

    OSCache缓存框架的简单用法

    OSCache缓存框架的简单用法,希望对大家有所帮助!!!

    oscache缓存技术

    oscache缓存技术,压缩包中有详细代码及步骤

    oscache缓存技术入门实例

    oscache缓存技术入门实例

    oscache缓存技术应用

    描述了oscahce在JAVA开发中的应用和配置说明

    oscache缓存使用总结.doc

    oscache缓存使用总结

    oscache,缓存机制的使用

    oscache,java,缓存机制的使用

    OSCache配置说明文档

    OSCache是一个广泛采用的高性能的J2EE缓存框架,OSCache能用于任何Java应用程序的普通的缓存解决方案。 OSCache有以下特点:缓存任何对象,你可以不受限制的缓存部分jsp页面或HTTP请求,任何java对象都可以缓存。...

    Java缓存框架简介

    OSCache和Ehcache应用介绍,主要对页面缓存、服务器缓存相关应用的介绍

    oscache的使用实例和详解

    缓存框架oscache 的使用实例和详细解释,

    OSCache缓存技术

    Cache是一种用于提高系统响应速度、改善系统运行性能的技术。尤其是在Web应用中,通过缓存页面的输出...OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。

    oscache缓存中间件

    OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存...OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。

    Hibernate OSCache缓存

    Hibernate OSCache缓存 Hibernate OSCache缓存

    oscache文档

    OSCache是个一个广泛采用的高性能的J2EE缓存框架,OSCache能用于任何Java应用程序的普通的缓存解决方案。OSCache有以下特点:缓存任何对象,你可以不受限制的缓存部分jsp页面或HTTP请求,任何java对象都可以缓存。 ...

    Oscache使用手册

    Cache是一种用于提高系统响应速度、改善系统运行性能的技术。尤其是在Web应用中,通过缓存页面的...OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。。。。

    oscache(JSP定制标记应用)

    javaweb做页面缓存常用,OSCache是一个工业级的J2EE缓存实现。OSCache不但能缓存java对象,还可以缓存页面,http请求和二进制内容,例如pdf文件等。通过应用OSCache,我们不但可以实现通常的Cache功能,还能够改善...

    oscache的使用

    oscache的使用

    Oscache-入门教程.doc

    OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。 OSCache是当前运用最广的缓存方案,JBoss,Hibernate,Spring等都对其有支持。 Oscache的使用非常方便,...

    Oscache框架的搭建步骤

    使用oscache进行缓存,大大提高web系统运行效率

Global site tag (gtag.js) - Google Analytics