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