緩存分為三種:一級緩存、二級緩存、查詢緩存(三級緩存)
1、一級緩存
Session級緩存,只能在Session內部使用。
2、二級緩存
sessionFactory級別,所有Session可以共用。以配置EHCache為例:
a)新建/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="1200"
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>
b)hibernate.cfg.xml
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
c)在需要使用緩存的實體類上加標注
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
d)測試用例:
@Test

public void testCache1()
{
Session session = sf.openSession();
session.beginTransaction();
Category c = (Category) session.load(Category.class, 1);
System.out.println(c.getName());
session.getTransaction().commit();
session.close();

Session session2 = sf.openSession();
session2.beginTransaction();
Category c2 = (Category) session2.load(Category.class, 1);
System.out.println(c2.getName());
session2.getTransaction().commit();
session2.close();

}
3、查詢緩存(三級緩存)
Query緩存,查詢語句相同時會使用。
a)hibernate.cfg.xml
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="cache.use_query_cache">true</property>
b)測試用例:
@Test

public void testQueryCache()
{
Session session = sf.openSession();
session.beginTransaction();
List<Category> q = session.createQuery("from Category c where c.id>?")
.setCacheable(true)
.setInteger(0, 3)
.list();
session.getTransaction().commit();
session.close();

Session session2 = sf.openSession();
session2.beginTransaction();

List<Category> q2 = session2
.createQuery("from Category c where c.id>?")
.setCacheable(true)
.setInteger(0, 3)
.list();
session2.getTransaction().commit();
session2.close();

}
1、一級緩存
Session級緩存,只能在Session內部使用。
2、二級緩存
sessionFactory級別,所有Session可以共用。以配置EHCache為例:
a)新建/src/ehcache.xml文件,內容如下:



























































































@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
d)測試用例:



















Query緩存,查詢語句相同時會使用。
a)hibernate.cfg.xml



























二級緩存適合放什么對象?
1、經常被訪問
2、改動不大(不會被經常改動)
3、數量有限
load默認使用二級緩存,iterator默認使用二級緩存
list默認往二級緩存加數據,但是查詢的時候不使用
緩存算法:
LRU(Least Recently Used)最近很少使用的對象會被刪除
LFU(Least Frequently Used)命中率低的對象會被刪除
FIFO(First In First Out)先進先出