def cache_tasks(pipe: Pipeline) -> None: """ Put active tasks 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.Task.get_select_active_query()) tasks = curs.fetchall() tasks = list(models.Task.from_dict(task) for task in tasks) key = CacheKeys.tasks() pipe.delete(key) if tasks: pipe.sadd(key, *(task.to_json() for task in tasks))
def get_tasks() -> List[models.Task]: """Get list of tasks registered in database.""" key = CacheKeys.tasks() with storage.utils.redis_pipeline(transaction=True) as pipe: cache_helper( pipeline=pipe, cache_key=key, cache_func=caching.cache_tasks, cache_args=(pipe, ), ) tasks, = pipe.smembers(key).execute() tasks = list(models.Task.from_json(task) for task in tasks) return tasks
def flush_tasks_cache(): with utils.redis_pipeline(transaction=False) as pipe: pipe.delete(CacheKeys.tasks()).execute()