Spring Boot 中的二级缓存究竟需要哪些依赖项?

马克_丹尼尔斯

我读了几篇文章。一 - 表示需要这些依赖项:

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.7.1</version>
</dependency>

另一个只有这个:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.2.Final</version>
问题是,为什么在第一个例子中我需要一个 API 来缓存?hibernate 没有自己的吗?为什么在第二个示例中不需要 spring-boot-starter-cache ?

这是我参考的两篇文章。

第一个例子

第二个

巴托什·希曼斯基

这两个教程都展示了不同的缓存方法。

  1. 首先是在 Spring 级别实现 Cache。通过这样做,您可以在 Spring 应用程序中的某些特定方法或对其他 API 的调用上实现缓存。这就是为什么您需要spring-boot-starter-cache并且cache-api-spring-boot-starter-cache因为将它与 Spring 一起使用,cache-api因为您的缓存不会使用 hibernate api。
  2. 其次是在 Hibernate 级别实现缓存。在这里,您的缓存将在调用 DB 期间使用 - 您不能将其用于缓存某些方法、调用其他 API 等等。这就是为什么你不需要spring-boot-starter-cache- 你的代码根本不会使用这些。

如果您只想缓存对 DB 的调用,请使用第二种方法。如果您尝试缓存可以(但不需要)使用 DB 的方法的执行,请使用第一种方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章