-
Notifications
You must be signed in to change notification settings - Fork 8
/
Cnt.py
40 lines (38 loc) · 986 Bytes
/
Cnt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import logging
from google.appengine.api import memcache
from google.appengine.ext import db
class Cnt(db.Model):
count = db.IntegerProperty(required=True, indexed=False)
@classmethod
def get(cls, name):
count = memcache.get(name)
if count is None:
cnt = Cnt.get_by_key_name(name)
if cnt is None:
logging.warning("Cnt: returning None (%s)" % name)
return cnt
else:
count = cnt.count
memcache.set(name, count, 86400)
logging.warning("Cnt: returning %d (%s)" % (count, name))
return count
@classmethod
def incr(cls, name, value=1):
cnt = Cnt.get_by_key_name(name)
if cnt is None:
cnt = Cnt(key_name=name, count=value)
cnt.put()
else:
cnt.count += value
cnt.put()
memcache.set(name, cnt.count, 86400)
@classmethod
def set(cls, name, value):
cnt = Cnt.get_by_key_name(name)
if cnt is None:
cnt = Cnt(key_name=name, count=value)
cnt.put()
else:
cnt.count = value
cnt.put()
memcache.set(name, cnt.count, 86400)