二级缓存大量数据的解决方案
数据很大
二级缓存 存储大数据,让 内存和磁盘文件进行交互,数据库中的不变的数据在磁盘上,这样就可以少和数据库进行交互了
ehcache.xml 放在src下
1 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> 3 <!--存储位置--> 4 <diskStore path="C:\\TEMP1"/> 5 <defaultCache 6 maxElementsInMemory="12" 7 eternal="false" 8 timeToIdleSeconds="120" 9 timeToLiveSeconds="120" 10 overflowToDisk="false" 11 maxElementsOnDisk="10000000" 12 diskPersistent="false" 13 diskExpiryThreadIntervalSeconds="120" 14 memoryStoreEvictionPolicy="LRU" 15 /> 16 17 <Cache 18 name="cn.itcast.hiberate.sh.domain.Classes" <!--针对的类对象,它最好都是不变的 只执行查询语句--> 19 maxElementsInMemory="5" 20 eternal="false" 21 timeToIdleSeconds="120" 22 timeToLiveSeconds="120" 23 overflowToDisk="true" 24 maxElementsOnDisk="10000000" 25 diskPersistent="false" 26 diskExpiryThreadIntervalSeconds="120" 27 memoryStoreEvictionPolicy="LRU" 28 /> 29 </ehcache>
磁盘和内存进行交互,加快速度
1 @Test 2 public void testAllClasses(){ 3 Session session = sessionFactory.openSession(); 4 List<Classes> classesList = session.createQuery("from Classes").list(); 5 session.close(); 6 try { 7 Thread.sleep(1000); 8 } catch (InterruptedException e) { 9 // TODO Auto-generated catch block 10 e.printStackTrace(); 11 } 12 }
在c://Temp1中会多个文件,查询数据库的数据会放在此文件中,以减少和数据库的交互