Fix crash

This commit is contained in:
itqop 2024-10-23 00:22:01 +03:00
parent ed5266594d
commit a0dcf50a65
4 changed files with 26 additions and 11 deletions

View File

@ -1,13 +1,13 @@
# Dockerfile # Dockerfile
FROM python:3.11-alpine FROM python:3.11-slim
# Установка системных зависимостей # Установка системных зависимостей
RUN apk update && apk add --no-cache \ RUN apt-get update && apt-get install -y --no-install-recommends \
build-base \ build-essential \
libffi-dev \ libffi-dev \
openssl-dev \ libssl-dev \
&& rm -rf /var/cache/apk/* && rm -rf /var/lib/apt/lists/*
COPY app /app COPY app /app

View File

@ -1,6 +1,6 @@
# app/api/routes.py # app/api/routes.py
from fastapi import APIRouter, HTTPException, Depends from fastapi import APIRouter, HTTPException
from app.models.schemas import TextInput, ToxicityOutput from app.models.schemas import TextInput, ToxicityOutput
from app.core.cache import cache from app.core.cache import cache
import json import json

View File

@ -1,26 +1,41 @@
import aioredis # app/core/cache.py
import asyncio
import json
import hashlib
import logging
from typing import Optional
import redis.asyncio as aioredis
from app.core.config import settings from app.core.config import settings
logger = logging.getLogger(__name__)
class RedisCache: class RedisCache:
def __init__(self): def __init__(self):
self.redis = None self.redis: Optional[aioredis.Redis] = None
async def connect(self): async def connect(self):
self.redis = await aioredis.from_url(settings.REDIS_URL, encoding="utf-8", decode_responses=True) if not self.redis:
self.redis = aioredis.from_url(settings.REDIS_URL, encoding="utf-8", decode_responses=True)
logger.info("Подключение к Redis установлено.")
async def disconnect(self): async def disconnect(self):
if self.redis: if self.redis:
await self.redis.close() await self.redis.close()
logger.info("Подключение к Redis закрыто.")
async def get(self, key: str): async def get(self, key: str):
if not self.redis: if not self.redis:
await self.connect() await self.connect()
return await self.redis.get(key) value = await self.redis.get(key)
logger.debug(f"Получено из кеша по ключу {key}: {value}")
return value
async def set(self, key: str, value: str, expire: int = 3600): async def set(self, key: str, value: str, expire: int = 3600):
if not self.redis: if not self.redis:
await self.connect() await self.connect()
await self.redis.set(key, value, ex=expire) await self.redis.set(key, value, ex=expire)
logger.debug(f"Сохранено в кеш по ключу {key}: {value} с истечением {expire} секунд")
cache = RedisCache() cache = RedisCache()

View File

@ -2,7 +2,7 @@ fastapi
uvicorn[standard] uvicorn[standard]
transformers transformers
gunicorn gunicorn
aioredis==2.0.1 redis>=4.0
celery==5.3.0 celery==5.3.0
redis==4.5.5 redis==4.5.5
pydantic_settings pydantic_settings