def get_count_group_types_for_user(self, user_id: int, hidden: bool) -> Optional[List[Tuple[int, int]]]: if hidden is None: return None if hidden: key = RedisKeys.count_group_types_including_hidden(user_id) else: key = RedisKeys.count_group_types_not_including_hidden(user_id) count = self.redis.get(key) if count is None: return None types = str(count, "utf-8") # TODO: log and check this, was blank in redis once if len(types) == 0 or "," not in types: logger.warning(f"group types was none in cache for key {key}") return None types = types.split(",") types = [ group_type.split(":", maxsplit=1) for group_type in types ] return [(int(a), int(b)) for a, b in types]
def set_count_group_types_for_user(self, user_id: int, counts: List[Tuple[int, int]], hidden: bool) -> None: if hidden: key = RedisKeys.count_group_types_including_hidden(user_id) else: key = RedisKeys.count_group_types_not_including_hidden(user_id) types = ",".join([":".join(map(str, values)) for values in counts]) self.redis.set(key, types) self.redis.expire(key, ONE_DAY)
def get_sent_message_count_in_group_for_user(self, group_id: str, user_id: int) -> Optional[int]: key = RedisKeys.sent_message_count_in_group(group_id) count = self.redis.hget(key, str(user_id)) if count is None: return None return int(float(str(count, "utf-8")))
def get_user_count_in_group(self, group_id: str) -> Optional[int]: key = RedisKeys.user_in_group(group_id) n_users = self.redis.hlen(key) if n_users is None: return None return n_users
def get_last_sent_for_user(self, user_id: int) -> (str, float): key = RedisKeys.last_sent_time_user(user_id) values = self.redis.get(key) if values is None: return None, None group_id, last_time = str(values, "utf-8").split(":", maxsplit=1) return group_id, float(last_time)
def reset_unread_in_groups(self, user_id: int, group_ids: List[str]): p = self.redis.pipeline() for group_id in group_ids: key = RedisKeys.unread_in_group(group_id) p.hset(key, str(user_id), 0) p.execute()
def get_last_message_time_in_group(self, group_id: str): key = RedisKeys.last_message_time(group_id) last_message_time = self.redis.get(key) if last_message_time is None: return None return float(str(last_message_time, "utf-8"))
def increase_unread_in_group_for(self, group_id: str, user_ids: List[int]) -> None: key = RedisKeys.unread_in_group(group_id) p = self.redis.pipeline() for user_id in user_ids: p.hincrby(key, str(user_id), 1) p.execute()
def get_user_ids_and_join_time_in_group( self, group_id: str ) -> Optional[Dict[int, float]]: users = self.redis.hgetall(RedisKeys.user_in_group(group_id)) if not len(users): return None return {int(user_id): float(join_time) for user_id, join_time in users.items()}
def set_user_ids_and_join_time_in_group( self, group_id: str, users: Dict[int, float] ): key = RedisKeys.user_in_group(group_id) self.redis.delete(key) if len(users): self.add_user_ids_and_join_time_in_group(group_id, users) self.redis.expire(key, ONE_HOUR)
def get_messages_in_group(self, group_id: str) -> (Optional[int], Optional[float]): key = RedisKeys.messages_in_group(group_id) messages_until = self.redis.get(key) if messages_until is None: return None, None messages, until = str(messages_until, "utf-8").split("|") return int(messages), float(until)
def set_last_read_in_group_for_users( self, group_id: str, users: Dict[int, float] ) -> None: key = RedisKeys.last_read_time(group_id) p = self.redis.pipeline() for user_id, last_read in users.items(): p.hset(key, str(user_id), last_read) p.execute()
def get_last_read_in_group_for_user( self, group_id: str, user_id: int ) -> Optional[float]: key = RedisKeys.last_read_time(group_id) last_read = self.redis.hget(key, str(user_id)) if last_read is None: return None return float(str(last_read, "utf-8"))
def add_user_ids_and_join_time_in_group( self, group_id: str, users: Dict[int, float] ) -> None: key = RedisKeys.user_in_group(group_id) p = self.redis.pipeline() for user_id, join_time in users.items(): p.hset(key, str(user_id), str(join_time)) p.expire(key, ONE_DAY) p.execute()
def get_unread_in_group(self, group_id: str, user_id: int) -> Optional[int]: key = RedisKeys.unread_in_group(group_id) n_unread = self.redis.hget(key, str(user_id)) if n_unread is None: return None try: return int(str(n_unread, "utf-8")) except (TypeError, ValueError): return None
def set_hide_group( self, group_id: str, hide: bool, user_ids: List[int] = None ) -> None: key = RedisKeys.hide_group(group_id) if user_ids is None: users = [str(user, "utf-8") for user in self.redis.hgetall(key)] else: users = user_ids for user in users: self.redis.hset(key, user, "t" if hide else "f")
def set_user_ids_and_join_time_in_groups( self, group_users: Dict[str, Dict[int, float]] ): p = self.redis.pipeline() for group_id, users in group_users.items(): key = RedisKeys.user_in_group(group_id) self.redis.delete(key) if len(users): for user_id, join_time in users.items(): p.hset(key, str(user_id), str(join_time)) p.expire(key, ONE_DAY) p.execute()
def get_user_ids_and_join_time_in_groups(self, group_ids: List[str]): join_times = dict() p = self.redis.pipeline() for group_id in group_ids: p.hgetall(RedisKeys.user_in_group(group_id)) for group_id, users in zip(group_ids, p.execute()): if not len(users): continue join_times[group_id] = { int(user_id): float(join_time) for user_id, join_time in users.items() } return join_times
def get_last_read_in_group_for_users( self, group_id: str, user_ids: List[int] ) -> Tuple[dict, list]: p = self.redis.pipeline() for user_id in user_ids: key = RedisKeys.last_read_time(group_id) p.hget(key, str(user_id)) not_cached = list() last_reads = dict() for user_id, last_read in zip(user_ids, p.execute()): if last_read is None: not_cached.append(user_id) else: last_reads[user_id] = float(str(last_read, "utf-8")) return last_reads, not_cached
def set_last_read_in_group_for_user( self, group_id: str, user_id: int, last_read: float ) -> None: key = RedisKeys.last_read_time(group_id) self.redis.hset(key, str(user_id), last_read)
def set_sent_message_count_in_group_for_user(self, group_id: str, user_id: int, count: int) -> None: key = RedisKeys.sent_message_count_in_group(group_id) self.redis.hset(key, str(user_id), count)
def get_group_exists(self, group_id: str) -> Optional[bool]: value = self.redis.get(RedisKeys.group_exists(group_id)) if value is None: return None return str(value, 'utf-8') == '1'
def set_group_exists(self, group_id: str, exists: bool) -> None: self.redis.set(RedisKeys.group_exists(group_id), '1' if exists else '0')
def clear_user_ids_and_join_time_in_group(self, group_id: str) -> None: key = RedisKeys.user_in_group(group_id) self.redis.delete(key)
def remove_last_read_in_group_for_user(self, group_id: str, user_id: int) -> None: key = RedisKeys.last_read_time(group_id) self.redis.hdel(key, str(user_id))
def set_unread_in_group(self, group_id: str, user_id: int, unread: int) -> None: key = RedisKeys.unread_in_group(group_id) self.redis.hset(key, str(user_id), unread)
def set_last_message_time_in_group(self, group_id: str, last_message_time: float): key = RedisKeys.last_message_time(group_id) self.redis.set(key, last_message_time) self.redis.expire(key, ONE_WEEK)
def reset_count_group_types_for_user(self, user_id: int) -> None: key = RedisKeys.count_group_types_including_hidden(user_id) self.redis.delete(key) key = RedisKeys.count_group_types_not_including_hidden(user_id) self.redis.delete(key)
def set_last_sent_for_user(self, user_id: int, group_id: str, last_time: float) -> None: key = RedisKeys.last_sent_time_user(user_id) self.redis.set(key, f"{group_id}:{last_time}")
def set_messages_in_group(self, group_id: str, n_messages: int, until: float) -> None: key = RedisKeys.messages_in_group(group_id) messages_until = f"{n_messages}|{until}" self.redis.set(key, messages_until) self.redis.expire(key, ONE_HOUR) # can't cache forever, since users may delete historical messages