Java ehcache disk store

陌路散爱 提交于 2019-12-20 02:15:22

问题


I'm working on application in which I need to generate and frequently access thousands of files. For disk space usage reasons, I only want to keep around a fixed number of these files at any given time. For example, the files are being written to C:\my-folder. Once my-folder reaches 1000 files, if I need to save a new file, I would like to erase the LRU file from my-folder. Is something like this possible using ehcache (or any caching tool)? I thought I could use a disk store in ehcache, but whenever I call get on the cache, its only looking at the keys that are in memory and not in disk.

Some snippets of my code are shown here:

    // 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");

Thanks,

Jeff


回答1:


This doesn't seem like you are using EH Cache to it's potential.. to store 1000 items on disk and then rotate away the last used one. EH Cache is more for storing stuff in memory, and using disk as a backup when memory fills.

I would personally just roll my own. You have very specific business rules. Store 1000 items. Then when you are ready to store file 1001, you could go and find a file to delete. If you have access time turned on at the OS level, you can do a single linux command to delete the least recently accessed file... Usually you don't use access time for files in linux, but it would make your problem easy to solve at the OS level..



来源:https://stackoverflow.com/questions/3732582/java-ehcache-disk-store

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!