mirror of
https://github.com/manualdousuario/marreta.git
synced 2025-09-01 18:20:22 +00:00
suporte ao redis para salvar a contagem de arquivos
This commit is contained in:
parent
69bc8640b0
commit
e660074530
5 changed files with 167 additions and 19 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
121
app/inc/Cache/RedisStorage.php
Normal file
121
app/inc/Cache/RedisStorage.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
namespace Inc\Cache;
|
||||
|
||||
class RedisStorage implements CacheStorageInterface
|
||||
{
|
||||
/**
|
||||
* @var \Redis|null Redis client instance
|
||||
*/
|
||||
private $redis;
|
||||
|
||||
/**
|
||||
* @var string Diretório de cache para contagem de arquivos
|
||||
*/
|
||||
private $cacheDir;
|
||||
|
||||
/**
|
||||
* Construtor da classe
|
||||
*
|
||||
* @param string $cacheDir Diretório base para armazenamento do cache
|
||||
*/
|
||||
public function __construct(string $cacheDir)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="pt-BR">
|
||||
|
@ -56,22 +61,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) {
|
|||
<body class="bg-gray-50 min-h-screen">
|
||||
<div class="container mx-auto px-4 py-8 max-w-4xl">
|
||||
<!-- Cabeçalho da página -->
|
||||
<div class="text-center mb-8">
|
||||
<h1 class="text-4xl font-bold text-gray-800 mb-4">
|
||||
<img src="assets/svg/marreta.svg" class="inline-block w-12 h-12 mb-2" alt="Marreta">
|
||||
<?php echo SITE_NAME; ?>
|
||||
</h1>
|
||||
<p class="text-gray-600 text-lg"><?php echo SITE_DESCRIPTION; ?></p>
|
||||
<p class="text-gray-600 text-lg">
|
||||
<span class="font-bold text-blue-600">
|
||||
<?php
|
||||
$cache_folder = iterator_count(new FilesystemIterator('cache/'));
|
||||
echo number_format($cache_folder, 0, ',', '.');
|
||||
?>
|
||||
</span>
|
||||
<span>paredes derrubadas!</span>
|
||||
</p>
|
||||
</div>
|
||||
<div class="text-center mb-8">
|
||||
<h1 class="text-4xl font-bold text-gray-800 mb-4">
|
||||
<img src="assets/svg/marreta.svg" class="inline-block w-12 h-12 mb-2" alt="Marreta">
|
||||
<?php echo SITE_NAME; ?>
|
||||
</h1>
|
||||
<p class="text-gray-600 text-lg"><?php echo SITE_DESCRIPTION; ?></p>
|
||||
<p class="text-gray-600 text-lg">
|
||||
<span class="font-bold text-blue-600">
|
||||
<?php echo number_format($cache_folder, 0, ',', '.'); ?>
|
||||
</span>
|
||||
<span>paredes derrubadas!</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- Formulário principal de análise de URLs -->
|
||||
<div class="bg-white rounded-xl shadow-lg p-8 mb-8">
|
||||
|
@ -231,5 +233,4 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) {
|
|||
?>
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
Loading…
Add table
Reference in a new issue