一:需要jar包
1、ehcache-2.10.2.jar
2、ehcache-web-2.0.4.jar
3、slf4j-api-1.5.8.jar
4、slf4j-log4j12-1.5.8.jar
5、log4j-1.2.15.jar
由于我是结合Spring操作,所以还需导入Spring相关包
二:在项目根路径src目录下加入Ehcache配置文件ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <!-- 默认缓存 --> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> <!-- 指定名字缓存缓存 --> <cache name="studyCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"/> <!-- 指定名字缓存缓存 --> <cache name="pageCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"/> </ehcache>
三:在web.xml中加入Ehcache页面过滤器
<filter> <filter-name>CachePageCachingFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class> <init-param> <param-name>cacheName</param-name> <param-value>pageCache</param-value> </init-param> </filter> <filter-mapping> <filter-name>CachePageCachingFilter</filter-name> <url-pattern>/pageEhcache/*</url-pattern> </filter-mapping>
这样凡是访问路径带 pageEhcache 都会被缓存起来
四:java代码
package com.ehcache.study.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.ehcache.study.beans.TradeBlotter; import com.ehcache.study.service.IAppTradeBlotterService; @Scope("prototype") @Controller public class IndexCtrl { @Autowired private IAppTradeBlotterService appTradeBlotterService; @RequestMapping("/ehcacheStudy/myTradeBlotter") public ModelAndView myTradeBlotter(HttpServletRequest request,HttpServletResponse response,ModelAndView view)throws Exception{ view.setViewName("/WEB-INF/ehcacheStudy/myTradeBlotter.jsp"); System.out.println("执行myTradeBlotter方法"); String appNo = request.getParameter("appNo"); String opId = request.getParameter("opId"); String fundAcct = request.getParameter("fundAcct"); String fundId = request.getParameter("fundId"); long startTime = System.currentTimeMillis(); List<TradeBlotter> tradeBlotterList = appTradeBlotterService.qryTradeBlotterList(appNo, opId, fundAcct, fundId); long endTime = System.currentTimeMillis(); long excuteTime = endTime-startTime; System.out.println("excuteTime=" + excuteTime); view.addObject("tradeBlotterList", tradeBlotterList); return view; } @RequestMapping("/pageEhcache/myCachePage") public ModelAndView myCachePage(HttpServletRequest request,HttpServletResponse response,ModelAndView view)throws Exception{ view.setViewName("/WEB-INF/pageEhcache/myCachePage.jsp"); System.out.println("执行myCachePage方法"); String appNo = request.getParameter("appNo"); String opId = request.getParameter("opId"); String fundAcct = request.getParameter("fundAcct"); String fundId = request.getParameter("fundId"); long startTime = System.currentTimeMillis(); List<TradeBlotter> tradeBlotterList = appTradeBlotterService.qryTradeBlotterList(appNo, opId, fundAcct, fundId); long endTime = System.currentTimeMillis(); long excuteTime = endTime-startTime; System.out.println("excuteTime=" + excuteTime); view.addObject("tradeBlotterList", tradeBlotterList); return view; } }
结果:当执行http://localhost:8080/pageEhcache/myCachePage?opId=Mobile11请求时,页面被缓存
五:上面代码已经能够缓存页面了,如果还需结合缓存方法则在Spring配置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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"> <mvc:annotation-driven/> <context:annotation-config/> <context:component-scan base-package="com.ehcache.study"/> <!-- 数据库配置 --> <bean id="dataSourceSales" class="org.apache.commons.dbcp.BasicDataSource" > <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@10.1.50.25:1521:tadev"></property> <property name="username" value="fund_sales_dev"></property> <property name="password" value="pass4tadev"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="30000"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <!-- sqlSessionFactory --> <bean id="sqlSessionFactorySales" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceSales"></property> <property name="mapperLocations" value="classpath*:com/ehcache/study/dao/**/*.xml"></property> <property name="configLocation" value="classpath:MybatisConfig.xml"></property> </bean> <bean name="salesConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ehcache.study.dao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactorySales"></property> </bean> <!-- 此处为Ehcache相关配置 --> <cache:annotation-driven cache-manager="cacheCacheManager"/> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> <property name="cacheManagerName" value="myCacheManager"></property> </bean> <bean id="cacheCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean> </beans>
此处注意要加上 <property name="cacheManagerName" value="myCacheManager"></property> ,value的值是自己随便定义, 此处如果不写的话会报already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following 错误
六:service层代码
package com.ehcache.study.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.ehcache.study.beans.TradeBlotter; import com.ehcache.study.dao.IAppTradeBlotterDao; import com.ehcache.study.service.IAppTradeBlotterService; @Service public class AppTradeBlotterServiceImpl implements IAppTradeBlotterService{ @Autowired private IAppTradeBlotterDao appTradeBlotterDao; @Cacheable(value="studyCache") public List<TradeBlotter> qryTradeBlotterList(String appNo, String opId,String fundAcct, String fundId) { System.out.println("执行查询数据库"); return appTradeBlotterDao.selectTradeBlotterList(appNo, opId, fundAcct, fundId); } }
以上就是Ehcache页面缓存和方法缓存,仅供参考,如有不当之处,欢迎大家多多指点!
时间: 2024-12-03 21:12:51