java – 安全地处理AppEngine中的并发Memcache更新
有关安全处理并发Memcache更新的Google Apps Engine文档:
我正在构建一个需要准确缓存值的应用程序,下面是我的代码,请帮我检查一下是否正确: ... 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,result ); return result; 如果putIfUntouched返回null,则表示结果变量不再准确,您可以执行以下操作:忽略并返回当前结果或从缓存中加载结果. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |