二级缓存处理大数据 用ehcache.xml配置文件

时间:2023-03-09 14:38:42
二级缓存处理大数据 用ehcache.xml配置文件

二级缓存处理大数据 用ehcache.xml配置文件二级缓存大量数据的解决方案

数据很大

二级缓存 存储大数据,让 内存和磁盘文件进行交互,数据库中的不变的数据在磁盘上,这样就可以少和数据库进行交互了

ehcache.xml 放在src下

 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--存储位置-->
<diskStore path="C:\\TEMP1"/>
<defaultCache
maxElementsInMemory="12"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/> <Cache
name="cn.itcast.hiberate.sh.domain.Classes"        <!--针对的类对象,它最好都是不变的 只执行查询语句-->
maxElementsInMemory="5"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>

磁盘和内存进行交互,加快速度

     @Test
public void testAllClasses(){
Session session = sessionFactory.openSession();
List<Classes> classesList = session.createQuery("from Classes").list();
session.close();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

在c://Temp1中会多个文件,查询数据库的数据会放在此文件中,以减少和数据库的交互