持久层缓存的级别
Hibernate的一级缓存
Hibernate中的缓存分成两级:一级缓存和二级缓存
Hibernate的一级缓存是内置的缓存,不能通过程序代码或者配置进行人为的取消,同时一级
缓存通过Session对象实现缓存,所以一级缓存也称为Session缓存
一级缓存的管理
使用evict()方法从缓存中移除缓存对象
使用clear()方法从缓存中清除所有对象
使用Session对象contains()判断是否在缓存中(true/false)、flush()和setReadOnly()方法
Hibernate的二级缓存
Hibernate的二级缓存由SessionFactory对象负责管理,是应用级别的缓存,可以缓存整个应
用的持久化对象
Hibernate的二级缓存又称SessionFactory缓存
Hibernate的二级缓存
缓存名称 缓存类型 对应的适配器类
Hashtable 内存 org.hibernate.cache.HashtableCacheProvider
EHCache 内存,硬盘 org.hibernate.cache.EhCacheProvider
OSCache 内存,硬盘 org.hibernate.cache.OSCacheProvider
SwarmCahe 分布式(不支持分布式事务) org.hibernate.cache.SwarmCacheProvider
JBoss Cache1.x 分布式(支持分布式事务) org.hibernate.cache.TreeCacheProvider
JBoss Cache2. 分布式(支持分布式事务) org.hibernate.cache.jbc2.JBossCacheRegionFactory
EHCache作为二级缓存
应用EHCache作为二级缓存
<hibernate-configuration> <session-factory> <property name="cache.provider_class"> org.hibernate.cache.EhCacheProvider </property> </session-factory> </hibernate-configuration>
编写ehcache.xml
<ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <cache name="com.rbh.examples.Product" maxElementsInMemory="2000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> </ehcache>
maxElementsInMemory 设置保存在内存中的缓存对象的最大数量
eternal 设置缓存中对象是否永远不过期,如果值为true,超过设置被忽略,缓存对象永不过期
timeToIdleSeconds 设置缓存中对象在它过期之前的最大空闲时间,单位为秒
timeToLiveSeconds 设置缓存中对象在它过期之前的最大生存时间,单位为秒
overflowToDisk 设置内存中的缓存对象达到maxElementsInMemory限制时,是否将缓存对象保存到硬盘中
二级缓存的策略
只读缓存(read-only)
读/写缓存(read-write)
不严格的读/写缓存(nonstrict-read-write)
事务缓存(transactional)
缓存组件对缓存策略的支持
在对应的持久化类中设定使用缓存的策略
添加
<cache usage="read-write" />
list读取数据,读的数据可以帮助增加二级缓存,不会读取缓存中的数据.
iterator会查询二级缓存数据,如果读的数据不在二级缓存 n+1次查询记录
二级缓存 缓存集合对象
demo
ehcache.xml
<ehcache> <cache name="com.rbh.examples.Category" maxElementsInMemory="2000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> <cache name="com.rbh.examples.Category.products" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> </ehcache>
Category.hbm.xml
<hibernate-mapping> <class name="com.rbh.examples.Category" table="CATEGORY"> <cache usage="read-write" /> <set name="products" cascade="save-update" inverse="true"> <cache usage="read-write" /> <key column="category_id"></key> <one-to-many class="com.rbh.examples.Product" /> </set> </class> </hibernate-mapping>
二级缓存的管理
SessionFactory sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
sessionFactory.evict(Category.class);
把二级缓存的Category的对象全部清除
SessionFactory sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
sessionFactory.evictCollection("com.rbh.examples.Category.products");
把Category对象的下的products集合所缓存的对象清除
SessionFactory sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
sessionFactory.evictEntity("com.rbh.examples.Category");
把Category对应的所有持久化对象的二级缓存都清除
SessionFactory sessionFactory=HibernateSessionFactoryUtil.getSessionFactory();
sessionFactory.evictEntity("com.rbh.examples.Category",new Integer(1));
把指定的持久化对象二级缓存清除
查询缓存(Query Cache)
<hibernate-configuration> <session-factory> <property name="cache.provider_class"> org.hibernate.cache.EhCacheProvider </property> <property name="hibernate.cache.use_query_cache">true</property> </session-factory> </hibernate-configuration>
Transaction tx = session.beginTransaction(); Query query = session.createQuery("from Category"); query.setCacheable(true); List<Category> list = query.list();
memcached作为二级缓存
memcached是一款知名的,高性能的,分布式的内存对象缓存系统,主要用于在web应用中降低数据库的负载,它通过缓存数据库的查询结果,减少对数据库的访问次数,用以提高web应用的访问速度
Hibernate-memcached
Hibernate-memcached是CacheProvider接口的memcached实现
需要用到 memcached-2.x.jar,spy-2.4.jar,commons-codec-1.x.jar,hibernate-memcached-1.2.2.jar
使用memcached windows的服务器
添加Hibernate.hbm.xml
<property name="cache.provider_class"> com.googlecode.hibernate.memcached.MemcachedCacheProvider </property>