From a0dcf50a655b2b25fe0dd71fe12f118a2edb19d1 Mon Sep 17 00:00:00 2001 From: itqop Date: Wed, 23 Oct 2024 00:22:01 +0300 Subject: [PATCH] Fix crash --- Dockerfile | 10 +++++----- app/api/routes.py | 2 +- app/core/cache.py | 23 +++++++++++++++++++---- app/requirements.txt | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 744d501..bb28b53 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/app/api/routes.py b/app/api/routes.py index 89a290f..55b7e62 100644 --- a/app/api/routes.py +++ b/app/api/routes.py @@ -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 diff --git a/app/core/cache.py b/app/core/cache.py index ccda863..5ffc233 100644 --- a/app/core/cache.py +++ b/app/core/cache.py @@ -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() diff --git a/app/requirements.txt b/app/requirements.txt index 95c88fc..a879e08 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -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