Hibernate- Cache

Cache

Cache in the session inside, also called session cache / transactional cache, as the session closed disappear.

principle

session caching will find the corresponding record by the primary key, if not in the cache, the cache from the database join.

management

A cache acts on too small a custom configuration.

Common method

Common method

Features

clear()

Empty the cache

flush()

Refresh the cache, database synchronization

setReadOnly()

Read-only

Secondary cache

A secondary cache SessionFactory management, the entire application is cached. Hibernate comes with the second-level cache is very sad, commonly used third-party L2 cache.

Scope

Cache for frequently read, modify extraordinary data.

Not suitable for frequent changes, high concurrency, data sharing of data.

Cache should be properly designed to improve the cache hit rate, reducing the cache size. 66

EHCache

EHCache support read-only, not strictly read and write, write, write the cache files.

Configuration

  1. In hibernate.cfg.xml setting cache.provider_class

    <session-factory>

            <-! EhCacheProvider deprecated in favor of EhCacheRegionFactory

            <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

            -->

    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory</property>

            <! - use the query cache ->

            <property name="hibernate.cache.use_query_cache">true</property>

    </session-factory>

  2. Add ehcache.xml profile, set EHCache parameters

    <ehcache>

        <diskStore path="java.io.tmpdir"/>

        <defaultCache maxElementInMemory="10000" maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="60" timeToLiveSeconds="120" overflowToDisk="true"/>

        <Cache name = "POJOs  class ( including the package name )"  ... ( parameter with defaultCache) />

    </ehcache>

    Parameter Description

parameter

Explanation

diskStore

When the number of cache exceeds maxElementInMemory time, save the file to the directory into the cache

java.io.tmpdir represents a relative path generated automatically

defaultCache

The default cache parameters

maxElementInMemory (expired)

内存中可缓存的数量(弃用)

maxEntriesLocalHeap

内存中可缓存的数量

详见Ehcache04——设置缓存的大小

maxElementsOnDisk

硬盘最大缓存个数

eternal

true表示永不过期,则超时设置无效

timeToIdleSconds

过期之前的最大空闲时间,单位秒

timeToLiveSeconds

过期之前的最大生存时间,单位秒

overflowToDisk

缓存数量超过maxElementInMemory时,是否将缓存写入硬盘

cache

为具体类设定具体参数,当没有该类没有设定cache时,使用defaultCache

60秒未使用或存储时间超过120秒,则移除

  1. 修改表映射文件,使用缓存

    添加<cache usage=类型/>

    usage可选类型为:read-onlyread-writenonstrict-read-writeEnCache不支持transactional

    可选region,默认为本类名,也可指定为cachename

    cache标签可放在class下,也可在集合下。当在集合下时,若集合增删则该缓存失效。

管理

常用方法

功能

evict(Class)

移除该实体类的所有缓存

evict(Class, key)

移除该实体类中Object指定主键的缓存

evictCollection(Collection)

移除该集合中的所有元素的缓存

evictEntity(String)

String是实体类的类名,功能同evict

 

Guess you like

Origin www.cnblogs.com/AlMirai/p/12546589.html