java ehcache lru,Java ehcache磁盘存储

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

解决方案

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..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值