0

我是 EhCache 的新手,正在实现分布式缓存服务。我正在尝试简单的程序,但无法解决错误。我能够将数据存储到缓存中,但无法检索它。

这是我为测试编写的两个简单程序。

    package com.db.tests;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestCachePut {

    public TestCachePut() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        CacheManager cache= CacheManager.create("E:/ehcache-2.6.2/ehcache.xml");

        Cache caches = cache.getCache("cache1");

        Element element= new Element("testKey", "inserted to cache");
        caches.put(element);

        System.out.println("Put in to cache");



    }

}

方案二:

    package com.db.tests;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestCacheGet {

    public TestCacheGet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */

    public static void main(String[] args) {




        CacheManager caches =  CacheManager.getInstance();
        Element val = caches.getCache("cache1").get("testKey");
        System.out.println(" cache content: "+val.getValue());


    }

}

我将 ehcache.xml 配置为<cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="3000" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localTempSwap"/> </cache>

Teracotta 服务器已启动,并显示其正在运行。当我运行程序 1 时,它运行时没有任何错误。之后我跑了第二个,我得到了 NPE 错误,

   24 Dec, 2012 12:05:42 PM net.sf.ehcache.config.ConfigurationFactory parseConfiguration
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/E:/ehcache-2.6.2/lib/ehcache-core-2.6.2.jar!/ehcache-failsafe.xml
Exception in thread "main" java.lang.NullPointerException
    at com.db.tests.TestCacheGet.main(TestCacheGet.java:22)

如果我在这两种情况下都指定了配置 xml,它会再次生成带有警告的 NPE:

4 Dec, 2012 12:22:34 PM net.sf.ehcache.DiskStorePathManager resolveAndLockIfNeeded
WARNING: diskStorePath 'E:\Users\SKRISH~1\AppData\Local\Temp' is already used by an existing CacheManager either in the same VM or in a different process.
The diskStore path for this CacheManager will be set to E:\Users\SKRISH~1\AppData\Local\Temp\ehcache_auto_created1315701513913502723diskstore.
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance.

我做错了什么?请输入一些信息。

谢谢

4

2 回答 2

1

还指定配置文件TestCacheGet

CacheManager cacheManager =  CacheManager.create("E:/ehcache-2.6.2/ehcache.xml");

[编辑]

而且,localTempSwap策略是特定于 VM 的。尝试localRestartable指定diskStore path。(仅适用于企业版)

<ehcache>
<diskStore path="/Users/me/store/data"/>
<cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="3000" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localRestartable" synchronousWrites="true"/> </cache>
</ehcache> 
于 2012-12-24T06:43:35.230 回答
1

看起来您想通过 Terracotta 分发(并在 JVM 重新启动时持续存在)您的缓存。在这种情况下,您必须通过在 ehcache.xml 中添加指向服务器的 TerracottaConfig 元素来分发缓存(与缓存处于同一级别)

<terracottaConfig url="localhost:9510" />

完成后,添加

<terracotta/> 

标记到您的缓存以使其分发。

希望有帮助!

于 2012-12-27T13:15:30.687 回答