Created
May 5, 2010 20:45
-
-
Save dound/391399 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""High-concurrency counters without sharding | |
Author: Nick Johnson | |
http://appengine-cookbook.appspot.com/recipe/high-concurrency-counters-without-sharding | |
""" | |
def incrementCounter(key, 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 = "counter_lock:%s" % (key,) | |
count_key = "counter_value:%s" % (key,) | |
if memcache.add(lock_key, None, time=update_interval): | |
# Time to update the DB | |
count = int(memcache.get(count_key) or 0) + 1 | |
def tx(): | |
entity = db.get(key) | |
entity.counter += count | |
entity.put() | |
db.run_in_transaction(tx) | |
memcache.delete(count_key) | |
else: | |
# Just update memcache | |
memcache.incr(count_key, initial_value=0) |
Commit c3fc97 is the revised recipe by evlogimenos.
Commit 8b1063 is the revised recipe by me. It includes a doctest which can be run with nosegae.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The initial commit fe5edc is the original recipe by Nick Johnson: http://appengine-cookbook.appspot.com/recipe/high-concurrency-counters-without-sharding