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 : 로컬로만 시도했습니다.
왜 다른 작업을 수행하겠습니까? 캐시 항목을 60 초 후에 만료시키지 않으려면 그 이유를 알려주시겠습니까? –
새 항목을 추가하면 저장되지 않습니다. 한 번 60이 지나간 것처럼 보이지만, 캐시는 아무 것도 저장하지 않습니다. GCacheFactory.EXPIRATION_DELTA가 전체 캐시에 있고 아이템이 아닙니다 ... – corgrath