Java 如何读取和写入EhCache中的记录?

Java 如何读取和写入EhCache中的记录?,java,ehcache,Java,Ehcache,大家好, 我目前的需求是使用EhCache存储和读取记录。我不熟悉EhCache实现。我已经阅读了EhCache文档并开始实施。我已经完成了记录插入部分和阅读部分。插入记录时,将创建*.data和*.index文件。下面是代码 public class Driver { public static void main(String[] args) { CacheManager cm = CacheManager.create("ehcache.xml"); Cac

大家好, 我目前的需求是使用EhCache存储和读取记录。我不熟悉EhCache实现。我已经阅读了EhCache文档并开始实施。我已经完成了记录插入部分和阅读部分。插入记录时,将创建*.data和*.index文件。下面是代码

public class Driver
    {
    public static void main(String[] args) {
    CacheManager cm = CacheManager.create("ehcache.xml");
    Cache cache = cm.getCache("test");
    // I do a couple of puts
    for(int i=0;i<10;i++){
        cache.put(new Element("key1", "val1"));
            cache.flush();
    }
    System.out.println(cache.getKeys());
    for(int i=0;i<10;i++){
        Element el = cache.get("key"+i);
        System.out.println(el.getObjectValue());
    }
    cm.shutdown();  
    }
    }

需要任何输入。

您所做的是正确的,预期的行为也是正确的。缓存通常用于通过快速提供常用数据来提高应用程序性能,同时避免昂贵的数据存储访问

并非所有应用程序都需要在系统关闭后保持缓存,这是您看到的默认行为(大多数应用程序将在应用程序启动时或请求开始时构建缓存)。您正在缓存的数据在堆中,一旦JVM死亡,缓存就消失了。现在您想在重新启动之后继续使用它吗?有很多选择。放屁

我正从同一页复制代码片段:

DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
diskStoreConfiguration.setPath("/my/path/dir");
// Already created a configuration object ...
configuration.addDiskStore(diskStoreConfiguration);
// By adding configuration for storing the cache in a file - you are not using default cache manager
CacheManager mgr = new CacheManager(configuration);
此外,还必须按照说明配置持久性选项

再次从链接复制代码段:

<cache>
   <persistence strategy=”localRestartable” synchronousWrites=”true”/>
</cache>


希望这有帮助

嗨,维沙,谢谢你的回答。我已将ehCache.xml文件中的DiskStoreConfiguration设置为。。。。但我还是不能得到它。。。您能告诉我有什么问题吗?但是您正在将配置对象传递给CM吗?哦,您的逐出和过期策略是什么?我已经使用ehcache.xml创建了CacheManager。。CacheManager cm=CacheManager.create(“ehcache.xml”);因此,在XML本身中添加了diskstoreConfiguration。因此CacheManager将具有配置对象。。。无需通过编程方式添加。。我说的对吗?我对这个地方还不熟悉。。。你能详细解释一下吗。。驱逐和期满政策呢?
<cache>
   <persistence strategy=”localRestartable” synchronousWrites=”true”/>
</cache>