简体   繁体   English

spring data jpa做缓存吗

[英]does spring data jpa do caching

I've got a simple repository that looks like this 我有一个看起来像这样的简单存储库

public interface PlayerLevelRepository extends CrudRepository<PlayerLevel, Integer> {
    @Query("FROM PlayerLevel WHERE exp <= :exp ORDER BY exp DESC")
    List<PlayerLevel> findClosestToExperienceLevel(@Param("exp") long exp);
}

I was analyzing the response time from rests and saw that the first rest execution was ten times slower than the next. 我正在分析来自休息的响应时间,发现第一次休息执行的速度比下一次慢十倍。

So I decided to check the time of repository method (I don't have any other time consuming actions there) like this 所以我决定检查存储库方法的时间(那里没有其他耗时的操作)

@Override
public int findClosestLevel(long exp) {
    long startTime = System.nanoTime();

    int closestLevel = levelRepository.findClosestToExperienceLevel(exp).get(0).getLevel();

    long endTime = System.nanoTime();
    LOGGER.info("Execution time: {}", endTime - startTime);
    return closestLevel;
}

And I saw that the first invocation spends 35012440 aka 35 ms . 而且我看到第一次调用花费了3501244035 ms Second, third and other invocations spend ten times less time - 2194712 or 2ms , 3058421 or 3ms and so on. 第二,第三和其他调用花费的时间减少了21947122ms30584213ms等等。

My question is - does Spring Data JPA cache query results of something? 我的问题是Spring Data JPA缓存某些查询结果?

Spring Data JPA is "just" a wrapper (or a nice facade) over JPA, so underneath you still have standard JPA capabilities. Spring Data JPA是JPA的“包装”(或漂亮的外观),因此在其下面仍具有标准的JPA功能。 You have 1lvl cache on the EntityManager / Session level, and 2lvl cache on EntityManagerFactory / SessionFactory level. 您在EntityManager / Session级别具有1lvl缓存,在EntityManagerFactory / SessionFactory级别具有2lvl缓存。 There are more types of cache, like: query result cache etc. You can read more about them here (it's for older Hibernate version, but it should still be valid). 缓存的类型更多,例如:查询结果缓存等。您可以在此处了解更多有关它们的信息 (适用于较早的Hibernate版本,但它仍然有效)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM