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

View File

@ -1,6 +1,6 @@
# app/api/routes.py
from fastapi import APIRouter, HTTPException, Depends
from fastapi import APIRouter, HTTPException
from app.models.schemas import TextInput, ToxicityOutput
from app.core.cache import cache
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
logger = logging.getLogger(__name__)
class RedisCache:
def __init__(self):
self.redis = None
self.redis: Optional[aioredis.Redis] = None
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):
if self.redis:
await self.redis.close()
logger.info("Подключение к Redis закрыто.")
async def get(self, key: str):
if not self.redis:
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):
if not self.redis:
await self.connect()
await self.redis.set(key, value, ex=expire)
logger.debug(f"Сохранено в кеш по ключу {key}: {value} с истечением {expire} секунд")
cache = RedisCache()

View File

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