Skip to content

Commit

Permalink
sistema de compressão dos caches em html
Browse files Browse the repository at this point in the history
  • Loading branch information
altendorfme committed Nov 22, 2024
1 parent 380d101 commit 4ed8af9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
26 changes: 21 additions & 5 deletions app/inc/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ public function generateId($url) {
*/
public function exists($url) {
$id = $this->generateId($url);
$cachePath = $this->cacheDir . '/' . $id . '.html';
$cachePath = $this->cacheDir . '/' . $id . '.gz';
return file_exists($cachePath);
}

Expand All @@ -60,19 +61,34 @@ public function get($url) {
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);
}

/**
* Armazena conteúdo em cache para uma URL
*
* @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;
}
}

0 comments on commit 4ed8af9

Please sign in to comment.