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 */
910class 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