Skip to content

Commit 4ed8af9

Browse files
committed
sistema de compressão dos caches em html
1 parent 380d101 commit 4ed8af9

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ composer.lock
33
.env
44
app/logs/*.log
55
app/cache/*.html
6+
app/cache/*.gz
67
TODO.md
78

89
# Created by https://www.toptal.com/developers/gitignore/api/composer,windows,macos,linux

Dockerfile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
FROM php:8.0-fpm
22

3-
RUN apt-get update && apt-get install -y nginx nano procps unzip git htop
3+
# Instala dependências e extensões do PHP
4+
RUN apt-get update && apt-get install -y \
5+
nginx \
6+
nano \
7+
procps \
8+
zip \
9+
git \
10+
htop \
11+
libzip-dev \
12+
&& docker-php-ext-install zip
413

514
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
615

@@ -12,13 +21,13 @@ COPY app/ /app/
1221
WORKDIR /app
1322
RUN composer install --no-interaction --optimize-autoloader
1423

15-
# Copy and set permissions for entrypoint script
24+
# Copia e configura permissões do script de inicialização
1625
COPY docker-entrypoint.sh /usr/local/bin/
1726
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
1827

1928
RUN mkdir -p /app/cache /app/logs
2029

21-
# Set base permissions for /app
30+
# Configura permissões base para o diretório /app
2231
RUN chown -R www-data:www-data /app \
2332
&& chmod -R 755 /app
2433

app/inc/Cache.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Esta classe implementa funcionalidades para armazenar e recuperar
66
* conteúdo em cache, utilizando o sistema de arquivos como storage.
77
* O cache é organizado por URLs convertidas em IDs únicos usando SHA-256.
8+
* O conteúdo é comprimido usando gzip para economizar espaço em disco.
89
*/
910
class Cache {
1011
/**
@@ -45,7 +46,7 @@ public function generateId($url) {
4546
*/
4647
public function exists($url) {
4748
$id = $this->generateId($url);
48-
$cachePath = $this->cacheDir . '/' . $id . '.html';
49+
$cachePath = $this->cacheDir . '/' . $id . '.gz';
4950
return file_exists($cachePath);
5051
}
5152

@@ -60,19 +61,34 @@ public function get($url) {
6061
return null;
6162
}
6263
$id = $this->generateId($url);
63-
$cachePath = $this->cacheDir . '/' . $id . '.html';
64-
return file_get_contents($cachePath);
64+
$cachePath = $this->cacheDir . '/' . $id . '.gz';
65+
66+
// Lê e descomprime o conteúdo
67+
$compressedContent = file_get_contents($cachePath);
68+
if ($compressedContent === false) {
69+
return null;
70+
}
71+
72+
return gzdecode($compressedContent);
6573
}
6674

6775
/**
6876
* Armazena conteúdo em cache para uma URL
6977
*
7078
* @param string $url URL associada ao conteúdo
7179
* @param string $content Conteúdo a ser armazenado em cache
80+
* @return bool True se o cache foi salvo com sucesso, False caso contrário
7281
*/
7382
public function set($url, $content) {
7483
$id = $this->generateId($url);
75-
$cachePath = $this->cacheDir . '/' . $id . '.html';
76-
file_put_contents($cachePath, $content);
84+
$cachePath = $this->cacheDir . '/' . $id . '.gz';
85+
86+
// Comprime o conteúdo usando gzip
87+
$compressedContent = gzencode($content, 3);
88+
if ($compressedContent === false) {
89+
return false;
90+
}
91+
92+
return file_put_contents($cachePath, $compressedContent) !== false;
7793
}
7894
}

0 commit comments

Comments
 (0)