JPA學習筆記(11)——使用二級快取
阿新 • • 發佈:2019-01-24
一級快取
查詢兩次id為1的user
User user1 = entityManager.find(User.class, 1);
User user2 = entityManager.find(User.class, 1);
結果發現只調用了一次sql查詢,因為使用了一級快取
如果查詢一次後,關掉entityManager,再查詢
User user1 = entityManager.find(User.class, 1);
entityManager.close();
entityManager = factory.createEntityManager();
User user2 = entityManager.find (User.class, 1);
發現查詢了兩次,因為entityManager關閉之後,快取也就沒有了。
使用二級快取
所謂的二級快取,也就是可以跨entityManager的快取,也就是說:就算你關閉了entityManager,快取也依然在。
在配置檔案persistence.xml中配置
<!-- 二級快取相關 -->
<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"/>
快取需要以下jar包:
在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>
啟用二級快取:
1.在實體類上加註解@Cacheable(true)
@Cacheable(true)
@Table(name="T_USER")
@Entity
public class User ...
2.在配置檔案persistence.xml中配置二級快取的策略
<!--
配置二級快取的策略
ALL:所有的實體類都被快取
NONE:所有的實體類都不被快取.
ENABLE_SELECTIVE:標識 @Cacheable(true) 註解的實體類將被快取
DISABLE_SELECTIVE:快取除標識 @Cacheable(false) 以外的所有實體類
UNSPECIFIED:預設值,JPA 產品預設值將被使用
-->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
注意:這個配置要放在provider 節點和class 節點後面
再次執行
User user1 = entityManager.find(User.class, 1);
entityManager.close();
entityManager = factory.createEntityManager();
User user2 = entityManager.find(User.class, 1);
結果只調用了一次sql查詢語句,說明二級快取 起作用了。