30 lines
724 B
Python
30 lines
724 B
Python
from functools import cache
|
|
from pathlib import Path
|
|
import redis
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session
|
|
from config.main import config
|
|
|
|
RedisPool = redis.ConnectionPool.from_url(config.REDIS_URL)
|
|
BACKEND_DIR = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def get_redis_client():
|
|
return redis.Redis(connection_pool=RedisPool)
|
|
|
|
|
|
def get_redis():
|
|
with get_redis_client() as redis_conn:
|
|
yield redis_conn
|
|
|
|
|
|
@cache
|
|
def get_db_engine(version='0'):
|
|
db_path = BACKEND_DIR / 'dataFiles' / f'dcalc_{version}.db'
|
|
return create_engine(f'sqlite:///{db_path}', connect_args={'check_same_thread': False})
|
|
|
|
|
|
def get_db():
|
|
with Session(get_db_engine()) as session:
|
|
yield session
|