Example #1
0
def incrementCounter(key, property="counter", update_interval=10):
  """Increments a memcached counter.
  Args:
    key: The key of a datastore entity that contains the counter.
    update_interval: Minimum interval between updates.
  """
  lock_key = "icl:%s" % key
  count_key = "icv:%s" % key
  if cache_add(None, lock_key, time=update_interval):
    # Time to update the DB
    count = int(cache_get(count_key) or 0) + 1
    def tx():
      entity = db.get(key)
      setattr(entity, property, getattr(entity, property) + count)
      entity.put()
    db.run_in_transaction(tx)
    cache_delete(count_key)
  else:
    # Just update memcache
    cache_incr(1, count_key, initial_value=0)