Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 缓存正常,但缓存项消失_Google App Engine_Caching_Jcache - Fatal编程技术网

Google app engine 缓存正常,但缓存项消失

Google app engine 缓存正常,但缓存项消失,google-app-engine,caching,jcache,Google App Engine,Caching,Jcache,我在GAE中使用JSR107JCache临时存储从web捕获的数据,并以一定的间隔(10分钟)将其写入数据存储。我的应用程序只使用一个缓存。大多数情况下,它工作得很好。偶尔(每天4000次中有5-6次)缓存中的一个条目因未知原因丢失。我不太喜欢JCache,但我知道我的应用程序可能运行在不同的JVM实例上,我的应用程序可能使用不同的JCache实例 可在以下日志中找到问题: 2013-12-31 09:58:14.229 /cron/<myservlet>?code=11 200 .

我在GAE中使用JSR107JCache临时存储从web捕获的数据,并以一定的间隔(10分钟)将其写入数据存储。我的应用程序只使用一个缓存。大多数情况下,它工作得很好。偶尔(每天4000次中有5-6次)缓存中的一个条目因未知原因丢失。我不太喜欢JCache,但我知道我的应用程序可能运行在不同的JVM实例上,我的应用程序可能使用不同的JCache实例

可在以下日志中找到问题:

2013-12-31 09:58:14.229 /cron/<myservlet>?code=11 200 ........... app_engine_release=1.8.8 instance=00c61b117c1599a88baa456ae838cbfa9b0f28
2013-12-31 09:58:14.011 <myapp>.RealCache validate: realCache 29829270 retrieved
2013-12-31 09:58:14.117 <myapp>.RealCache validate: Cache contains 0, 1, 2, 3, 4, 5, 6, 8, 11, 12, 13,
2013-12-31 09:58:14.121 <myapp>.RealCache get: Cache 11 has ...... <expected result>......
2013-12-31 09:58:14.121 cron.<myservlet> doGet: Appended with <mydata> for 11
2013-12-31 09:58:14.121 <myapp>.RealCache validate: realCache 29829270 retrieved
2013-12-31 09:58:14.224 <myapp>.RealCache validate: Cache contains 0, 1, 2, 3, 4, 5, 6, 8, 11, 12, 13, 

2013-12-31 09:58:14.443 /cron/<myservlet>?code=6 200 ............ app_engine_release=1.8.8 instance=00c61b117c1599a88baa456ae838cbfa9b0f28
2013-12-31 09:58:14.227 <myapp>.RealCache validate: realCache 29829270 retrieved
2013-12-31 09:58:14.326 <myapp>.RealCache validate: Cache contains 0, 1, 2, 3, 4, 5, 6, 8, 11, 12, 13, 
2013-12-31 09:58:14.329 <myapp>.RealCache get: Cache 6 has ..... <expected result>......
2013-12-31 09:58:14.329 cron.<myservlet> doGet: Appended with <mydata> for 6
2013-12-31 09:58:14.329 <myapp>.RealCache validate: realCache 29829270 retrieved
2013-12-31 09:58:14.437 <myapp>.RealCache validate: Cache contains 0, 2, 3, 4, 5, 6, 8, 11, 12, 13, 

请告知我的编码有什么问题。

您应该阅读appengine上的各种文档。JCache是appengine上使用memcache的标准缓存api实现。请参阅appengine上JCache文档的第一行

appengine上的Memcache不保证缓存中存在条目。他们可以在任何时候被驱逐,而不需要事先通知。请阅读appengine上的memcache行为


谷歌特别指出:“一般来说,应用程序不应该期望缓存的值总是可用的。”

感谢您的回复。我确实读过台词。但作为一个普通开发人员,我解释说“不保证……生存”只适用于缓存,暂时没有活动。在我的例子中,如果我们讨论的是缓存实例,那么它在问题发生前0.2秒被访问。如果我们谈论的是缓存实例中的缓存键,那么根据设计,它是在1分钟前使用的。使用寿命如此短的缓存根本没有任何实际用途。缓存的使用寿命因许多原因而不同。但是,它只是一个具有LRU行为的缓存。但你不能指望有什么东西在场。即使是付费的独占memcache,如果它们转移您的实例,也可能会将内容逐出。你不会得到任何通知的。你必须编写你的应用程序,以便在缓存被逐出时具有弹性。我得到了大约90%的缓存命中率。如果缓存中有很多实体,并且它们没有被频繁读取,那么它们将被逐出。感谢Tim的回复。我的缓存经常是get()和put(),这可以在我的日志中看到。这就是为什么我认为它应该还在这里。无论如何,在这方面,任何关于弹性的技巧。我的应用在缓存地图中有11个条目,每一个键在每天的几个小时内每分钟被访问一次。在这种访问频率下,我需要缓存至少持续10分钟。
public class RealCache {
    private static Cache realCache;
    public static synchronized void validate() {
        realCache = CacheManager.getInstance().getCache(Constants.Whale);
        if (realCache == null) {    //for first time of the day
             CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
             Map<String, Integer> props = new HashMap<>();
             props.put(GCacheFactory.EXPIRATION_DELTA, 28800);   //8 hrs is enough
             realCache = cacheFactory.createCache(Collections.emptyMap());  
             CacheManager.getInstance().registerCache(Constants.Whale, realCache);
         }
    }//end validate
    public static synchronized MyObj get(int code) {
        validate();
        MyObj myObj = (MyObj) realCache.get(code);
        return myObj;
    }//end get
    public static synchronized void put(int code, MyObj myObj) {
        validate();
        realCache.put(code, myObj);
    }//end put
}//end RealCache
synchronized ("RealCache") {
    MyObj myObj = RealCache.get(code);
    if (myObj != null) {
        myObj.setData(myObj.getData() + newData);
        log.info("Appended with "+ newData+ " for "+ code);
    } else {
        myObj = new MyObj();
        myObj.setData(newData);
        log.warning("Created with "+ newData+ " for "+ code);
    }
    RealCache.put(code, myObj);
}//end sync