安全地处理AppEngine中的并发Memcache更新 [英] Safely Handling Concurrent Memcache Updates in AppEngine

查看:130
本文介绍了安全地处理AppEngine中的并发Memcache更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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

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

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

Long结果= new Long(q.count());
mc.putIfUntouched(cacheKey,oldCountValue,result);
返回结果;

我的问题是:如果putIfuntouched()返回false?代码应该返回什么?如何?

当数据存储添加一个实体时,我在下面使用:

  mc.increment(cacheKey,1,initValue); 

是否正确?

谢谢很多。

解决方案

我需要了解一些关于您的代码的内容:

首先,您为什么要检查if count!= null?不是oldCountIdValue == null意味着count == null,反之亦然:if oldCountIdValue!= null then count!= null如果是的话那么代码应该是这样的be:


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

Long结果= new Long(q.count());
mc.putIfUntouched(cacheKey,oldCountValue,result);
返回结果;

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


The Google Apps Engine doc about safely Handling Concurrent Memcache Updates:

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.)

i am building an application which need accurate cache value, below is my code, please help me check if it is right:

        ...
        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;

My question is: what if putIfuntouched() return false? what should the code return? and how?

When the datastore add one entity, i use below:

mc.increment( cacheKey, 1, initValue );

is this usage correct?

Thanks a lot.

解决方案

I need to understand something about your code:
First why are you checking if count != null? doesn't oldCountIdValue == null means that count == null and the other way around: if oldCountIdValue != null then count != null

if so then the code should be:

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;

if putIfUntouched returns null, it means that your result variable is not accurate anymore, you can do the following: ignore and return your current result or load the result from the cache.

这篇关于安全地处理AppEngine中的并发Memcache更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆