转载自http://blog.java1234.com/blog/articles/277.html
Ehcache默认配置的话 为了提高效率,所以有一部分缓存是在内存中,然后达到配置的内存对象总量,则才根据策略持久化到硬盘中,这里是有一个问题的,假如系统突然中断运行 那内存中的那些缓存,直接被释放掉了,不能持久化到硬盘;这种数据丢失,对于一般项目是不会有影响的,但是对于我们的爬虫系统,我们是用来判断重复Url的,所以数据不能丢失;
这时候我们就需要通过Ehcache配置,来实现缓存的持久化,不存内存中。
这里给下参考配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
ehcache
>
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
-->
<
diskStore
path
=
"C:\ehcache"
/>
<!--
defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期
overflowToDisk:当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
-->
<
defaultCache
maxElementsInMemory
=
"100"
eternal
=
"true"
overflowToDisk
=
"true"
/>
<!--
maxElementsInMemory设置成1,overflowToDisk设置成true,只要有一个缓存元素,就直接存到硬盘上去
eternal设置成true,代表对象永久有效
maxElementsOnDisk设置成0 表示硬盘中最大缓存对象数无限大
diskPersistent设置成true表示缓存虚拟机重启期数据
-->
<
cache
name
=
"a"
maxElementsInMemory
=
"1"
eternal
=
"true"
overflowToDisk
=
"true"
maxElementsOnDisk
=
"0"
diskPersistent
=
"true"
/>
</
ehcache
>
|
运行前面的测试代码,只要我们添加缓存元素 ,则直接写入到硬盘中;