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

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

Memcache服务的putIfUntouched和getIdentifiable方法可用于提供一种方法,在需要以primefaces方式更新同一个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 ); 

这个用法是否正确?

非常感谢。

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

如果是这样,那么代码应该是:

 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,则表示结果变量不再准确,您可以执行以下操作:忽略并返回当前结果或从缓存中加载结果。