<commit_before>from common.log import logUtils as log
from common.ripple import userUtils


class userStatsCache:
	def __init__(self):
		self.cache = [{}, {}, {}, {}]

	def get(self, userID, gameMode):
		"""
		Get cached user stats.
		If cached values are not found, they'll be read from db, cached and returned

		userID --
		gameMode -- gameMode number
		return -- userStats dictionary (rankedScore, totalScore, pp, accuracy, playcount)
		"""
		if userID not in self.cache[gameMode]:
			log.debug("userStatsCache miss")
			self.cache[gameMode][userID] = userUtils.getUserStats(userID, gameMode)
		log.debug("userStatsCache hit")
		return self.cache[gameMode][userID]

	def update(self, userID, gameMode, data = {}):
		"""
		Update cached user stats with new values

		userID --
		gameMode -- gameMode number
		data -- updated stats dictionary. Optional. If not passed, will get from db
		"""
		if len(data) == 0:
			data = userUtils.getUserStats(userID, gameMode)
		log.debug("userStatsCache set {}".format(data))
		self.cache[gameMode][userID] = data<commit_msg>.SCORES. Move userStatsCache to redis<commit_after>from common.log import logUtils as log
from common.ripple import userUtils
from objects import glob
import json

class userStatsCache:
	def get(self, userID, gameMode):
		"""
		Get cached user stats from redis.
		If user stats are not cached, they'll be read from db, cached and returned

		:param userID: userID
		:param gameMode: game mode number
		:return: userStats dictionary (rankedScore, totalScore, pp, accuracy, playcount)
		"""
		data = glob.redis.get("lets:user_stats_cache:{}:{}".format(gameMode, userID))
		if data is None:
			# If data is not cached, cache it and call get function again
			log.debug("userStatsCache miss")
			self.update(userID, gameMode)
			return self.get(userID, gameMode)

		log.debug("userStatsCache hit")
		retData = json.loads(data.decode("utf-8"))
		return retData

	def update(self, userID, gameMode, data = {}):
		"""
		Update cached user stats in redis with new values

		:param userID: userID
		:param gameMode: game mode number
		:param data: data to cache. Optional. If not passed, will get from db
		:return:
		"""
		if len(data) == 0:
			data = userUtils.getUserStats(userID, gameMode)
		log.debug("userStatsCache set {}".format(data))
		glob.redis.set("lets:user_stats_cache:{}:{}".format(gameMode, userID), json.dumps(data), 1800)
{'fraction_non_alphanumeric': 0.11769616026711185, 'fraction_numerical': 0.002921535893155259, 'mean_word_length': 4.303097345132743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7416016', 'n_tokens_mistral': 830, 'n_tokens_neox': 753, 'n_words': 244}