2010-02-21 39 views
1

我在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:我只在本地尝试过。

+0

为什么你会期望它做任何不同的事情?如果您不希望缓存项目在60秒后过期,那么为什么要告诉他们? – 2010-02-21 15:50:44

+0

如果我添加新的项目,他们不会被保存。似乎有一次60了,缓存并没有保存任何东西。 这就像GCacheFactory.EXPIRATION_DELTA是在整个缓存而不是项目.. – corgrath 2010-02-21 16:05:22

回答

3

最近读过一篇文章(现在找不到它),使用JCache GCacheFactory.EXPIRATION_DELTA会使整个缓存过期,并且您必须使用Low-level API来使单个对象过期。

+0

我发现了一个支持该理论的链接:http://javathings.blogspot.com/2009/07/cache-expiration-in-google- APP-engine.html – Fedearne 2010-09-16 11:14:14

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秒后不可用.....