Spring Cache 使用
来源:互联网 发布:windows软件包下载 编辑:程序博客网 时间:2024/06/07 09:02
结合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
- 使用Spring Cache
- Spring Cache 使用
- Spring 使用cache
- Spring使用Cache
- Spring Cache 使用
- Spring使用Cache
- Spring使用Cache
- Spring使用Cache
- Spring Cache使用详解
- Spring使用Cache
- 使用Spring Cache
- Spring使用Cache
- Spring使用Cache
- spring cache简单使用
- Spring 使用Cache
- Spring使用Cache
- Spring使用Cache
- spring中使用cache
- BMP位图信息·DIB结构
- git 代码协同之回车问题
- hdu(5402)——Travelling Salesman Problem(模拟题)
- common.io中Tailer类读取文件内容乱码问题解决
- Android基础入门教程——2.3.8 SeekBar(拖动条)
- Spring Cache 使用
- JAVA--Runtime类研究
- 用makefile编译生成.a文件(linux静态库),并编译进可执行文件中
- Linux运行脚本作为守护进程的各种方法
- Android设计模式系列-单例模式
- 工具的使用问题解决(eclipse)
- 创建线程的两种常用方法
- C语言数组和指针的区别
- [数据库] 关系型数据库与NoSql