打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
spring-boot(六)缓存配置

这篇文章主要介绍下spring-boot如何集成缓存。
java的缓存框架有很多种,官方也都给了很多缓存的支持。

官方的这些缓存想必大家在项目中多多少少都有接触到。
由于ehcache3.x实现了jsr-107的标准接口,这里我主要介绍spring-boot下的JCache (JSR-107) 和EhCache 3的集成整合。

案例代码在 “spring-boot(五)自定义属性配置” 的基础上改造

项目结构图

1。 在pom中引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>javax.cache</groupId>
  7. <artifactId>cache-api</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.ehcache</groupId>
  11. <artifactId>ehcache</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-test</artifactId>
  16. <scope>test</scope>
  17. </dependency>

2。在 src/man/resources 目录下建立 ehcache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <config
  3. xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  4. xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
  5. xmlns='http://www.ehcache.org/v3'
  6. xsi:schemaLocation="
  7. http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd
  8. http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">
  9. <cache-template name="heap-cache">
  10. <resources>
  11. <heap unit="entries">2000</heap>
  12. <offheap unit="MB">100</offheap>
  13. </resources>
  14. </cache-template>
  15. <cache alias="Hello" uses-template="heap-cache">
  16. <expiry>
  17. <ttl unit="seconds">3</ttl>
  18. </expiry>
  19. </cache>
  20. </config>

这里特别注意下ehcahce2.x和ehcahce3的配置是不同的,这也是我选择讲3的一个原因吧

3。 在service层增加缓存 (@CacheConfig@Cacheable

  1. @Service
  2. @CacheConfig(cacheNames = "Hello")
  3. public class HelloService {
  4. private Logger log = LoggerFactory.getLogger(this.getClass());
  5. @Autowired
  6. Dao dao;
  7. @Cacheable
  8. public List<HelloBean> queryHellos() {
  9. log.info("查询了数据库");
  10. List<HelloBean> hellos = dao.queryForList(HelloBean.class, "select * from hello ");
  11. return hellos;
  12. }
  13. public HelloBean queryHello(Integer id) {
  14. HelloBean hello = dao.queryForObject(HelloBean.class, "select * from hello where id = ? ", id);
  15. return hello;
  16. }
  17. }

在这里特别说明下,之前的代码 HelloBean 类没有序列化,缓存的存储是需要对象序列化的,所以 修改代码片段
public class HelloBean implements Serializable{

4。 在主启动类上增加启用缓存的注解 @EnableCaching

  1. @EnableCaching
  2. @SpringBootApplication
  3. public class MainApplication {
  4. public static void main(String[] args) throws Exception {
  5. SpringApplication.run(MainApplication.class, args);
  6. }
  7. }

5。 在 application.properties 中指定缓存的配置文件路径

  1. spring.cache.jcache.config=classpath:ehcache.xml

到此缓存配置已经搭建完成,下面我们写个单元测试来看下效果。
在目录 src/test/java 下建立单元测试类

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = MainApplication.class)
  3. public class MyTest {
  4. private Logger log = LoggerFactory.getLogger(this.getClass());
  5. @Autowired
  6. private CacheManager cacheManager;
  7. @Autowired
  8. HelloService service;
  9. @Before
  10. public void before() {
  11. log.info("cacheManager {}", cacheManager);
  12. }
  13. @Test
  14. public void cache() throws InterruptedException {
  15. log.info("访问了 {} 接口 , 现在时间为 {} ", "service.queryHellos()", new Date());
  16. service.queryHellos();
  17. log.info("访问了 {} 接口 , 现在时间为 {} ", "service.queryHellos()", new Date());
  18. service.queryHellos();
  19. Thread.sleep(5000);
  20. log.info("访问了 {} 接口 , 现在时间为 {} ", "service.queryHellos()", new Date());
  21. service.queryHellos();
  22. }
  23. }

运行测试类,观察控制台输出


同时我们也会看到缓存使用的是ehcache来管理的。

源代码附件: my-springboot-6.tar.gz

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SpringBoot中引入Ehcache3.x
Spring Boot (五): Redis缓存使用姿势盘点
Spring Boot 整合 Redis 实现缓存操作
java ssh maven pom文件
spring-boot如何集成缓存
SpringBoot缓存技术
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服