一、什么是缓存

在服务器内存中拿出一块空间,存储一些数据,这些数据在第一次使用时,需要读取数据库,并装载进缓存,以后再缓存期内,直接取缓存不用再读数据库,这是利用从内存读比数据库度速度快,用空间换时间的机制。

二、优势劣势

优势:用好可以提升性能

劣势:占内存、不及时性

所以有几种情况不要用缓存:

1.内存小的时候

2.更新频率高的时候

3.访问频率低的时候

4.缓存版本多的时候

三、分类

1.一级 -- 默认Session自带的,Session提交之前,访问可以自动读缓存。

*********

2.二级 -- 需要外部扩展的插件,SessionFactory内的缓存,Session关了以后只要,SessionFactory没有close,就可以继续使用缓存。

配置EhCache二级缓存:

1.复制三个包到lib文件夹下;

2.在hibernate.cfg.xml中配置,启动二级缓存

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

3.把ehcache配置文件复制过来

复制代码
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<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>
复制代码

4.在实体对象的映射文件中的<class>下配置缓存。
<cache usage="read-write"/>这句话放在<class>下的第一句

5.如果使用load或get的时候,不需要其它操作,直接使用的二缓存,中间session关闭也没关系

 

查询缓存--根据HQL不同,缓存不同的结果
在二级缓存的基础上。加两块:
1.hibernate.cfg.xml中加上
<property name="hibernate.cache.use_query_cache">true</property> <!-- 缓存查询语句,相同的查询语句就不再去查第二遍,但对象没有缓存 -->
2.在HQL的createQuery()后加上setCacheable(true)。