EhCache:简单程序无法正常工作 [英] EhCache: Simple Program not working

查看:170
本文介绍了EhCache:简单程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to EhCache, and on the way to implement as a distributed cache service. I was trying out simple programs, but not able to work out the error. I'm able to store data to cache, but not able to retrieve it.

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

These are two simple programs i wrote for testing.

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



    }

}

程序2:

    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>

I have the ehcache.xml configured like <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错误

Teracotta server is started, and showing its running. When I run Program 1, it runs without any error. After which i ran second one , i got error as 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并发出警告:

if i do specify configuration xml in both cases , its generating again NPE with a warning :

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.

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

What i'm doing wrong? Some input please.

谢谢

推荐答案

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

Looks like you want to distribute (and persist over JVM restarts) your cache via Terracotta. In that case, you have to make the cache distributed by adding a TerracottaConfig element pointing to your server inside your ehcache.xml (on same level as your caches)

<terracottaConfig url="localhost:9510" />

在此之后,添加

<terracotta/> 

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

tag to your cache to make it distributed.

希望有帮助!

这篇关于EhCache:简单程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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