Google app engine Google应用程序引擎:Memcache/JCache仅在过期时间内有效

Google app engine Google应用程序引擎:Memcache/JCache仅在过期时间内有效,google-app-engine,null,memcached,Google App Engine,Null,Memcached,我在谷歌应用程序引擎中使用jsr107 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)) { r

我在谷歌应用程序引擎中使用jsr107 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;

    }
我的缓存逻辑:

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);
 }

}
公共类缓存逻辑{
私有静态缓存逻辑单例;
专用缓存;
私人最终int TTL=60;
公共静态CacheLogic实例(){
if(singleton==null){
singleton=新的CacheLogic();
}
返回单身;
}
专用缓存逻辑(){
Map props=newhashmap();
道具放置(GCacheFactory.EXPIRATION_DELTA,TTL);
试一试{
CacheFactory CacheFactory=CacheManager.getInstance().getCacheFactory();
cache=cacheFactory.createCache(道具);
}捕获(缓存异常){
e、 printStackTrace();
}
}
公共void put(字符串键、字符串值){
缓存。删除(键);
cache.put(键、值);
CacheStatistics=cache.getCacheStatistics();
System.out.println(“[CacheLogic]添加了新条目。Count=“+stats.getObjectCount());
System.out.println(“[CacheLogic]添加了新条目。size=“+cache.size());
}
公共字符串获取(字符串键){
return(String)cache.get(key);
}
}
我已将缓存过期时间设置为60秒,并使用诸如“”或“”之类的参数调用read

事情是这样的

如果在我第一次调用read方法的60秒内,调用read方法时始终使用相同的参数,那么我总是得到缓存的数据。太好了

然而,一旦超过60秒,我就会返回空值。我阅读了网站的内容,并将其放入缓存。下次我调用它时,我再次得到null

似乎在过期时间结束后,memcache永远不会保存数据,无论密钥是什么

我是否以错误的方式使用了API


更新1:我只在本地尝试过这个方法。

最近读到一篇文章(现在找不到),说使用JCache GCacheFactory.EXPIRATION\u DELTA会使整个缓存过期,您必须使用来使单个对象过期。

这里是一个实际的例子

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);
 }

}
公共类缓存逻辑{
私有静态缓存逻辑单例;
专用缓存;
//私人最终int TTL=60;
公共静态CacheLogic实例(){
if(singleton==null){
singleton=新的CacheLogic();
}
返回单身;
}
专用缓存逻辑(){
//Map props=newhashmap();
//道具放置(GCacheFactory.EXPIRATION_DELTA,TTL);
试一试{
CacheFactory CacheFactory=CacheManager.getInstance().getCacheFactory();
cache=cacheFactory.createCache(/*props*/);
}捕获(缓存异常){
e、 printStackTrace();
}
}
公共void put(字符串键、字符串值){
缓存。删除(键);
cache.put(键、值);
CacheStatistics=cache.getCacheStatistics();
System.out.println(“[CacheLogic]添加了新条目。Count=“+stats.getObjectCount());
System.out.println(“[CacheLogic]添加了新条目。size=“+cache.size());
}
公共字符串获取(字符串键){
return(String)cache.get(key);
}
}

默认情况下,存储在缓存中的对象将在缓存中保留“尽可能长的时间”。您正在将保存在缓存中的对象设置为在60秒后删除,但似乎让您感到惊讶的是,对象在60秒后不可用。…

为什么您希望它做任何不同的事情?如果您不希望缓存项在60秒后过期,那么为什么要告诉它们?如果我添加新项,它们将不会被保存。似乎一旦60消失了,缓存根本就不会保存任何东西。这就像GCacheFactory.EXPIRATION\u DELTA在整个缓存中,而不是在项目中..我发现了一个支持该理论的链接: