Getting hibernate hibernate cache of five

What is the cache?

  • It does not refer to a computer's memory or secondary cache of cpu
  • It refers to the cache in order to reduce the frequency of physical access to the application data source, a strategy to improve the performance of applications running

Why use the cache?

  • Efficiency ORM framework to access the database directly affect the speed of running, lifting and critical to optimize the efficiency of ORM framework.

  • The key Hibernate cache is an important means to enhance and optimize the efficiency of hibernate, so learn to hibernate cache usage and configuration are optimized

General works caching

 

Cache

  • hibernate cache is also known as "session cache", "session level cache"
  • When an entity will not get stored in memory by session from the database query entities together, the next time the same query entities from the database, and fetched from memory, that is cached
  • A cache and the same life cycle and session; session destroy, destroy it
  • An applicable range data is in the cache of the current session
  • Repeatedly query the same data in a different session, perform multiple database queries
  • Cache, the persistence of each instance has a unique OID

Level cache API (cache can not be canceled, with the two methods management)

  1. evict (), is used to remove an object from a cache of session
  2. clear (), for all objects in a cache clear

Cache sometimes have an impact on the performance of the program

public class SessionTest {

/**
* @param "测试一级缓存"
*/
public static void main(String[] args) {
Session session = HibernadUtil.getSession();
// Students students = session.get(Students.class, 31);
// System.out.println(students.getSname());
//
// //session.evict(students);// 清除缓存
// session.clear();// 清除所有缓存

//Session session1 = HibernadUtil.getSession(); //新的session
// students = session.get(Students.class, 31);
// System.out.println(students.getSname());

Query query = session.createQuery("from Students ");
List<Students> list = query.list();
for (Students student : list) {
System.out.println(student);
}

/*list不会用到当前会话中的session,依然会从数据库中查找*/
// list = query.list();
// for (Students student : list) {
// System.out.println(student);
// }

/*
*干掉
* List<Students> list = query.list();
// for (Students student : list) {
// System.out.println(student);
// }
* 从雇员表中查取学生编号,如果在缓存中找到了,从缓存中拿,否则从缓存中拿
* */
Iterator iterator = query.iterate();
while (iterator.hasNext()) {
Object student = (Students) iterator.next();
System.out.println(((Students) student).getSname());

}

session.close();
}
}

 

二级缓存

有些常用的数据,在一个session中缓存以后,我们希望在其他session中能够直接使用,而不用在次缓存怎么办?这时,我们的二级缓存就登场了!
二级缓存的配置步骤(默认不自动开启):

  • 二级缓存又称为“全局缓存”、“应用级缓存”
  • 二级缓存中的数据可适用范围是当前应用的所有会话
  • 二级缓存是可插拔式缓存,默认是ehcache,还支持其他的二级缓存:Hashtable,OSCache、等等

1、导入架包(ehcache及它所依赖的架包,版本根据自己的情况选)

2、配置ehcache.xml文件(参考地址:https://www.cnblogs.com/jeremy-blog/p/4021833.html)

3、在跟文件hibernate.cfg.xml指定二级缓存的入口

4、在指定实体类的hbm.xml中开启二级缓存

 

什么时候使用二级缓存?

  • 很少被修改的数据
  • 不是很重要的数据,允许偶尔出现并发的数据
  • 不会被并发访问的数据
  • 参考数据

 

一级缓存和二级缓存的对比和总结

Guess you like

Origin www.cnblogs.com/lindaiyu/p/11001363.html