def get_totals(): """ Get the total for all known counters. Known counters are just those that have been incremented at least once. """ counters = client.keys('counter:*:total') pipe = client.pipeline(transaction=False) for counter in counters: pipe.get(counter) return {counter.split(':')[1]: int(value) for counter, value in zip(counters, pipe.execute())}
def get_totals(): """ Get the total for all known counters. Known counters are just those that have been incremented at least once. """ counters = client.keys('counter:*:total') pipe = client.pipeline(transaction=False) for counter in counters: pipe.get(counter) return { counter.split(':')[1]: int(value) for counter, value in zip(counters, pipe.execute()) }
def increment_counter(counter): """ Increment the specified counter. """ pipe = client.pipeline(transaction=False) pipe.incr('counter:%s:total' % counter) for label, bucket, expire in INTERVALS: key = 'counter:%s:%s:%s' % (counter, label, unicode(time.strftime(bucket))) pipe.incr(key) # It's safe to just keep on expiring the counter, even if it already # had an expiration, since it is bounded by the current day. We don't # really mind at what time of the day the expiration will be exactly. pipe.expire(key, expire) pipe.execute()