diff --git a/Dockerfile b/Dockerfile index a5825f3..df42f44 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,8 +9,10 @@ RUN apt-get update && apt-get install -y \ git \ htop \ libzip-dev \ + libhiredis-dev \ && docker-php-ext-install zip opcache \ - && docker-php-ext-enable opcache + && pecl install redis \ + && docker-php-ext-enable redis opcache # Copia a configuração do OPCache COPY opcache.ini /usr/local/etc/php/conf.d/opcache.ini diff --git a/app/config.php b/app/config.php index 171f7cf..b60c2a0 100644 --- a/app/config.php +++ b/app/config.php @@ -26,9 +26,14 @@ define('SITE_NAME', isset($_ENV['SITE_NAME']) ? $_ENV['SITE_NAME'] : 'Marreta'); define('SITE_DESCRIPTION', isset($_ENV['SITE_DESCRIPTION']) ? $_ENV['SITE_DESCRIPTION'] : 'Chapéu de paywall é marreta!'); define('SITE_URL', isset($_ENV['SITE_URL']) ? $_ENV['SITE_URL'] : 'https://' . $_SERVER['HTTP_HOST']); define('DNS_SERVERS', isset($_ENV['DNS_SERVERS']) ? $_ENV['DNS_SERVERS'] : '1.1.1.1, 8.8.8.8'); -define('CACHE_DIR', __DIR__ . '/cache'); define('DISABLE_CACHE', isset($_ENV['DISABLE_CACHE']) ? filter_var($_ENV['DISABLE_CACHE'], FILTER_VALIDATE_BOOLEAN) : false); define('SELENIUM_HOST', isset($_ENV['SELENIUM_HOST']) ? $_ENV['SELENIUM_HOST'] : 'localhost:4444'); +define('CACHE_DIR', __DIR__ . '/cache'); + +// Configurações de Redis +define('REDIS_HOST', isset($_ENV['REDIS_HOST']) ? $_ENV['REDIS_HOST'] : 'localhost'); +define('REDIS_PORT', isset($_ENV['REDIS_PORT']) ? $_ENV['REDIS_PORT'] : 6379); +define('REDIS_PREFIX', isset($_ENV['REDIS_PREFIX']) ? $_ENV['REDIS_PREFIX'] : 'marreta:'); /** * Configurações de Cache S3 diff --git a/app/inc/Cache.php b/app/inc/Cache.php index eba9b4a..3b79eff 100644 --- a/app/inc/Cache.php +++ b/app/inc/Cache.php @@ -3,6 +3,7 @@ use Inc\Cache\CacheStorageInterface; use Inc\Cache\DiskStorage; use Inc\Cache\S3Storage; +use Inc\Cache\RedisStorage; /** * Classe responsável pelo gerenciamento de cache do sistema @@ -19,6 +20,11 @@ class Cache */ private $storage; + /** + * @var RedisStorage Instância do Redis para contagem de arquivos + */ + private $redisStorage; + /** * Construtor da classe * @@ -26,6 +32,9 @@ class Cache */ public function __construct() { + // Inicializa o RedisStorage para contagem de arquivos + $this->redisStorage = new RedisStorage(CACHE_DIR); + // Se S3 está configurado e ativo, usa S3Storage if (defined('S3_CACHE_ENABLED') && S3_CACHE_ENABLED === true) { $this->storage = new S3Storage([ @@ -43,6 +52,16 @@ class Cache } } + /** + * Obtém a contagem de arquivos em cache + * + * @return int Número de arquivos em cache + */ + public function getCacheFileCount(): int + { + return $this->redisStorage->countCacheFiles(); + } + /** * Gera um ID único para uma URL * diff --git a/app/inc/Cache/RedisStorage.php b/app/inc/Cache/RedisStorage.php new file mode 100644 index 0000000..7576f04 --- /dev/null +++ b/app/inc/Cache/RedisStorage.php @@ -0,0 +1,121 @@ +cacheDir = $cacheDir; + + // Tenta inicializar conexão Redis + try { + $this->redis = new \Redis(); + $this->redis->connect(REDIS_HOST, REDIS_PORT, 2.5); + $this->redis->setOption(\Redis::OPT_PREFIX, REDIS_PREFIX); + } catch (\Exception $e) { + // Se falhar, define redis como null + $this->redis = null; + } + } + + /** + * Conta o número de arquivos no diretório de cache + * + * @return int Número de arquivos no diretório de cache + */ + public function countCacheFiles(): int + { + // Chave para armazenar a contagem de arquivos no Redis + $cacheCountKey = 'cache_file_count'; + + // Se Redis estiver disponível + if ($this->redis !== null) { + // Verifica se a chave existe e tem valor + $cachedCount = $this->redis->get($cacheCountKey); + if ($cachedCount !== false) { + return (int)$cachedCount; + } + } + + // Se Redis não estiver disponível ou chave vazia, conta os arquivos + $fileCount = iterator_count(new \FilesystemIterator($this->cacheDir)); + + // Se Redis estiver disponível, salva a contagem + if ($this->redis !== null) { + $this->redis->set($cacheCountKey, $fileCount); + } + + return $fileCount; + } + + /** + * Atualiza a contagem de arquivos no Redis + * + * @param int $count Número de arquivos + */ + public function updateCacheFileCount(int $count): void + { + if ($this->redis !== null) { + $this->redis->set('cache_file_count', $count); + } + } + + /** + * {@inheritdoc} + */ + public function exists(string $id): bool + { + return $this->redis !== null ? $this->redis->exists($id) : false; + } + + /** + * {@inheritdoc} + */ + public function get(string $id): ?string + { + if ($this->redis === null) { + return null; + } + + $content = $this->redis->get($id); + return $content === false ? null : $content; + } + + /** + * {@inheritdoc} + */ + public function set(string $id, string $content): bool + { + // Se Redis não estiver disponível, retorna false + if ($this->redis === null) { + return false; + } + + // Ao salvar um novo arquivo, atualiza a contagem + $result = $this->redis->set($id, $content); + + if ($result) { + // Incrementa a contagem de arquivos no Redis + $currentCount = $this->redis->get('cache_file_count') ?: 0; + $this->redis->set('cache_file_count', $currentCount + 1); + } + + return $result; + } +} diff --git a/app/index.php b/app/index.php index e30c664..66599cb 100644 --- a/app/index.php +++ b/app/index.php @@ -12,6 +12,7 @@ */ require_once 'config.php'; +require_once 'inc/Cache.php'; // Inicialização de variáveis $message = ''; @@ -36,6 +37,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) { $message_type = MESSAGES['INVALID_URL']['type']; } } + +// Inicializa o cache para contagem +$cache = new Cache(); +$cache_folder = $cache->getCacheFileCount(); ?> @@ -56,22 +61,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) {
- - - - paredes derrubadas! -
-+ + + + paredes derrubadas! +
+