首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中创建EHCache实例时的EHCache

在android中创建EHCache实例时的EHCache
EN

Stack Overflow用户
提问于 2017-08-14 10:52:40
回答 1查看 650关注 0票数 0

我试图在Android中使用ehCache,并得到以下错误

代码语言:javascript
复制
java.lang.ExceptionInInitializerError
    at net.sf.ehcache.EhcacheDefaultClassLoader.getInstance(EhcacheDefaultClassLoader.java:29)
    at net.sf.ehcache.config.Configuration.<init>(Configuration.java:208)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:152)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:140)
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:892)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:873)
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:907)

Caused by: java.lang.NullPointerException: parentLoader == null && !nullAllowed
    at java.lang.ClassLoader.<init>(ClassLoader.java:210)
    at java.lang.ClassLoader.<init>(ClassLoader.java:202)
    at net.sf.ehcache.EhcacheDefaultClassLoader.<init>(EhcacheDefaultClassLoader.java:35)
    at net.sf.ehcache.EhcacheDefaultClassLoader.<clinit>(EhcacheDefaultClassLoader.java:26)
    at net.sf.ehcache.EhcacheDefaultClassLoader.getInstance(EhcacheDefaultClassLoader.java:29) 
    at net.sf.ehcache.config.Configuration.<init>(Configuration.java:208) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:152) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:140) 
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:892) 
    at net.sf.ehcache.CacheManager.create(CacheManager.java:873) 
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:907)

下面是我试图初始化的代码

代码语言:javascript
复制
private static Cache getCache(String cacheName) throws IllegalStateException{
    CacheManager cacheManager = CacheManager.getInstance();
    Cache cache;
    if(!cacheManager.cacheExists(cacheName)) {
        cacheManager.addCache(cacheName);
    }
    cache = cacheManager.getCache(cacheName);
    return cache;
}

看起来EHCache不能在安卓系统中工作?有人能给这个放点光吗?在得到错误后,我还在res/xml/ ehcache.xml 中放置了一个配置为

代码语言:javascript
复制
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  monitoring="autodetect" dynamicConfig="true">

    <!-- By default, Ehcache stored the cached files in temp folder. -->    <!-- <diskStore path="java.io.tmpdir" /> -->

    <!-- Ask Ehcache to store cache in this path -->    <!--<diskStore path="c:\\cache" /> -->

    <!-- Sample cache named cache1
    This cache contains a maximum in memory of 10000 elements, and will expire
    an element if it is idle for more than 5 minutes and lives for more than
    10 minutes.

    If there are more than 10000 elements it will overflow to the
    disk cache, which in this configuration will go to wherever java.io.tmp is
    defined on your system. On a standard Linux system this will be /tmp" 
    -->     
    <cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" transactionalMode="off">         
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>
EN

回答 1

Stack Overflow用户

发布于 2017-08-14 11:31:08

Ehcache无法找到ehcach.xml文件。它不知道res/xml文件夹。

因此,尝试以这种方式以编程方式配置缓存(ehcache 2.10):

代码语言:javascript
复制
// create default CacheManager
Configuration config = new Configuration();
config.setName("Mngmt");
// [...]
CacheManager cacheManager = CacheManager.create(config);

int maxEntriesLocalHeap = 100;
String cachName = "cacheName";
Cache cache = new Cache(
  new CacheConfiguration(cachName, maxEntriesLocalHeap)
    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
    .eternal(false)
    .timeToLiveSeconds(120)
    .timeToIdleSeconds(60)
    .diskExpiryThreadIntervalSeconds(0));
cacheManager.addCache(cache);

更多信息,您可以在这里找到:以编程方式创建缓存

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45672912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档