SSH框架学习笔记(五)----hibernate开启二级缓存


hibernate开启二级缓存

  1. 在hibernate.cfg.xml文件中开启二级缓存:
    1. 设置启用二级缓存:
      <property name="hibernate.cache.use_second_level_cache">true</property>

       

    2. 设置二级缓存的实现类:
      <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

      需要导入hibernate-ehcache.jar包

      <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.1.2.Final</version>
          </dependency>

       

  2.   配置缓存实现类所需的配置文件ehcache.xml
    <?xml version="1.0" encoding="GBK" ?>
    <ehcache>
        <diskStore path="java.io.tmpdir"/>
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskPersistent="false"/>
    </ehcache>

     

  3. 对具体的类用注解方式开启二级缓存
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM