-
Notifications
You must be signed in to change notification settings - Fork 55
/
personalBestCache.py
69 lines (61 loc) · 2.08 KB
/
personalBestCache.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
from common.log import logUtils as log
from common import generalUtils
from objects import glob
class cacheMiss(Exception):
pass
class personalBestCache:
def get(self, userID, fileMd5, country=False, friends=False, mods=-1, relax=False):
"""
Get cached personal best rank
:param userID: userID
:param fileMd5: beatmap md5
:param country: True if country leaderboard, otherwise False
:param friends: True if friends leaderboard, otherwise False
:param relax: True if relax leaderboard, False if classic leaderboard
:param mods: leaderboard mods
:return: 0 if cache miss, otherwise rank number
"""
try:
# Make sure the value is in cache
data = glob.redis.get("lets:personal_best_cache:{}".format(userID))
if data is None:
raise cacheMiss()
# Unpack cached data
data = data.decode("utf-8").split("|")
cachedpersonalBestRank = int(data[0])
cachedfileMd5 = str(data[1])
cachedCountry = generalUtils.stringToBool(data[2])
cachedFriends = generalUtils.stringToBool(data[3])
cachedMods = int(data[4])
cachedRelax = generalUtils.stringToBool(data[5])
# Check if everything matches
if fileMd5 != cachedfileMd5 \
or country != cachedCountry \
or friends != cachedFriends \
or mods != cachedMods \
or relax != cachedRelax:
raise cacheMiss()
# Cache hit
log.debug("personalBestCache hit")
return cachedpersonalBestRank
except cacheMiss:
log.debug("personalBestCache miss")
return 0
def set(self, userID, rank, fileMd5, country=False, friends=False, mods=-1, relax=False):
"""
Set userID's redis personal best cache
:param userID: userID
:param rank: leaderboard rank
:param fileMd5: beatmap md5
:param country: True if country leaderboard, otherwise False
:param friends: True if friends leaderboard, otherwise False
:param mods: leaderboard mods
:param relax: True if relax leaderboard, False if classic
:return:
"""
glob.redis.set(
"lets:personal_best_cache:{}".format(userID),
"{}|{}|{}|{}|{}|{}".format(rank, fileMd5, country, friends, mods, relax),
1800
)
log.debug("personalBestCache set")