Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么设置expireAfterAccess不起作用?_Java_Caching_Testing_Guava - Fatal编程技术网

Java 为什么设置expireAfterAccess不起作用?

Java 为什么设置expireAfterAccess不起作用?,java,caching,testing,guava,Java,Caching,Testing,Guava,为什么timedCachetest在最后一行失败?为什么缓存在60秒后不为空 package com.test.cache; import java.util.concurrent.TimeUnit; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.google.common.cache.Cache; impo

为什么
timedCachetest
在最后一行失败?为什么缓存在60秒后不为空

package com.test.cache;

import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class CacheTest {
    private static final int MAXIMUM_SIZE = 10;
    private static final int CONCURRENCY_LEVEL = 1;
    private static final long EXPIRE_AFTER_ACCESS = 60;
    Cache<String, Thing> cache;
    private static TimeUnit unit = TimeUnit.SECONDS;
    private static long sec = 1000;

    @Before
    public void setUp() throws Exception {
        cache = CacheBuilder.newBuilder().maximumSize(MAXIMUM_SIZE).concurrencyLevel(CONCURRENCY_LEVEL).expireAfterAccess(EXPIRE_AFTER_ACCESS, unit).build();
    }

    @After
    public void tearDown() {
        cache = null;
    }

    @Test
    public void simpleCachetest() {
        String key = "key";
        Integer hc = key.hashCode();
        Thing thing = new Thing(key);
        cache.put(key, thing);
        thing = cache.getIfPresent(key);
        Assert.assertNotNull(thing);
        Assert.assertEquals(hc, thing.getValue());
        Assert.assertEquals(key, thing.getName());
        Assert.assertEquals(1, cache.size());
    }

    @Test
    public void timedCachetest() {
        String key = "key";
        Thing thing = new Thing(key);
        Assert.assertEquals(0, cache.size());
        cache.put(key, thing);
        Assert.assertEquals(1, cache.size());
        try {
            thing = cache.getIfPresent(key);
            long millis = 100 * sec;
            Thread.sleep(millis);
            // cache.invalidateAll();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Assert.assertNotNull(thing);
        Assert.assertEquals(key, thing.getName());
        Assert.assertEquals(0, cache.size());
    }

    class Thing {
        public Thing(String key) {
            this.name = key;
            this.value = key.hashCode();
        }

        public String getName() {
            return name;
        }

        public Integer getValue() {
            return value;
        }

        private String name;
        private Integer value;
    }
}
package com.test.cache;
导入java.util.concurrent.TimeUnit;
导入junit.framework.Assert;
导入org.junit.After;
导入org.junit.Before;
导入org.junit.Test;
导入com.google.common.cache.cache;
导入com.google.common.cache.CacheBuilder;
公共类缓存测试{
专用静态最终int最大_大小=10;
私有静态最终int并发_级别=1;
访问后私有静态最终长过期\u=60;
缓存;
专用静态时间单位=TimeUnit.SECONDS;
专用静态长秒=1000;
@以前
public void setUp()引发异常{
cache=CacheBuilder.newBuilder();
}
@之后
公共无效拆卸(){
cache=null;
}
@试验
public void simpleCachetest(){
字符串key=“key”;
整数hc=key.hashCode();
事物=新事物(关键);
放置(钥匙、东西);
thing=cache.getIfPresent(key);
Assert.assertNotNull(事物);
Assert.assertEquals(hc,thing.getValue());
Assert.assertEquals(key,thing.getName());
Assert.assertEquals(1,cache.size());
}
@试验
public void timedCachetest(){
字符串key=“key”;
事物=新事物(关键);
Assert.assertEquals(0,cache.size());
放置(钥匙、东西);
Assert.assertEquals(1,cache.size());
试一试{
thing=cache.getIfPresent(key);
长毫秒=100*秒;
睡眠(毫秒);
//cache.invalidateAll();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Assert.assertNotNull(事物);
Assert.assertEquals(key,thing.getName());
Assert.assertEquals(0,cache.size());
}
阶级事务{
公共事物(字符串键){
this.name=key;
this.value=key.hashCode();
}
公共字符串getName(){
返回名称;
}
公共整数getValue(){
返回值;
}
私有字符串名称;
私有整数值;
}
}

它就在
CacheBuilder
Javadoc中声明:

如果请求expireAfterWrite或expireAfterAccess,则在每次修改缓存、偶尔访问缓存或调用cache.cleanUp()时,都可能逐出条目。过期的条目可以在Cache.size()中计数,但对读或写操作来说永远不可见


CacheBuilder
缓存在特定请求时进行维护,或者可以作为缓存变异的一部分进行维护,或者偶尔在读取时进行维护。例如,它不会启动一个线程来进行缓存维护,因为a)线程相对较重,b)某些环境限制了线程的创建。

感谢您为我阅读手册。;)也许我应该删除这个问题。