2011-10-20 3 views

답변

1

http://ehcache.org/apidocs/net/sf/ehcache/config/CacheConfiguration.html#timeToLiveSeconds(long)

그것은 다음은 기본 캐시 http://ehcache.org/apidocs/net/sf/ehcache/Cache.html#Cache%28java.lang.String,%20int,%20boolean,%20boolean,%20long,%20long%29

의 생성자에서 해당 인수되어있다 캐시에 요소의 살아있는 시간을 설정 CacheConfiguration의 방법입니다 부분적인 캐시의 요소 값 - 정확한 요소에도 지정할 수 있습니다. http://ehcache.org/apidocs/net/sf/ehcache/Element.html#setTimeToLive(int

여기에 그것을 사용의 작은 예입니다

import java.util.concurrent.TimeUnit; 

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

public class EhCacheFixedExpirationTest { 
public static void main(String[] args) { 
    CacheManager cacheManager = new CacheManager(); 
    Cache testCache = new Cache("testCache", 100, false, false, 10, 10); 
    cacheManager.addCache(testCache); 

    Element element = new Element("1", "20"); 
    element.setTimeToLive(1); 
    testCache.put(element); 

    long start = System.nanoTime(); 

    while (testCache.get("1") != null) { 
     //wait 
    } 
    System.out.println(TimeUnit.MILLISECONDS.convert((System.nanoTime() - start), TimeUnit.NANOSECONDS)); 
} 
} 

출력은 거의 항상 1000, 그래서 정밀도는 오히려 좋다. Is used Ehcache 2.4.6

관련 문제