From 4ed8af9ad8db3457412a8375a40d3f045c03c85d Mon Sep 17 00:00:00 2001 From: Renan Bernordi Date: Fri, 22 Nov 2024 20:21:44 -0300 Subject: [PATCH] =?UTF-8?q?sistema=20de=20compress=C3=A3o=20dos=20caches?= =?UTF-8?q?=20em=20html?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Dockerfile | 15 ++++++++++++--- app/inc/Cache.php | 26 +++++++++++++++++++++----- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 13b3549..0411cca 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ composer.lock .env app/logs/*.log app/cache/*.html +app/cache/*.gz TODO.md # Created by https://www.toptal.com/developers/gitignore/api/composer,windows,macos,linux diff --git a/Dockerfile b/Dockerfile index e423cf6..021948a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,15 @@ FROM php:8.0-fpm -RUN apt-get update && apt-get install -y nginx nano procps unzip git htop +# Instala dependências e extensões do PHP +RUN apt-get update && apt-get install -y \ + nginx \ + nano \ + procps \ + zip \ + git \ + htop \ + libzip-dev \ + && docker-php-ext-install zip RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer @@ -12,13 +21,13 @@ COPY app/ /app/ WORKDIR /app RUN composer install --no-interaction --optimize-autoloader -# Copy and set permissions for entrypoint script +# Copia e configura permissões do script de inicialização COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh RUN mkdir -p /app/cache /app/logs -# Set base permissions for /app +# Configura permissões base para o diretório /app RUN chown -R www-data:www-data /app \ && chmod -R 755 /app diff --git a/app/inc/Cache.php b/app/inc/Cache.php index db4fb0d..e8f1b64 100644 --- a/app/inc/Cache.php +++ b/app/inc/Cache.php @@ -5,6 +5,7 @@ * Esta classe implementa funcionalidades para armazenar e recuperar * conteúdo em cache, utilizando o sistema de arquivos como storage. * O cache é organizado por URLs convertidas em IDs únicos usando SHA-256. + * O conteúdo é comprimido usando gzip para economizar espaço em disco. */ class Cache { /** @@ -45,7 +46,7 @@ class Cache { */ public function exists($url) { $id = $this->generateId($url); - $cachePath = $this->cacheDir . '/' . $id . '.html'; + $cachePath = $this->cacheDir . '/' . $id . '.gz'; return file_exists($cachePath); } @@ -60,8 +61,15 @@ class Cache { return null; } $id = $this->generateId($url); - $cachePath = $this->cacheDir . '/' . $id . '.html'; - return file_get_contents($cachePath); + $cachePath = $this->cacheDir . '/' . $id . '.gz'; + + // Lê e descomprime o conteúdo + $compressedContent = file_get_contents($cachePath); + if ($compressedContent === false) { + return null; + } + + return gzdecode($compressedContent); } /** @@ -69,10 +77,18 @@ class Cache { * * @param string $url URL associada ao conteúdo * @param string $content Conteúdo a ser armazenado em cache + * @return bool True se o cache foi salvo com sucesso, False caso contrário */ public function set($url, $content) { $id = $this->generateId($url); - $cachePath = $this->cacheDir . '/' . $id . '.html'; - file_put_contents($cachePath, $content); + $cachePath = $this->cacheDir . '/' . $id . '.gz'; + + // Comprime o conteúdo usando gzip + $compressedContent = gzencode($content, 3); + if ($compressedContent === false) { + return false; + } + + return file_put_contents($cachePath, $compressedContent) !== false; } }