Java – safely handle concurrent Memcache updates in App Engine

Google Apps engine documentation for secure handling of concurrent Memcache updates:

I am building an application that needs accurate cache values. Here is my code. Please check whether it is correct for me:

...
        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 () returns false? What should this code return? how?

When adding an entity to the data store, I use the following:

mc.increment( cacheKey,1,initValue );

Is this usage correct?

Thank you.

Solution

I need to know your code:

If yes, 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,result );
return result;

If putifuntouched returns null, the result variable is no longer accurate. You can ignore and return the current result or load the result from the cache

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>