Hibernate 集合成 Ehcache缓存
Hibernate 缓存分为:
1、session 缓存
每个session拥有一个缓存区,不同session不能共享该缓存
2、二级缓存
二级缓存是SessionFactory缓存,就是不同session拥有的公共缓存区
3、 查询缓存
二级缓存只缓存对象,而不会缓存如字符串、取数组这类的数据。
查询缓存,不仅会缓存对象还可以缓存字符串、取数组
二级缓存配置
spring配置文件中配置hibernate属性
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
</props>
</property>
ehcache.xml配置缓存模块
<!--各配置参数的含义:
maxElementsInMemory: 缓存中允许创建的最大对象数
eternal: 缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds: 缓存数据的钝化时间,也就是在一个元素消亡之前,两次访问时间的最大时间间隔值,
这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds: 缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
overflowToDisk: 内存不足时,是否启用磁盘缓存。
name:缓存模块名称
--> <cache name="security.cache" maxElementsInMemory="3000" eternal="false" overflowToDisk="false" timeToIdleSeconds="18000" timeToLiveSeconds="36000" diskPersistent="false"/>
hbm.xml中配置缓存的实体
<class name="security.entity.RoleRelation" table="SYS_SEC_ROLE_RELATION">
<cache usage="read-write" region="security.cache" />
<id name="id" type="java.lang.String" column="ID">
<generator class="uuid" />
</id>
<property name="roleId" type="java.lang.String" column="ROLE_ID" />
<property name="managedRoleId" type="java.lang.String" column="MANAGED_ROLE_ID" />
</class>