backend optimizations + redundant commits cleanup
This commit is contained in:
+11
-1
@@ -1,16 +1,24 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from core.redis_cache import cache_delete, cache_get, cache_set
|
||||
from database.models import Setting
|
||||
|
||||
|
||||
_KEY = "SITE_INITIALIZED"
|
||||
_CACHE_KEY = "site_state:initialized"
|
||||
_CACHE_TTL_SEC = 300
|
||||
|
||||
|
||||
async def is_site_initialized(session: AsyncSession) -> bool:
|
||||
cached = await cache_get(_CACHE_KEY)
|
||||
if isinstance(cached, bool):
|
||||
return cached
|
||||
result = await session.execute(select(Setting).where(Setting.key == _KEY))
|
||||
setting = result.scalar_one_or_none()
|
||||
return bool(setting and setting.value is True)
|
||||
value = bool(setting and setting.value is True)
|
||||
await cache_set(_CACHE_KEY, value, _CACHE_TTL_SEC)
|
||||
return value
|
||||
|
||||
|
||||
async def mark_site_initialized(session: AsyncSession) -> None:
|
||||
@@ -21,6 +29,7 @@ async def mark_site_initialized(session: AsyncSession) -> None:
|
||||
session.add(Setting(key=_KEY, value=True, description="Сайт прошёл первую настройку админом"))
|
||||
elif setting.value is not True:
|
||||
setting.value = True
|
||||
await cache_delete(_CACHE_KEY)
|
||||
|
||||
|
||||
async def reset_site_initialized(session: AsyncSession) -> None:
|
||||
@@ -29,3 +38,4 @@ async def reset_site_initialized(session: AsyncSession) -> None:
|
||||
setting = result.scalar_one_or_none()
|
||||
if setting is not None:
|
||||
setting.value = False
|
||||
await cache_delete(_CACHE_KEY)
|
||||
|
||||
Reference in New Issue
Block a user