1. 程式人生 > >一級和二級快取使用詳解

一級和二級快取使用詳解

【1】一級快取

JPA預設情況下和MyBatis一樣開啟一級快取。JPA是針對與entityManager,Mybatis是針對於namespace。

示例程式碼如下:

@Test
public void testSecondLevelCache(){
    Customer customer1 = entityManager.find(Customer.class, 1);
    Customer customer2 = entityManager.find(Customer.class, 1);
}

控制檯輸出如下(將只打印一條SQL語句):

這裡寫圖片描述

【2】二級快取

JPA的二級快取是跨entityManager的,JPA的二級快取需要顯式配置。

persistence.xml配置如下:

<!--ENABLE_SELECTIVE表示只在使用了快取註解(@Cacheable)的上面使用二級快取-->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

<properties>
    <!-- 配置 JPA 實現產品的基本屬性. 配置 hibernate 的基本屬性 -->
    <property name="hibernate.format_sql" value="true"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>

    <!-- 二級快取相關 -->
    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
    <property name="hibernate.cache.use_query_cache" value="true"/>
</properties>

這裡使用的快取產品為ehcache,將ehcache.xml放在src資源路徑下。

ehcache.xml示例如下:

<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.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->

    <!-- Place configuration for your caches following -->

</ehcache>

Customer類修改如下:

@Cacheable(true)//這裡表示啟用二級快取
@Table(name="JPA_CUTOMERS")
@Entity
public class Customer {
    //...
}

測試程式碼如下:

    @Test
    public void testSecondLevelCache(){
        Customer customer1 = entityManager.find(Customer.class, 1);

        transaction.commit();
        entityManager.close();

        entityManager = entityManagerFactory.createEntityManager();
        transaction = entityManager.getTransaction();
        transaction.begin();

        Customer customer2 = entityManager.find(Customer.class, 1);
    }

控制檯輸出如下(將只會有一條語句):

這裡寫圖片描述

綜上可知,JPA的一級(二級)快取和Mybatis的一級(二級)快取相當類似。

只不過這裡JPA的二級快取以entityManager來區分,Mybatis的二級快取以namespace來區分。

【3】配置二級快取的策略

<shared-cache-mode>節點:若 JPA 實現支援二級快取,該節點可配置在當前的持久化單元中是否啟用二級快取,可配置如下屬性值:

ALL:所有的實體類都被快取;

NONE:所有的實體類都不被快取;

ENABLE_SELECTIVE:標識 @Cacheable(true) 註解的實體類將被快取;

DISABLE_SELECTIVE:快取除標識 @Cacheable(false) 以外的所有實體類;

UNSPECIFIED:預設值,JPA 產品預設值將被使用。

示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="jpa-1" transaction-type="RESOURCE_LOCAL">
        <!-- 
        配置使用什麼 ORM 產品來作為 JPA 的實現 
        1. 實際上配置的是  javax.persistence.spi.PersistenceProvider 介面的實現類
        2. 若 JPA 專案中只有一個 JPA 的實現產品, 則也可以不配置該節點. 
        -->
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- 
        配置二級快取的策略 
        ALL:所有的實體類都被快取
        NONE:所有的實體類都不被快取. 
        ENABLE_SELECTIVE:標識 @Cacheable(true) 註解的實體類將被快取
        DISABLE_SELECTIVE:快取除標識 @Cacheable(false) 以外的所有實體類
        UNSPECIFIED:預設值,JPA 產品預設值將被使用
        -->
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

        <properties>
            <!-- 連線資料庫的基本資訊 -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="123456"/>

            <!-- 配置 JPA 實現產品的基本屬性. 配置 hibernate 的基本屬性 -->
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>

            <!-- 二級快取相關 -->
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
        </properties>
    </persistence-unit>
</persistence>