From 2ec2e62304cd96566e764df752318290683ee652 Mon Sep 17 00:00:00 2001 From: Maarten Plieger Date: Wed, 25 Sep 2024 15:15:35 +0200 Subject: [PATCH] Reverted brotli change --- python/lib/adaguc/runAdaguc.py | 5 +++-- python/python_fastapi_server/routers/caching_middleware.py | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/python/lib/adaguc/runAdaguc.py b/python/lib/adaguc/runAdaguc.py index ba249c8b..584d743c 100644 --- a/python/lib/adaguc/runAdaguc.py +++ b/python/lib/adaguc/runAdaguc.py @@ -2,6 +2,7 @@ import os from PIL import Image from io import BytesIO +import brotli import shutil import random import string @@ -313,7 +314,7 @@ async def response_to_cache(redis_pool, key, headers: str, data): entrytime + f"{len(cacheable_headers_json):06d}".encode("utf-8") + cacheable_headers_json - + data.getvalue(), + + brotli.compress(data.getvalue(), quality = 4), ex=ttl, ) await redis_client.aclose() @@ -334,5 +335,5 @@ async def get_cached_response(redis_pool, key): headers = json.loads(cached[16 : 16 + headers_len].decode("utf-8")) headers.append(f"age: {age}") - data = cached[16 + headers_len :] + data = brotli.decompress(cached[16 + headers_len :]) return age, headers, BytesIO(data) diff --git a/python/python_fastapi_server/routers/caching_middleware.py b/python/python_fastapi_server/routers/caching_middleware.py index 867bfe6f..3f17368b 100644 --- a/python/python_fastapi_server/routers/caching_middleware.py +++ b/python/python_fastapi_server/routers/caching_middleware.py @@ -10,6 +10,7 @@ import redis.asyncio as redis # This can also be used to connect to a Redis cluster import json +import brotli ADAGUC_REDIS = os.environ.get("ADAGUC_REDIS") @@ -31,7 +32,7 @@ async def get_cached_response(redis_pool, request): headers_len = int(cached[10:16].decode("utf-8")) headers = json.loads(cached[16 : 16 + headers_len].decode("utf-8")) - data = cached[16 + headers_len :] + data = brotli.decompress(cached[16 + headers_len :]) return age, headers, data @@ -50,6 +51,7 @@ async def response_to_cache(redis_pool, request, headers, data, ex: int): entrytime = f"{calendar.timegm(datetime.utcnow().utctimetuple()):10d}".encode( "utf-8" ) + compressed_data = brotli.compress(data, quality=4) if len(data) < MAX_SIZE_FOR_CACHING: redis_client = redis.Redis(connection_pool=redis_pool) await redis_client.set( @@ -57,7 +59,7 @@ async def response_to_cache(redis_pool, request, headers, data, ex: int): entrytime + f"{len(headers_json):06d}".encode("utf-8") + headers_json - + data, + + compressed_data, ex=ex, ) await redis_client.aclose()