Hibernate缓存

缓存分类

  • Session级别的缓存:缓存周期比较短,session关闭后缓存失效
  • SessionFactory级别的缓存:生命周期长,SessionFactory缓存也叫作二级缓存,Hibernate默认没有开启二级缓存。

SessionFactory级别的缓存(二级缓存)

Hibernate对二级缓存有一个简单的实现,但是一般情况下不会使用Hibernate默认的缓存实现,而是使用第三方厂商提供的缓存产品,如:EHCache、OSCache。

使用二级缓存的方法:

1、开启二级缓存,也就是在hibernate.cfg.xml配置二级缓存的提供商,如下:

<!-- 指定缓存的类也就是开启二级缓存,默认不启用 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

2、指定哪些类要使用缓存

  • 在hibernate.cfg.xml使用如下节点指定:
<class-cache usage="read-only" class=""/>
  • 在hbm文件中指定,如下:
<cache usage="read-only"/>

EHCache缓存的配置文件,该文件放在类路径下

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

</ehcache>

Query.list():默认情况下,该方法默认不会使用二级缓存,如果希望该方法使用缓存需要在hibernate.cfg.xml中开启查询缓存才行

<property name="cache.use_query_cache">true</property>

还需要在查询时设置使用缓存,不过该缓存是根据条件缓存的,如果条件改变,还是不会使用缓存的

List list2 = session2.createQuery("FROM Employee e WHERE e.id<18")//
                .setCacheable(true)// 是否使用查询缓存
                .list();

Query.iterate():该方法会使用缓存,因为这个方法是先查询所有符合条件的id集合,在一个一个的查询数据,所有能够实用上缓存,但是该查询会有N+1次查询的问题,提升性能有限,不建议使用

update()方法和delete()方法对缓存的影响:会让二级缓存中相关的数据失效,下次使用这些数据时会重新到数据库中加载



原文地址:https://www.cnblogs.com/heml/p/4753179.html