def get_teams() -> List[models.Team]: """Get list of active teams.""" key = CacheKeys.teams() with storage.utils.redis_pipeline(transaction=True) as pipe: cache_helper( pipeline=pipe, cache_key=key, cache_func=storage.caching.cache_teams, cache_args=(pipe, ), ) teams, = pipe.smembers(key).execute() teams = list(models.Team.from_json(team) for team in teams) return teams
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)
def flush_teams_cache(): with utils.redis_pipeline(transaction=False) as pipe: pipe.delete(CacheKeys.teams()).execute()