我在Google Apps引擎中使用jsr107 JCache来保存网站数据。我的代码如下所示:Google Apps引擎:Memcache/JCache仅在有效期内有效
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;
}
我CacheLogic:
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秒,我打电话与像“http://www.google.com/something”或'https://stackoverflow.com/something的论点阅读。
继承人的事情。
如果在我第一次调用它的60秒内所有的时间里,调用都使用相同的参数来读取读取方法,我总是得到缓存的数据。完善。
但是,一旦它已经超过60+秒,我就会回零。我读了网站并将其放入缓存中。下次我给它打电话时,我再次得到空。
看起来memcache在到期时间结束后从不保存数据,不管密钥如何。
我以不正确的方式使用了API吗?
更新1:我只在本地尝试过。
为什么你会期望它做任何不同的事情?如果您不希望缓存项目在60秒后过期,那么为什么要告诉他们? – 2010-02-21 15:50:44
如果我添加新的项目,他们不会被保存。似乎有一次60了,缓存并没有保存任何东西。 这就像GCacheFactory.EXPIRATION_DELTA是在整个缓存而不是项目.. – corgrath 2010-02-21 16:05:22