MicroBoy 2015-04-02
这里用的是SpringMVC-3.2.4和Ehcache-2.7.4
介绍二者集成之前,先介绍下GoogleCode上的ehcache-spring-annotations项目
/** * ehcache-spring-annotations简介 * @see ---------------------------------------------------------------------------------------------------------------- * @see 关于Spring与Ehcache的集成,googlecode上有一个经Apache认证的开源项目叫做ehcache-spring-annotations * @see 目前已经到了1.2.0版本,它只是简化了Spring和Ehcache集成的复杂性(事实上我觉得完全没必要,因为它俩集成并不复杂) * @see 尽管如此还是要提一下,它的项目主页为https://code.google.com/p/ehcache-spring-annotations/ * @see 这篇文章中描述了其用法http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/ * @see ---------------------------------------------------------------------------------------------------------------- * @see 1)使用时在项目中引入ehcache-spring-annotations-1.2.0.jar和guava-r09.jar两个jar文件 * @see 2)然后在applicationContext.xml按照如下配置 * @see <beans xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" * @see xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring * @see http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"> * @see <ehcache:annotation-driven/> * @see <ehcache:config cache-manager="cacheManager"> * @see <ehcache:evict-expired-elements interval="60"/> * @see </ehcache:config> * @see <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> * @see <property name="configLocation" value="classpath:ehcache.xml"/> * @see </bean> * @see 3)最后在需要缓存的方法上使用@Cacheable和@TriggersRemove注解即可 * @see 经我亲测,@TriggersRemove(cacheName="..", when="..", removeAll=true)可移除缓存中的全部对象 * @see 但若写成@TriggersRemove(cacheName="..", when="..")则不会移除缓存中的单一的或所有的对象,即缓存中的对象无变化 * @see ---------------------------------------------------------------------------------------------------------------- * @create Oct 3, 2013 4:56:35 PM * @author 玄玉<http://blog.csdn.net/jadyer> */下面开始罗列示例代码,首先是web.xml
package com.jadyer.service; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * Cacheable注解负责将方法的返回值加入到缓存中 * CacheEvict注解负责清除缓存(它的三个参数与@Cacheable的意思是一样的) * @see ---------------------------------------------------------------------------------------------------------- * @see value------缓存位置的名称,不能为空,若使用EHCache则其值为ehcache.xml中的<cache name="myCache"/> * @see key--------缓存的Key,默认为空(表示使用方法的参数类型及参数值作为key),支持SpEL * @see condition--只有满足条件的情况才会加入缓存,默认为空(表示全部都加入缓存),支持SpEL * @see ---------------------------------------------------------------------------------------------------------- * @see 该注解的源码位于spring-context-3.2.4.RELEASE-sources.jar中 * @see Spring针对Ehcache支持的Java源码位于spring-context-support-3.2.4.RELEASE-sources.jar中 * @see ---------------------------------------------------------------------------------------------------------- * @create Oct 3, 2013 6:17:54 PM * @author 玄玉<http://blog.csdn.net/jadyer> */ @Service public class UserService { private Map<String, String> usersData = new ConcurrentHashMap<String, String>(); public UserService(){ System.out.println("用户数据初始化..开始"); usersData.put("2", "玄玉"); usersData.put("3", "我的博客:http://blog.csdn.net/jadyer"); System.out.println("用户数据初始化..完毕"); } //将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的key //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值 @Cacheable(value="myCache", key="'get'+#userNo") public String get(String userNo){ System.out.println("数据库中查到此用户号[" + userNo + "]对应的用户名为[" + usersData.get(userNo) + "]"); return usersData.get(userNo); } @CacheEvict(value="myCache", key="'get'+#userNo") public void update(String userNo){ System.out.println("移除缓存中此用户号[" + userNo + "]对应的用户名[" + usersData.get(userNo) + "]的缓存"); } //allEntries为true表示清除value中的全部缓存,默认为false @CacheEvict(value="myCache", allEntries=true) public void removeAll(){ System.out.println("移除缓存中的所有数据"); } }下面是UserController.java
package com.jadyer.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.jadyer.service.UserService; /** * 这里用到的jar如下 * aopalliance.jar * commons-logging-1.1.2.jar * ehcache-2.7.4.jar * slf4j-api-1.7.5.jar * spring-aop-3.2.4.RELEASE.jar * spring-beans-3.2.4.RELEASE.jar * spring-context-3.2.4.RELEASE.jar * spring-context-support-3.2.4.RELEASE.jar * spring-core-3.2.4.RELEASE.jar * spring-expression-3.2.4.RELEASE.jar * spring-web-3.2.4.RELEASE.jar * spring-webmvc-3.2.4.RELEASE.jar * @create Oct 3, 2013 6:22:43 PM * @author 玄玉<http://blog.csdn.net/jadyer> */ @Controller @RequestMapping("cacheTest") public class UserController { @Resource private UserService userService; @RequestMapping(value="/get/{userNo}", method=RequestMethod.GET) public String get(@PathVariable String userNo, Model model){ String username = userService.get(userNo); model.addAttribute("username", username); return "getUser"; } @RequestMapping(value="/update/{userNo}", method=RequestMethod.GET) public String update(@PathVariable String userNo, Model model){ userService.update(userNo); model.addAttribute("userNo", userNo); return "updateUser"; } @RequestMapping(value="/removeAll", method=RequestMethod.GET) public String removeAll(){ userService.removeAll(); return "removeAllUser"; } }最后把剩下的4个jsp页面列出来,首先是index.jsp
测试时,访问index.jsp之后点击各个链接并依次观察控制台输出即可
缓存有效果的特征是:第二次查询数据时不会访问数据库(即不打印日志)