结合MyBatis一起使用,MyBatis相关的内容省略
@Cacheable支持如下参数:根据方法的请求参数对其结果进行缓存
value:缓存位置名称,不能为空.
key:缓存的key,默认为空,支持springEL表达式,如果为空缺省按照方法的所有参数进行组合
condition:触发条件,只有满足条件的情况下才会加入缓存,默认为空,表示全部加入缓存,支持springEL表达式.
@CachEvict支持如下参数:负责清除缓存.
value:缓存位置名称,不能为空.
key:缓存的key,默认为空,支持springEL表达式.
condition:触发条件,只有满足条件的情况下才会清除缓存,默认为空,支持springEL表达式.
allEntries:ture表示清除value中的全部缓存,默认为false.
beforeInvocation:是否在方法执行前就清空,缺省为false,如果指定为 true则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存
@CachePut作用和配置方法
根据方法的请求参数对其结果进行缓存,和@Cacheable不同的是,它每次都会触发真实方法的调用.
value:缓存位置名称,不能为空.
key:缓存的key,可以为空,支持使用SpEL编写,如果为空缺省按照方法的所有参数进行组合.
condition:缓存的条件,可以为空,支持使用SpEL编写,如果为空缓存全部数据.
1.service
@Service("userService")
public class UserService {
@Autowired
private UserMapper userMapper;
@CacheEvict(value="userCache",allEntries=true)
public int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
};
@CacheEvict(value="userCache",allEntries=true)
public User insert(User user) {
//ID自动新增,新增保存时获取新增记录的ID
userMapper.insert(user);
return user;
};
@Cacheable(value="userCache")
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
};
@Cacheable(value="userCache")
public List<User> selectAll() {
return userMapper.selectAll();
};
@CacheEvict(value="userCache",allEntries=true)
public int updateByPrimaryKey(User user) {
return userMapper.updateByPrimaryKey(user);
};
}
2.applicationContext.xml中关于Cache的配置
引入xmlns:cache=”http://www.springframework.org/schema/cache”
<!-- 应用spring cache注解功能 -->
<cache:annotation-driven />
<!-- 创建spring cache bean -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="default" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="userCache" />
</set>
</property>
</bean>
3.测试
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("userService",UserService.class);
User user = userService.selectByPrimaryKey(4);//第一次查询,从数据库获取
System.out.println(user);
user = userService.selectByPrimaryKey(4);//第二次查询,从缓存中获取
System.out.println(user);
user = new User();
user.setName("张三");
user = userService.insert(user);//新增记录,清空所有缓存
System.out.println(user);
user = userService.selectByPrimaryKey(4);//第三次查询,从数据库获取
System.out.println(user);
4.按指定key从缓存中移除
@Service("userService")
public class UserService {
@Autowired
private UserMapper userMapper;
@CacheEvict(value="userCache",key="#id")
public int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
};
@CacheEvict(value="userCache",key="#user.id")
public User insert(User user) {
//ID自动新增,新增保存时获取新增记录的ID
userMapper.insert(user);
return user;
};
@Cacheable(value="userCache")
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
};
@Cacheable(value="userCache")
public List<User> selectAll() {
return userMapper.selectAll();
};
@CacheEvict(value="userCache",key="#user.id")
public int updateByPrimaryKey(User user) {
return userMapper.updateByPrimaryKey(user);
};
}
5.测试
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("userService",UserService.class);
User user = userService.selectByPrimaryKey(4);//第一次查询,从数据库获取
System.out.println(user);
user = userService.selectByPrimaryKey(4);//第二次查询,从缓存中获取
System.out.println(user);
user = new User();
user.setName("张三");
user = userService.insert(user);//新增记录,清空指定缓存
System.out.println(user);
user = userService.selectByPrimaryKey(4);//第三次查询,从缓存获取
System.out.println(user);
参考:
Spring Cache使用 http://liuxing.info/2015/06/18/Spring%20Cache%E4%BD%BF%E7%94%A8/
注释驱动的 Spring cache 缓存介绍 https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
Spring使用Cache、整合Ehcache http://www.mincoder.com/article/2096.shtml