3
我正在处理需要生成并经常访问数千个文件的应用程序。由于磁盘空间使用的原因,我只想在任何时候保留这些文件的固定数量。例如,这些文件正在写入C:\ my-folder。一旦我的文件夹达到1000个文件,如果我需要保存一个新文件,我想从我的文件夹中删除LRU文件。是这样的可能使用ehcache(或任何缓存工具)?我以为我可以在ehcache中使用磁盘存储,但每当我在缓存上调用get
时,它只会查看内存中而不是磁盘中的键。Java ehcache磁盘存储
我的代码的某些代码段如下所示:
// create the cache
CacheManager cm = CacheManager.create();
String name = getName();
CacheConfiguration config = new CacheConfiguration(name, 1)
.maxElementsOnDisk(2000).diskPersistent(true)
.overflowToDisk(true).eternal(true).diskStorePath(name)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
Cache cache = new Cache(config);
cm.addCache(cache);
// I do a couple of puts
cache.put(new Element("key1", val1));
cache.put(new Element("key2", val2));
cache.flush();
// now key1 is no longer in the cache (since max memory size is 1), but I'd like to look on disk since I have set maxElementsOnDisk to 2000
Element el = cache.get("key1");
感谢,
杰夫
您可以使用http://linux.about.com/library/cmd/blcmdl8_tmpwatch.htm来控制临时文件。 – emory 2010-09-17 04:14:37