阅读 15

【JPA专题】11.缓存

缓存

缓存出现的目的就是为增加效率,下面就简单配置和测试一下,最终我们还是要跟Spring弄在一起的,简单测试。

缓存测试代码类:图书类

package com.os.model;

import javax.persistence.*;

@Entity
@Table(name = "jpa_book")
public class Book {

    private Integer bookId;
    private String bookName;

    @Id
    @Column(name = "book_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }
    @Column(name = "book_name")
    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                '}';
    }
}

复制代码

公共的测试代码部分

import com.os.model.Book;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class 缓存测试 {

    private EntityManagerFactory factory ;
    private EntityManager entityManager ;
    private EntityTransaction tx;
    @Before
    public void init(){
        factory = Persistence.createEntityManagerFactory("jpa03");
        entityManager = factory.createEntityManager();
        tx = entityManager.getTransaction();
        tx.begin();
    }
    @After
    public void close(){
        tx.commit();
        entityManager.close();
        factory.close();
    }
}
复制代码

1.多次保存图书类,只能保存一次,默认开启一级缓存

@Test
public void test01(){
    Book b1 = new Book();
    b1.setBookName("西游记");

    entityManager.persist(b1);//第一次保存
    entityManager.persist(b1);//第二次保存
    entityManager.persist(b1);//第三次保存

    System.out.println("b1 = " + b1);
}
复制代码

虽然保存书籍多次,但是当执行第一次之后,在缓存中保存了b1的对象,第二次执行和第三次执行的时候,因为entityManager没有被关闭,一级缓存,判断缓冲池中是否存在该对象,如果存在那么不执行保存操作了。

产生的SQL语句

Hibernate: insert into jpa_book (book_name) values (?)
b1 = Book{bookId=1, bookName='西游记'}
复制代码

2.查询操作

@Test
public void test02() {
    Book b1 = entityManager.find(Book.class, 1);
    System.out.println("b1 = " + b1);
    Book b2 = entityManager.find(Book.class, 1);
    System.out.println("b2 = " + b2);
    Book b3 = entityManager.find(Book.class, 1);
    System.out.println("b3 = " + b3);
}
复制代码

产生的SQL语句如下

Hibernate: select book0_.book_id as book_id1_0_0_, book0_.book_name as book_nam2_0_0_ from jpa_book book0_ where book0_.book_id=?
b1 = Book{bookId=1, bookName='西游记'}
b2 = Book{bookId=1, bookName='西游记'}
b3 = Book{bookId=1, bookName='西游记'}
复制代码

如果关闭entityManager,下一次会重新进行查询

@Test
public void test03() {
    Book b1 = entityManager.find(Book.class, 1);
    System.out.println("b1 = " + b1);

    tx.commit();
    entityManager.close();//关闭了一级缓存

    entityManager = factory.createEntityManager();
    tx = entityManager.getTransaction();
    tx.begin();

    Book b2 = entityManager.find(Book.class, 1);
    System.out.println("b2 = " + b2);
}
复制代码

产生的SQL语句如下:

Hibernate: select book0_.book_id as book_id1_0_0_, book0_.book_name as book_nam2_0_0_ from jpa_book book0_ where book0_.book_id=?
b1 = Book{bookId=1, bookName='西游记'}
Hibernate: select book0_.book_id as book_id1_0_0_, book0_.book_name as book_nam2_0_0_ from jpa_book book0_ where book0_.book_id=?
b2 = Book{bookId=1, bookName='西游记'}
复制代码

3.配置二级缓存

a.pom.xml增加依赖

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>5.4.10.Final</version>
</dependency>
复制代码

b.核心文件配置二级缓存

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="jpa03" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>com.os.model.Student</class>
        <class>com.os.model.ClassInfo</class>

        <!-- 一对一示例 -->
        <class>com.os.model.Manager</class>
        <class>com.os.model.Department</class>

        <!-- 多对多示例 -->
        <class>com.os.model.User</class>
        <class>com.os.model.Role</class>

        <!-- 缓存测试类 -->
        <class>com.os.model.Book</class>

        <!--
            配置二级缓存的策略
            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://127.0.0.1:3306/yf01_mybatis"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>

            <!-- 配置Hibernate的相关操作 -->
            <!-- 配置JPA实现产品的基本属性,配置Hibernate的基本属性 -->
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <!-- 使用 MySQLDialect -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

            <!-- 二级缓存相关 -->
            <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>
复制代码

请看注释内容

c.增加二级缓存的核心配置文件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>

复制代码

d.给持久化类增加响应的注释

@Entity
@Table(name = "jpa_book")
@Cacheable(true)
public class Book 
复制代码

e.测试代码如下

@Test
public void test03() {
    Book b1 = entityManager.find(Book.class, 1);
    System.out.println("b1 = " + b1);

    tx.commit();
    entityManager.close();//关闭了一级缓存

    entityManager = factory.createEntityManager();
    tx = entityManager.getTransaction();
    tx.begin();

    Book b2 = entityManager.find(Book.class, 1);
    System.out.println("b2 = " + b2);
}
复制代码

产生的SQL语句

Hibernate: select book0_.book_id as book_id1_0_0_, book0_.book_name as book_nam2_0_0_ from jpa_book book0_ where book0_.book_id=?
b1 = Book{bookId=1, bookName='西游记'}
b2 = Book{bookId=1, bookName='西游记'}
复制代码
关注下面的标签,发现更多相似文章
评论