Java 在AppEngine中安全处理并发Memcache更新

Java 在AppEngine中安全处理并发Memcache更新,java,google-app-engine,caching,google-cloud-datastore,Java,Google App Engine,Caching,Google Cloud Datastore,Google Apps Engine关于安全处理并发Memcache更新的文档: Memcache服务的putIfUntouched和GetIdentification方法可用于提供一种方法,在需要以原子方式更新同一Memcache密钥的同时处理多个请求的情况下,安全地对Memcache进行键值更新。(在这些情况下,可以获得比赛条件。) 我正在构建一个需要精确缓存值的应用程序,下面是我的代码,请帮助我检查它是否正确: ... IdentifiableValue o

Google Apps Engine关于安全处理并发Memcache更新的文档:

Memcache服务的putIfUntouched和GetIdentification方法可用于提供一种方法,在需要以原子方式更新同一Memcache密钥的同时处理多个请求的情况下,安全地对Memcache进行键值更新。(在这些情况下,可以获得比赛条件。)

我正在构建一个需要精确缓存值的应用程序,下面是我的代码,请帮助我检查它是否正确:

        ...
        IdentifiableValue oldCountIdValue = mc.getIdentifiable( cacheKey );
        Long count = ( Long ) oldCountIdValue.getValue();
        if( count != null )
        {
            this.oldCountValue = oldCountIdValue;
            return count;
        }

        Long result = new Long( q.count() );
        mc.putIfUntouched( cacheKey, oldCountValue, result );
        return result;
我的问题是:如果putIfuntouched()返回false怎么办?代码应该返回什么?怎么做

当数据存储添加一个实体时,我使用以下方法:

mc.increment( cacheKey, 1, initValue );
这个用法正确吗


非常感谢。

我需要了解您的代码:
首先,为什么要检查计数!=无效的oldCountIdValue==null不是意味着count==null,反之亦然:如果oldCountIdValue!=空则计数!=空的

如果是,则代码应为:

IdentifiableValue oldCountIdValue = mc.getIdentifiable( cacheKey );
if (oldCountIdValue != null) {
   return ( Long ) oldCountIdValue.getValue();
}

Long result = new Long( q.count() );
mc.putIfUntouched( cacheKey, oldCountValue, result );
return result;

如果putIfUntouched返回null,则表示结果变量不再准确,您可以执行以下操作:忽略并返回当前结果或从缓存加载结果。

谢谢Shay,现在我想我了解了putIfUntouched()的工作原理