Java ehcache磁盘存储 [英] Java ehcache disk store

查看:207
本文介绍了Java ehcache磁盘存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应用程序,我需要生成并经常访问数千个文件。出于磁盘空间使用的原因,我只想在任何给定时间保留固定数量的这些文件。例如,文件正在写入C:\ my-folder。一旦我的文件夹达到1000个文件,如果我需要保存一个新文件,我想从我的文件夹中删除LRU文件。使用ehcache(或任何缓存工具)是否可以这样?我以为我可以在ehcache中使用磁盘存储,但每当我在缓存上调用 get 时,它只会查看内存中的密钥而不是磁盘中的密钥。

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

谢谢,

杰夫

推荐答案

这似乎不是你正在使用EH Cache来潜在..将1000个项目存储在磁盘上然后旋转掉最后一个用了一个。 EH Cache更适合在内存中存储内容,并在内存填充时使用磁盘作为备份。

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.

我个人只是自己滚动。您有非常具体的业务规则。存储1000个项目。然后,当您准备存储文件1001时,您可以找到要删除的文件。如果您在操作系统级别启用了访问时间,则可以执行单个linux命令来删除最近访问过的文件...通常您不会对linux中的文件使用访问时间,但这会使您的问题变得容易在操作系统级解决..

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

这篇关于Java ehcache磁盘存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆