本章介绍Ehcache结合Spring实现服务层的缓存,其他还有数据层缓存和页面缓存。
添加依赖
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.2.0</version>
</dependency>
spring配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
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.2.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.tmg" />
<!-- SpringMVC配置 -->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:view-controller path="/" view-name="forward:/index.jsp"/>
<!-- 缓存配置 -->
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- Spring自带的Cache!!! Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="movieFindCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/> //对应@Cacheable(value="movieFindCache"
</set>
</property>
</bean>
-->
<!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<!-- <property name="shared" value="true"/> true时为单例-->
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
</beans>
在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;
@Service
public class UserService {
private Map<String, String> usersData = new ConcurrentHashMap<String, String>();
public UserService(){
System.out.println("用户数据初始化..开始");
usersData.put("1", "test1");
usersData.put("2", "test2");
System.out.println("用户数据初始化..完毕");
}
//将查询到的数据缓存到movieFindCache中,并使用方法名称加上参数中的userNo作为缓存的key
//通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值
@Cacheable(value="movieFindCache", key="'get'+#userNo")
public String get(String userNo){
System.out.println("数据库中查到此用户号[" + userNo + "]对应的用户名为[" + usersData.get(userNo) + "]");
return usersData.get(userNo);
}
@CacheEvict(value="movieFindCache", key="'get'+#userNo")
public void update(String userNo){
System.out.println("移除缓存中此用户号[" + userNo + "]对应的用户名[" + usersData.get(userNo) + "]的缓存");
}
//allEntries为true表示清除value中的全部缓存,默认为false
@CacheEvict(value="movieFindCache", allEntries=true)
public void removeAll(){
System.out.println("移除缓存中的所有数据");
}
}
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;
@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";
}
}
要点:
在spring配置文件:
可使用ecache, 用ecache.xml构建cacheManagerFactory,然后把factory注入实现cacheManager。
也可以使用Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的cacheManager(该功能是从Spring3.1开始提供的)。
在service层:
然后使用@Cacheable等注解对缓存进行操作,这些注解都是org.springframework.cache提供的。
参考