2010-02-21 4 views
1

Google Apps Engine에서 jsr107 JCache를 사용하여 웹 사이트 데이터를 저장하고 있습니다. 내가 캐시가 60 초 시간 만료 설정하고 나는 'http://www.google.com/something'또는 'https://stackoverflow.com/something'와 같은 인수 읽어 호출Google Apps Engine : 만료 시간 동안에 만 Memcache/JCache가 작동합니다.

public class CacheLogic { 

private static CacheLogic singleton; 
private Cache cache; 
private final int TTL = 60; 

public static CacheLogic instance() { 

    if (singleton == null) { 
    singleton = new CacheLogic(); 
    } 

    return singleton; 

} 

private CacheLogic() { 

    Map<String, Integer> props = new HashMap<String, Integer>(); 
    props.put(GCacheFactory.EXPIRATION_DELTA, TTL); 

    try { 
    CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory(); 
    cache = cacheFactory.createCache(props); 
    } catch (CacheException e) { 
    e.printStackTrace(); 
    } 

} 

public void put(String key, String value) { 
    cache.remove(key); 
    cache.put(key, value); 
    CacheStatistics stats = cache.getCacheStatistics(); 
    System.out.println("[CacheLogic] New entry added. Count=" + stats.getObjectCount()); 
    System.out.println("[CacheLogic] New entry added. size=" + cache.size()); 
} 

public String get(String key) { 
    return (String) cache.get(key); 
} 

} 

:

public static String read(String link, boolean UTF8) throws Exception { 

    URL url = new URL(link); 

    String cached = CacheLogic.instance().get(link); 

    if (!StringUtil.isEmpty(cached)) { 
    return cached; 
    } 

    // Here i save the website's context into data 

    CacheLogic.instance().put(link, data); 

    return data; 

    } 

내 캐시 로직 :처럼 내 코드 보인다.

.

내가 처음 전화했을 때 60 초 이내에 항상 같은 인수로 read 메소드를 읽으면, 나는 항상 캐시 된 데이터를 얻는다. 완전한.

그러나 60 초 이상 지나면 null이 반환됩니다. 나는 웹 사이트를 읽고 캐쉬에 넣었다. 다음에 내가 그것을 부르면, 나는 다시 null이된다.

memcache는 만료 시간이 끝난 후 아무리 중요한 경우에도 데이터를 저장하지 않는 것처럼 보입니다.

잘못된 방식으로 API를 사용 했습니까?

업데이트 1 : 로컬로만 시도했습니다.

+0

왜 다른 작업을 수행하겠습니까? 캐시 항목을 60 초 후에 만료시키지 않으려면 그 이유를 알려주시겠습니까? –

+0

새 항목을 추가하면 저장되지 않습니다. 한 번 60이 지나간 것처럼 보이지만, 캐시는 아무 것도 저장하지 않습니다. GCacheFactory.EXPIRATION_DELTA가 전체 캐시에 있고 아이템이 아닙니다 ... – corgrath

답변

3

최근에 JCache를 사용하여 게시물을 읽었습니다 (GCacheFactory.EXPIRATION_DELTA는 전체 캐시를 만료하고 개별 개체를 만료하려면 Low-level API 사용해야합니다.

+0

그 이론을 뒷받침하는 링크를 찾았습니다 : http://javathings.blogspot.com/2009/07/cache-expiration-in-google- app-engine.html – Fedearne

0

다음은 캐시에 저장된 객체는 "가능한 한 오랫동안"에 대한 캐시에 남아 ... 실제적인 예이다 기본적으로

public class CacheLogic { 

private static CacheLogic singleton; 
private Cache cache; 
//private final int TTL = 60; 

public static CacheLogic instance() { 

    if (singleton == null) { 
    singleton = new CacheLogic(); 
    } 

    return singleton; 

} 

private CacheLogic() { 

    //Map<String, Integer> props = new HashMap<String, Integer>(); 
    //props.put(GCacheFactory.EXPIRATION_DELTA, TTL); 

    try { 
    CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory(); 
    cache = cacheFactory.createCache(/*props*/); 
    } catch (CacheException e) { 
    e.printStackTrace(); 
    } 

} 

public void put(String key, String value) { 
    cache.remove(key); 
    cache.put(key, value); 
    CacheStatistics stats = cache.getCacheStatistics(); 
    System.out.println("[CacheLogic] New entry added. Count=" + stats.getObjectCount()); 
    System.out.println("[CacheLogic] New entry added. size=" + cache.size()); 
} 

public String get(String key) { 
    return (String) cache.get(key); 
} 

} 

. 60 초 후에 캐시에 저장된 개체를 삭제하도록 설정했지만 60 초 후에 개체를 사용할 수 없다는 사실이 놀랍습니다 .....