-
Notifications
You must be signed in to change notification settings - Fork 24
/
stores.py
95 lines (78 loc) · 2.58 KB
/
stores.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import json
import config
from google.appengine.api import memcache
from google.appengine.ext import db
class AsyncValue:
def __init__(self, deferred):
self._deferred = deferred
def __str__(self):
model = self._deferred.get_result()
if model is None:
raise KeyError
return model.value
class StoreNode(db.Model):
value = db.StringProperty()
# These aren't readable just yet
collection = db.StringProperty()
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
@classmethod
def get(cls, key, async=True):
if async:
return AsyncValue(db.get_async(key))
else:
model = cls.get_by_key_name(key)
if model is None:
raise KeyError
return model.value
@classmethod
def delete(cls, key, async=True):
if async:
db.delete_async(key)
else:
db.delete(key)
@classmethod
def set(cls, key, value, collection, async=True):
model = cls(key_name=key, value=value, collection=collection)
if async:
db.put_async(model)
else:
model.put()
class DataStore:
def __init__(self, collection, persistent=True):
self._collection = collection
self._base_key = 'spartify:%s:%%s' % (self._collection,)
self._persistent = persistent
def _parse_key(self, key):
return self._base_key % (key,)
def __getitem__(self, key):
key = self._parse_key(key)
data = memcache.get(key)
if data is None:
try:
data = StoreNode.get(key, async=False)
memcache.set(key, data, config.CACHE_DEFAULT_TIMEOUT)
except KeyError:
raise KeyError
return json.loads(data)
def __contains__(self, key):
key = self._parse_key(key)
if memcache.get(key) is None:
try:
StoreNode.get(key, async=False)
except KeyError:
return False
return True
def __setitem__(self, key, value):
self.timeout_store(key, value)
def timeout_store(self, key, value, timeout=-1):
key = self._parse_key(key)
value = json.dumps(value)
timeout = config.CACHE_DEFAULT_TIMEOUT if timeout < 0 else timeout
memcache.set(key, value, timeout)
if self._persistent:
StoreNode.set(key, value, self._collection)
votes = DataStore('vote', False)
parties = DataStore('party')
events = DataStore('events')
queues = DataStore('queue')