加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 安全地处理AppEngine中的并发Memcache更新

发布时间:2020-12-15 02:34:06 所属栏目:Java 来源:网络整理
导读:有关安全处理并发Memcache更新的Google Apps Engine文档: The putIfUntouched and getIdentifiable methods of the Memcache service can be used to provide a way to safely make key-value updates to memcache in scenarios where multiple requests ar
有关安全处理并发Memcache更新的Google Apps Engine文档:

The putIfUntouched and getIdentifiable methods of the Memcache service can be used to provide a way to safely make key-value updates to memcache in scenarios where multiple requests are being handled concurrently that need to update the same memcache key in an atomic fashion. (It is possible to get race conditions in those scenarios.)

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读