在ehcache.xml文件中配置查询缓存参数,ehcache.xml文件配置如下:
<ehcache>
<!-- diskStore元素,配置一个目录,这个目录用来存放数据,
也就是说,如果EhCache需要把数据写入磁盘,将会写到这个目录下 -->
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<cache name="ehcacheName"
maxElementsInMemory="3000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="36000"
overflowToDisk="true"
/>
</ehcache>
2. spring的配置
第一步:给指定方法配置缓存/src/main/resources/applicationContext-resources.xml
<ehcache:proxy id="userGroupServiceProxy" refId="userGroupService" ><ehcache:caching cacheName="cash15Min" methodName="selectuserGroupWithDetailByMemberId" />
<ehcache:caching cacheName="cash15Min" methodName="selectuserGroupWithDetailByGroupId" />
<ehcache:caching cacheName="cash15Min" methodName="selectuserGroupById" />
</ehcache:proxy>
配置参数的含义如下:
id:唯一标识符
refId:需要配置缓存的service或者controller
cacheName:缓存名称
methodName:需要缓存的方法,这个方法必须是shoppingHomeService中的方法
第二步:在控制器中注入依赖的缓存userGroupServiceProxy /src/main/webapp/WEB-INF/dispatcher-servlet.xml
<bean id="PFController" class="com.java.mall.controller.PFController"><property name="userService" ref="userService"></property>
<property name="userGroupService" ref="userGroupServiceProxy"></property>
</bean>
同时需要在实体类中注入依赖,提供setter方法,
private userGroupService userGroupServicepublic void setuserGroupService(userGroupService userGroupService) {
this.userGroupService = userGroupService
}
二级缓存也称进程级的缓存或SessionFactory级的缓存,二级缓存可以被所有的session共享二级缓存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二级缓存
二级缓存的配置和使用:
* 将echcache.xml文件拷贝到src下
* 开启二级缓存,修改hibernate.cfg.xml文件
<property name="hibernate.cache.use_second_level_cache">true</property>
* 指定缓存产品提供商,修改hibernate.cfg.xml文件
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
* 指定那些实体类使用二级缓存(两种方法)
* 在映射文件中采用<cache>标签
<!--
<cache usage="read-only"/>
-->
* 在hibernate.cfg.xml文件中,采用<class-cache>标签
<class-cache class="com.bjsxt.hibernate.Student" usage="read-only"/>
二级缓存是缓存实体对象的
了解一级缓存和二级缓存的交互
package com.bjsxt.hibernate
import java.io.Serializable
import org.hibernate.CacheMode
import org.hibernate.Session
import org.hibernate.SessionFactory
import junit.framework.TestCase
public class CacheLevel2Test extends TestCase {
/**
* 开启两个session,分别调用load
*/
public void testCache1() {
Session session = null
try {
session = HibernateUtils.getSession()
session.beginTransaction()
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
try {
session = HibernateUtils.getSession()
session.beginTransaction()
//不会发出sql,因为开启了二级缓存,session是共享二级缓存的
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
}
/**
* 开启两个session,分别调用get
*/
public void testCache2() {
Session session = null
try {
session = HibernateUtils.getSession()
session.beginTransaction()
Student student = (Student)session.get(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
try {
session = HibernateUtils.getSession()
session.beginTransaction()
//不会发出sql,因为开启了二级缓存,session是共享二级缓存的
Student student = (Student)session.get(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
}
/**
* 开启两个session,分别调用load,在使用SessionFactory清除二级缓存
*/
public void testCache3() {
Session session = null
try {
session = HibernateUtils.getSession()
session.beginTransaction()
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
//管理二级缓存
SessionFactory factory = HibernateUtils.getSessionFactory()
//factory.evict(Student.class)
factory.evict(Student.class, 1)
try {
session = HibernateUtils.getSession()
session.beginTransaction()
//会发出查询sql,因为二级缓存中的数据被清除了
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
}
/**
* 一级缓存和二级缓存的交互
*/
public void testCache4() {
Session session = null
try {
session = HibernateUtils.getSession()
session.beginTransaction()
//仅向二级缓存读数据,而不向二级缓存写数据
session.setCacheMode(CacheMode.GET)
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
try {
session = HibernateUtils.getSession()
session.beginTransaction()
//发出sql语句,因为session设置了CacheMode为GET,所以二级缓存中没有数据
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
try {
session = HibernateUtils.getSession()
session.beginTransaction()
//只向二级缓存写数据,而不从二级缓存读数据
session.setCacheMode(CacheMode.PUT)
//会发出查询sql,因为session将CacheMode设置成了PUT
Student student = (Student)session.load(Student.class, 1)
System.out.println("student.name=" + student.getName())
session.getTransaction().commit()
}catch(Exception e) {
e.printStackTrace()
session.getTransaction().rollback()
}finally {
HibernateUtils.closeSession(session)
}
}
}
缓存的介质一般是内存,所以读写速度很快。但如果缓存中存放的数据量非常大时,也会用硬盘作为缓存介质。缓存的实现不仅仅要考虑存储的介质,还要考虑到管理缓存的并发访问和缓存数据的生命周期。
缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找。由于缓存的运行速度比内存快得多,故缓存的作用就是帮助硬件更快地运行。
因为缓存往往使用的是RAM(断电即掉的非永久储存),所以在用完后还是会把文件送到硬盘等存储器里永久存储。电脑里最大的缓存就是内存条了,最快的是CPU上镶的L1和L2缓存,显卡的显存是给显卡运算芯片用的缓存,硬盘上也有16M或者32M的缓存。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)