Hibernate二级缓存的注解(学习笔记)

通过注解配置二级缓存

首先要确定好二级缓存的插件(jar),以及准备好相应二级缓存的配置文件,并且需要在hibernate.cfg.xml文件中启用二级缓存的插件,最后在需要缓存的类上使用@Cache注解配置二级缓存,配置缓存时需要配置缓存的名称、属性、策略(不可省略)

 

  • jar包在hibernate开发包lib/optional/ehcache

 

  • 二级缓存配置文件ehcache:

 

<ehcache>
 
    <!-- Sets the path to the directorywhere cache .data files are created.
 
         If the path is a Java System Propertyit is replaced by
         its value in the running VM.
 
         The following properties aretranslated:
         user.home - User's home directory
         user.dir - User's current workingdirectory
         java.io.tmpdir - Default temp filepath -->
      <diskStore path="java.io.tmpdir"/>  <!-- 缓存到磁盘上的目录 ,默认路径-->
 
 
    <!--Default Cache configuration. Thesewill applied to caches programmatically created through
        the CacheManager.
 
        The following attributes are requiredfor defaultCache:
 
        maxInMemory       - Sets the maximum number of objectsthat will be created in memory
        eternal           - Sets whether elements are eternal.If eternal,  timeouts are ignored and theelement
                            is never expired.
        timeToIdleSeconds - Sets the time toidle for an element before it expires. Is only used
                            if the element isnot eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time tolive for an element before it expires. Is only used
                            if the element isnot eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow todisk when the in-memory cache
                            has reached themaxInMemory 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 foryour cache a WARNING will be issued when the
        CacheManager starts
 
        The following attributes are requiredfor 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 objectsthat will be created in memory
        eternal           - Sets whether elements are eternal.If eternal,  timeouts are ignored and theelement
                            is never expired.
        timeToIdleSeconds - Sets the time toidle for an element before it expires. Is only used
                            if the element isnot eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time tolive for an element before it expires. Is only used
                            if the element isnot eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow todisk when the in-memory cache
                            has reached themaxInMemory limit.
 
        -->
 
    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memoryof 10000 elements, and will expire
        an element if it is idle for more than5 minutes and lives for more than
        10 minutes.
 
        If there are more than 10000 elementsit will overflow to the
        disk cache, which in this configurationwill go to wherever java.io.tmp is
        defined on your system. On a standardLinux system this will be /tmp"
        -->
    <cachename="com.bestone.entity.Account"
       maxElementsInMemory="10000" <!-- 内存最大存放对象数目 -->
       eternal="false"                <!--当前内存中对象是否永久有效 -->
       timeToIdleSeconds="300" <!--最大空闲时间,如果 eternal为true这两个属性失效 -->
       timeToLiveSeconds="600"        <!--缓存中对象最大存活时间 -->
       overflowToDisk="true"        <!--当缓存中对象超出最大存放数目时,是否往磁盘上进行缓存 -->
        />
 
 <!--设置默认的查询缓存区域的属性-->
    <cachename="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="50"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="7200"
overflowToDisk="true"/>
   <!--设置时间戳缓存区域的属性 -->
    <cachename="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/>
   <!--设置自定义命名查询缓存区域的属性,一般可不配置 -->
    <cache name="myCacheRegion"
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"/>
 
 
</ehcache>


 

 

  • Hibernate.cfg.xml中:

<!--启用二级缓存-->
<propertyname="hibernate.cache.use_second_level_cache">true</property>
<!--启动二级缓存ehcache插件 -->
          <propertyname="hibernate.cache.region.factory_class">     org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!--打开查询缓存-->
<propertyname="hibernate.cache.use_query_cache">true</property>
 


  • 实体类中配置二级缓存:

@Cache(region="com.bestone.entity.Account",include="all",usage=CacheConcurrencyStrategy.READ_WRITE)   /*策略不可省略*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值