Hibernate-缓存
来源:互联网 发布:哈工大人工智能产业园 编辑:程序博客网 时间:2024/05/19 15:24
范围是进程 集群范围。
有多种实现的方式。
默认携带的EHCache
选择去开启不同的其持久化类,看他要不要缓存咯。。
1.引入相关JAR文件
commons-logging-1.2.jar
ehcache-1.2.3.jar
2.启用二级缓存(配置文件中添加)
cache.use_second_level_cache-true
cache.provider_class-org.hibernate.cache.EhCacheProvider
3.配置缓存框架相关信息
缓存的XML配置文件
在哈姆雷特JAR包 Project 、etc、ehcache.xml
<ehcache> <!-- 设置本地缓存的目录 --> <diskStore path="java.io.tmpdir" /> <!-- 默认缓存策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!-- 指定缓存策略 --> <cache name="po.User" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />//eternal永恒的?订单数据是会更新的//timeToLiveSeconds存活时间//timeToIdleSeconds多长时间更新一次//overflowToDisk数据溢出 存到磁盘 <!-- 指定User中的houses集合 --> <cache name="po.User.houses" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /></ehcache>
4.指定使用二级缓存的实体类(在实体类xml映射文件中)
如果是集合,两边都需要配置
测试缓存的作用
public class CacheTest { public static void main(String[] args) { Transaction tx = null; try { tx = HibernateSessionFactory.getSession().beginTransaction(); getTest(); tx.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (tx != null) { tx.rollback(); } System.out.println("========="); try { tx = HibernateSessionFactory.getSession().beginTransaction(); getTest(); tx.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (tx != null) { tx.rollback(); } } public static void getTest() { District d = (District) HibernateSessionFactory.getSession().get( District.class, (short) 1004); System.out.println(d.getName()); }
第一次for list没啥用。。弱。。
第二次 Iterator捡现成 ,先抽ID出来。当真正需要再去查。第二轮就查了下ID,此时缓存有作用了。
**
查询缓存 补偿list
**
条件语句需要相同,能够帮list。。
1.在配置文件中开启查询缓存
cache.use_query_cache——true
(可以指定缓存策略)
<cache name="query.test" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" />
2.在程序中开启和启用缓存策略
public static void queryCacheTest() { List<District> result = HibernateSessionFactory.getSession() .createQuery("from District") .setCacheable(true)//在程序中手动启用查询缓存 .setCacheRegion("query.test")//指定缓存策略 .list(); for (District d : result) { System.out.println(d.getName() + "\t" + d.getStreets().size()); } }
用注解来开启缓存
在实体类上@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
也可以再get的集合上面加注解来开启集合的缓存
0 0
- hibernate缓存
- Hibernate 缓存
- Hibernate 缓存
- hibernate 缓存
- hibernate 缓存
- Hibernate缓存
- hibernate缓存
- Hibernate 缓存
- hibernate 缓存
- Hibernate缓存~~~~~~
- Hibernate缓存
- Hibernate缓存
- Hibernate缓存
- Hibernate缓存
- hibernate 缓存
- hibernate缓存
- Hibernate缓存
- hibernate缓存
- java集合整理
- android 图片瀑布流效果
- Libgdx之Button TextButton ImageButton ImageTextButton 按钮使用
- 我的第一个C#
- c/C++ 田忌赛马(贪心算法)
- Hibernate-缓存
- Ubuntu14.04桥接网络设置与SSH登陆
- 苹果自带的json解析NSJSONSerialization(序列化)
- 小白笔记------------------最小m段和问题
- <<金刚经>> 读书小记
- 显式锁ReentrantLock
- Android学习心得(18) --- 对APK包动态写入信息
- 数组操作
- C/C++输出格式运用