Spring Boot 使用 Caching-EhCache
Spring boot 支持的缓存:
• Generic
• JCache (JSR-107)
• EhCache 2.x
• Hazelcast
• Infinispan
• Couchbase
• Redis
• Caffeine
• Guava
• Simple
最常用的是 EhCache,文档多,资料全
一、 添加依赖
<!-- caching -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
二、 配置文件:
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:config/ehcache.xml
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <cache name="roncooCache" eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="50"></cache> <!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false --> <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 --> <!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。 只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 --> </ehcache>
三、 启用注解支持:
@EnableCaching:启用缓存注解
代码实现:
public interface UserLogCache { /** * 查询 * * @param id * @return */ UserLog selectById(Integer id); /** * 更新 * * @param UserLog * @return */ UserLog updateById(UserLog userLog); /** * 删除 * * @param id * @return */ String deleteById(Integer id); }
实现类:
@CacheConfig(cacheNames = "gusCache") @Repository public class UserLogCacheImpl implements UserLogCache { @Autowired private UserLogDao userLogDao; @Cacheable(key = "#p0") @Override public UserLog selectById(Integer id) { System.out.println("查询功能,缓存找不到,直接读库, id=" + id); return userLogDao.findOne(id); } @CachePut(key = "#p0") @Override public UserLog updateById(UserLog userLog) { System.out.println("更新功能,更新缓存,直接写库, id=" + userLog); return userLogDao.save(userLog); } @CacheEvict(key = "#p0") @Override public String deleteById(Integer id) { System.out.println("删除功能,删除缓存,直接写库, id=" + id); return "清空缓存成功"; } }
注解说明:
@CacheConfig:缓存配置
@Cacheable:应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调
用方法获取数据,然后把数据添加到缓存中。适用于查找
@CachePut:主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。适用于更新和插入
@CacheEvict:主要针对方法配置,能够根据一定的条件对缓存进行清空。适用于删除
测试:
@RequestMapping(value = "/select", method = RequestMethod.GET) public UserLog get(@RequestParam(defaultValue = "1") Integer id) { return UserLogCache.selectById(id); } @RequestMapping(value = "/update", method = RequestMethod.GET) public UserLog update(@RequestParam(defaultValue = "1") Integer id) { UserLog bean = UserLogCache.selectById(id); bean.setUserName("测试"); bean.setCreateTime(new Date()); UserLogCache.updateById(bean); return bean; } @RequestMapping(value = "/del", method = RequestMethod.GET) public String del(@RequestParam(defaultValue = "1") Integer id) { return UserLogCache.deleteById(id); }
- Spring Boot 2.0.0.M7 使用 Caching-EhCache
- (十六)SpringBoot之使用 Caching- - EhCache
- Spring Boot 使用Redis和Ehcache做拥有二级缓存的系统
- Spring Boot 使用 Caching-Redis
- 自制Https证书并在Spring Boot和Nginx中使用
- Spring与ehcache整合,通过注解的方式使用缓存
- 解决使用Spring Boot开发模板视图修改后刷新不显示问题
- Spring Boot 使用465端口发送邮件
- 简单的使用ehcache
- 配置Spring+hibernate使用ehcache作为second-level cache
- Spring Boot使用模板freemarker【从零开始学Spring Boot(转)
- spring boot 使用不同的profile来加载不同的配置文件
- Ehcache配置详解及CacheManager使用
- Spring Boot下Druid连接池的使用配置分析
- spring boot集成ehcache 2.x 用于hibernate二级缓存
- Ehcache 整合Spring 使用页面、对象缓存
- 二级缓存:EHCache的使用
- java 缓存框架java caching system使用示例
- 集群环境中使用 EhCache 缓存系统
- Spring Boot 官方文档学习(一)入门及使用