您的位置:首页 > 编程语言 > Java开发

springmvc+oscache 简单实例

2013-08-13 15:44 507 查看
OSCache由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。

先做一个页面的实例:

工程目录结构如图:






步骤1:web.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>oscache-pro</display-name>

<!-- 设置spring容器上下文配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 设置log4j配置文件上下文 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>

<!-- 设置log4j自动刷新时间间隔 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>

<!-- 上下文监听类,确保spring容器已加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 上下文监听类,确保log4j加载 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- 配置spring mvc 核心Servlet -->
<servlet>
<servlet-name>oscache_project</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>oscache_project</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- oscache tld -->
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>

<!-- 定义缓存过滤器,定义特定资源的缓存 (待用对象缓存的时候再用,先留着)
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>60</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
-->
</web-app>


步骤2:oscache_project-servlet.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 
<!-- 对Controller包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="oscache.my.web"/>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/views/" p:suffix=".jsp"/>

</beans>


步骤3:applicationContext.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 (如果你需要用到业务逻辑层的接口)
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
-->
</beans>


步骤4:log4j.properties配置文件如下:

log4j.rootLogger=DEBUG,stdout,file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=oscache.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n

log4j.logger.org.apache.commons=ERROR
log4j.logger.com.opensymphony.oscache.base=INFO


步骤5:oscache.properties配置文件如下:

# CACHE IN MEMORY
cache.memory=true

# CACHE DIRECTORY
cache.path=D:\\temp\\cache

# CACHE SIZE
cache.capacity=1000


步骤6:OSCacheController控制器接口类代码

package oscache.my.web;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("cache.do")
public class OSCacheController {

@RequestMapping(params="method=c1")
public String cache1(ModelMap model){
model.addAttribute("date1", new Date().toString());
model.addAttribute("date2", new Date().toString());
model.addAttribute("date3", new Date().toString());
return "cache1";
}
}


步骤7:cache1.jsp(是views/cache1.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>oscache 1</title>
</head>
<body>
没有缓存的日期: ${date1 }<p>
<!--自动刷新-->
<cache:cache time="5">
每5秒刷新缓存一次的日期: ${date2 }
</cache:cache>
<!--手动刷新-->
<cache:cache key="testcache"  scope="application">
手动刷新缓存的日期: ${date3 } <p>
</cache:cache>
<a href="cache2.jsp">手动刷新</a>
</body>
</html>


步骤8:cache2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
缓存已刷新...<p>
<cache:flush key="testcache" scope="application"/>
<a href="cache.do?method=c1">返回</a>
</body>
</html>


刷新页面,查看页面显示时间的变化,你就知道了.

对象缓存还没测试,有高手研究的指教一下.不胜感激.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: