-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcache.py
133 lines (105 loc) · 3.83 KB
/
cache.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from google.appengine.api import memcache
class Cache(object):
"""Used to interact with memcache"""
@staticmethod
def san_check(gid):
return memcache.add(key="%s.san" % gid, value=True, time=10)
@staticmethod
def current_gid():
return memcache.get(key="gid_max") or -1
@staticmethod
def get_latest_game(user, bingo=False):
if bingo:
return memcache.get(key="%s.latest_bingo" % user)
return memcache.get(key="%s.latest" % user)
@staticmethod
def set_latest_game(user, gid, bingo=False):
if bingo:
memcache.set(key="%s.latest_bingo" % user, value=gid, time=604800)
memcache.set(key="%s.latest" % user, value=gid, time=604800)
@staticmethod
def set_gid(gid):
memcache.set(key="gid_max", value=int(gid))
@staticmethod
def set_hist(gid, pid, hist):
hist_map = Cache.get_hist(gid) or {}
hist_map[int(pid)] = hist
memcache.set(key="%s.hist" % gid, value=hist_map, time=14400)
@staticmethod
def append_hl(gid, pid, hl):
hist_map = Cache.get_hist(gid) or {}
if int(pid) not in hist_map:
hist_map[int(pid)] = [hl]
else:
hist_map[int(pid)].append(hl)
memcache.set(key="%s.hist" % gid, value=hist_map, time=14400)
@staticmethod
def get_hist(gid):
return memcache.get(key="%s.hist" % gid)
@staticmethod
def get_reachable(gid):
return memcache.get(key="%s.reach" % gid) or {}
@staticmethod
def set_reachable(gid, reachable):
memcache.set(key="%s.reach" % gid, value=reachable, time=7200)
@staticmethod
def get_have(gid):
return memcache.get(key="%s.have" % gid) or {}
@staticmethod
def set_have(gid, have):
memcache.set(key="%s.have" % gid, value=have, time=7200)
@staticmethod
def clear_reach(gid, pid):
reach_map = Cache.get_reachable(gid) or {}
reach_map[int(pid)] = {}
memcache.set(key="%s.reach" % gid, value=reach_map, time=7200)
@staticmethod
def get_items(gid, pid):
return memcache.get(key="%s.%s.items" % (gid, pid)) or ({}, {})
@staticmethod
def set_items(gid, pid, items, is_race=False):
memcache.set(key="%s.%s.items" % (gid, pid), value=items, time=10 if is_race else 14400)
@staticmethod
def get_relics(gid):
return memcache.get(key="%s.relics" % gid) or None
@staticmethod
def set_relics(gid, relics):
memcache.set(key="%s.relics" % gid, value=relics, time=14400)
@staticmethod
def clear_items(gid, pid=1):
Cache.set_items(gid, pid, {})
@staticmethod
def get_pos(gid):
return memcache.get(key="%s.pos" % gid)
@staticmethod
def set_pos(gid, pid, x, y):
pos_map = Cache.get_pos(gid) or {}
pos_map[int(pid)] = (x, y)
memcache.set(key="%s.pos" % gid, value=pos_map, time=3600)
@staticmethod
def get_git(k):
return memcache.get(key="git.%s" % k)
@staticmethod
def set_git(k, val):
memcache.set(key="git.%s" % k, value=val, time=3600)
@staticmethod
def get_board(gid):
return memcache.get(key="%s.board" % gid)
@staticmethod
def set_board(gid, board):
return memcache.set(key="%s.board" % gid, value=board, time=60)
@staticmethod
def get_areas():
areas = memcache.get(key="CURRENT_LOGIC")
if not areas:
with open("seedbuilder/areas.ori", 'r') as f:
areas = f.read()
memcache.set(key="CURRENT_LOGIC", value=areas, time=3600)
return areas
@staticmethod
def remove_game(gid):
memcache.delete_multi(keys=["have", "hist", "san", "pos", "reach", "items", "relics", "board"], key_prefix="%s." % gid)
@staticmethod
def clear():
memcache.flush_all()
Cache.set_gid(0)