zoukankan      html  css  js  c++  java
  • Redis 原子操作INCR

    The content below come from http://try.redis.io/ 

    There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as:

    x = GET count
    x = x + 1
    SET count x
    

    The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time:

    1. Client A reads count as 10.
    2. Client B reads count as 10.
    3. Client A increments 10 and sets count to 11.
    4. Client B increments 10 and sets count to 11.

    We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation. Redis provides many of these atomic operations on different types of data.

  • 相关阅读:
    Set,List,Map的区别
    阅读笔记15
    阅读笔记14
    阅读笔记13
    阅读笔记12
    阅读笔记11
    阅读笔记10
    架构漫谈读后感
    阅读笔记1
    暑期周记8
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/6903341.html
Copyright © 2011-2022 走看看