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


那月静好
2024-12-24 11:27:26 (1月前)
  1. 有关安全处理并发的Google Apps Engine文档


内存缓存
</跨度>
更新:

putIfUntouched和getIdentifiable方法

内存缓存
</跨度>
服务可用于提供安全地进行键值更新的方法

内存缓存
</跨度>
在同时处理多个请求需要更新相同请求的情况下

内存缓存
</跨度>
原子时尚的关键。 (有可能获得竞争条件 -

2 条回复
  1. 0# 易米烊光 | 2019-08-31 10-32



    我需要了解你的代码:

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



    若是,则代码应为:




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

    2. Long result = new Long( q.count() );
      mc.putIfUntouched( cacheKey, oldCountValue, result );
      return result;

    3. </code>


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


登录 后才能参与评论