hibernate caching strategy

hibernate caching strategy:

    hibernate cache points:

  1. hibernate framework built-in

  2. hibernate enforcement

  3. session level, a session can access the same, different session inaccessible.

  4. query.list () every time a query from the database, not the query cache query; query.iterator () queries from the cache. If the query can not, go to the database query.

  5. Clear Cache method 5.1: session.evict (emp) clears the cache of the specified object. 5.2: session.clear () the session to clear all cache. After If cleared, query.iterator () can not be cached, or queries from the database.

  6. Transaction level, does not appear concurrency issues

Important secondary cache :( global cache, and the application cache, the cache is pluggable)

    1. The third-party, such as EHcache, OScache etc.

    2. Off by default

    3. application level, as close sessionFactory closed.

    4. All of the session shared secondary cache, the cache may be running in memory, when the memory is full it is hardened to a hard disk

    The two buffers survival time, can configure the maximum number of cache objects, a maximum survival time of the cache, the cache maximum idle time

    6. secondary cache usage scenario, the query is more, no less concurrent or concurrent, not content with high timeliness, unlike stocks, the content is not high degree of importance.

    7. The secondary cache configuration

① lead pack EHcache.jar and commons.logging.jar, and ehcache.xml file

② hibernate.cfg.xml configuration file

<property name="cache.use_second_level_cache">true</property>
<!-- 该属性指定对哪些类进行read-only缓存, 是可选方案。这样在hbm.xml中就可以不用配置<cache>标签 -->
<!-- <class-cache usage="read-only"  class="com.hibernate.many2many.entity.Employee"/> -->
<property name="cache.provider_configuration_file_resource_path">/ehcache.xml</property>
<!-- 指定二级缓存的外部实现类 -->
<property name="cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>

③ caching policy configuration file mapping entity class in hbm.xml

<!-- 放在class标签内的第一行 -->
<!-- usage指定配置缓存模式为只读,include指定是为所有属性添加缓存还是为非懒加载属性配置缓存。region指定专门为该对象创建一个缓存区域 -->
<cache usage="read-only" include="all" region="Employee"/>

Please refer to the detailed configuration:

Ehcache.xml configuration file

Two cache configuration

Published 21 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34291570/article/details/82776944