# Path: counters/models.py
# class Shard(db.Model):
#     name = db.StringProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
# 
# class Config(db.Model):
#     name = db.StringProperty(required=True)
#     num_shards = db.IntegerProperty(required=True, default=20)


import random
from google.appengine.ext import db
from google.appengine.api import memcache
from counters.models import Shard, Config




def get_count(name):
    """
    Retrieve the value for a given sharded counter.
    """
    total = memcache.get(name)
    if total is None:
        total = 0
        for counter in Shard.all().filter('name = ', name):
            total += counter.count
        memcache.add(name, str(total), 60)
    return total


def increment(name, delta=1):
    """
    Increment the value for a given sharded counter.
    """

# Path: counters/models.py
# class Shard(db.Model):
#     name = db.StringProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
# 
# class Config(db.Model):
#     name = db.StringProperty(required=True)
#     num_shards = db.IntegerProperty(required=True, default=20)


# Path: counters/utils.py
import random
from google.appengine.ext import db
from google.appengine.api import memcache
from counters.models import Shard, Config





def get_count(name):
    """
    Retrieve the value for a given sharded counter.
    """
    total = memcache.get(name)
    if total is None:
        total = 0
        for counter in Shard.all().filter('name = ', name):
            total += counter.count
        memcache.add(name, str(total), 60)
    return total


def increment(name, delta=1):
    """
    Increment the value for a given sharded counter.
    """

    config = Config.get_or_insert(name, name=name)