def get_team_id_by_token(token: str) -> Optional[int]: """ Get team by token. :param token: token string :return: team id """ with storage.utils.redis_pipeline(transaction=False) as pipe: team_id, = pipe.get(CacheKeys.team_by_token(token)).execute() try: team_id = int(team_id) except (ValueError, TypeError): return None else: return team_id
def cache_teams(pipe: Pipeline) -> None: """ Put "teams" table data from database to cache. Just adds commands to pipeline stack, don't forget to execute afterwards. """ with utils.db_cursor(dict_cursor=True) as (_, curs): curs.execute(models.Team.get_select_active_query()) teams = curs.fetchall() teams = list(models.Team.from_dict(team) for team in teams) key = CacheKeys.teams() pipe.delete(key) if teams: pipe.sadd(key, *[team.to_json() for team in teams]) for team in teams: pipe.set(CacheKeys.team_by_token(team.token), team.id)