ehcache 2:SelfPopulatingCache 丢失刷新

问题描述

我必须使用带有 SelfPopulatingCache 的 ehache 2(使用 2.9.0 和 2.10.4 测试)。如果我在 overflowTodisk="true" 中设置 ehcache.xml,刷新将丢失。不写入硬盘,一切正常。

开始输出总是:

...
Key creating (hello) 0
Refresh
...

大约 50 次刷新后,输出更改为:

...
Refresh
...

原因是 getKeys() 方法上的方法 refresh() 返回一个空集合。

示例

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <diskStore path="./target" />

    <cache name="cache1" 
        maxEntriesLocalHeap="100"
        maxElementsOndisk="1000"
        eternal="false" 
        timetoIdleSeconds="3600" 
        timetoLiveSeconds="3600"
        memoryStoreevictionPolicy="LFU" 
        transactionalMode="off"
        diskPersistent="false"
        overflowTodisk="true"
        >
    </cache>

</ehcache>

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;

public class StartSimple {
    private static final int TEST_THREADS = 1;
    private static final int LOOPS = 1_000_000_000;

    private static class Value {
        public Value(String value) {
            this.value = value;
        }

        public String value;

        @Override
        public String toString() {
            return value;
        }
    }

    public static class MyCacheEntryFactory implements CacheEntryFactory {
        int i = 0;

        @Override
        public Object createEntry(Object key) throws Exception {
            System.out.println("Key creating (" + key + ") " + i);
            return new Value((String) key + i++);
        }
    }

    public static class RunningChild implements Runnable {
        Ehcache cache;

        public RunningChild(Ehcache cache) {
            this.cache = cache;
        }

        @Override
        public void run() {
            for (int i = 0; i < LOOPS; i++) {
                if (!cache.get("hello").getobjectValue().toString().startsWith("hello")) {
                    throw new IllegalStateException();
                }
            }
        }
    }

    public static class Refresh implements Runnable {
        SelfPopulatingCache cache;
        boolean running;

        public Refresh(SelfPopulatingCache cache) {
            this.cache = cache;
            this.running = true;
        }

        @Override
        public void run() {
            while (running) {
                cache.refresh(true);
                System.out.println("Refresh");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.err.println("Error " + e.getMessage());
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CacheManager cm = CacheManager.getInstance();
        Ehcache parentCache1 = cm.getCache("cache1");
        SelfPopulatingCache cache = new SelfPopulatingCache(parentCache1,new MyCacheEntryFactory());
        
        // add one element
        cache.put(new Element("hello",new Value("hello")));

        // create test threads
        final List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < TEST_THREADS; i++) {
            Thread t = new Thread(new RunningChild(cache));
            threads.add(t);
        }
        
        // create refresh thread
        Refresh refresh = new Refresh(cache);
        Thread refreshThread = new Thread(refresh);
        refreshThread.setPriority(Thread.MAX_PRIORITY);
        refreshThread.start();
        
        long start = System.currentTimeMillis();

        // start runner threads and wait for end
        for (final Thread t : threads) {
            t.start();
        }
        for (final Thread t : threads) {
            t.join();
        }

        long end = System.currentTimeMillis();
        
        // waiting for end - refresh thread
        refresh.running = false;
        refreshThread.join();

        System.out.println(cache.get("hello"));
        System.out.println((end - start) + " ms");
        System.out.println("End");
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...