|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + |
| 3 | + const noCacheBtn = document.getElementById('no-cache-btn'); |
| 4 | + const withCacheBtn = document.getElementById('with-cache-btn'); |
| 5 | + const resetBtn = document.getElementById('reset-btn'); |
| 6 | + |
| 7 | + const userEl = document.getElementById('user'); |
| 8 | + const cacheEl = document.getElementById('cache'); |
| 9 | + const serverEl = document.getElementById('server'); |
| 10 | + const packetEl = document.getElementById('packet'); |
| 11 | + const cacheStatusEl = document.getElementById('cache-status'); |
| 12 | + const serverStatusEl = document.getElementById('server-status'); |
| 13 | + const simulationLogEl = document.getElementById('simulation-log'); |
| 14 | + |
| 15 | + let isCached = false; |
| 16 | + let isAnimating = false; |
| 17 | + |
| 18 | + const getPosition = (el) => { |
| 19 | + const rect = el.getBoundingClientRect(); |
| 20 | + const containerRect = el.parentElement.getBoundingClientRect(); |
| 21 | + return { |
| 22 | + x: rect.left - containerRect.left + rect.width / 2 - packetEl.offsetWidth / 2, |
| 23 | + y: rect.top - containerRect.top + rect.height / 2 - packetEl.offsetHeight / 2, |
| 24 | + }; |
| 25 | + }; |
| 26 | + |
| 27 | + const animatePacket = (startEl, endEl, duration) => { |
| 28 | + return new Promise(resolve => { |
| 29 | + const startPos = getPosition(startEl); |
| 30 | + const endPos = getPosition(endEl); |
| 31 | + packetEl.style.transition = `all ${duration}s ease-in-out`; |
| 32 | + packetEl.style.left = `${startPos.x}px`; |
| 33 | + packetEl.style.top = `${startPos.y}px`; |
| 34 | + packetEl.style.opacity = '1'; |
| 35 | + |
| 36 | + setTimeout(() => { |
| 37 | + packetEl.style.left = `${endPos.x}px`; |
| 38 | + packetEl.style.top = `${endPos.y}px`; |
| 39 | + }, 100); |
| 40 | + |
| 41 | + setTimeout(resolve, duration * 1000 + 100); |
| 42 | + }); |
| 43 | + }; |
| 44 | + |
| 45 | + const resetState = () => { |
| 46 | + isCached = false; |
| 47 | + isAnimating = false; |
| 48 | + cacheStatusEl.textContent = '(Vazio)'; |
| 49 | + cacheStatusEl.style.color = ''; |
| 50 | + serverStatusEl.textContent = '(Ocioso)'; |
| 51 | + cacheEl.classList.remove('processing'); |
| 52 | + serverEl.classList.remove('processing'); |
| 53 | + packetEl.style.opacity = '0'; |
| 54 | + simulationLogEl.textContent = ''; |
| 55 | + }; |
| 56 | + |
| 57 | + noCacheBtn.addEventListener('click', async () => { |
| 58 | + if (isAnimating) return; |
| 59 | + isAnimating = true; |
| 60 | + simulationLogEl.textContent = 'Iniciando requisição sem cache...'; |
| 61 | + |
| 62 | + serverStatusEl.textContent = '(Processando...)'; |
| 63 | + serverEl.classList.add('processing'); |
| 64 | + |
| 65 | + await animatePacket(userEl, serverEl, 1.5); |
| 66 | + simulationLogEl.textContent = 'Servidor processando a requisição...'; |
| 67 | + |
| 68 | + await new Promise(r => setTimeout(r, 1500)); |
| 69 | + |
| 70 | + serverStatusEl.textContent = '(Ocioso)'; |
| 71 | + serverEl.classList.remove('processing'); |
| 72 | + |
| 73 | + await animatePacket(serverEl, userEl, 1.5); |
| 74 | + simulationLogEl.textContent = 'Requisição sem cache concluída. (Lento)'; |
| 75 | + |
| 76 | + packetEl.style.opacity = '0'; |
| 77 | + isAnimating = false; |
| 78 | + }); |
| 79 | + |
| 80 | + withCacheBtn.addEventListener('click', async () => { |
| 81 | + if (isAnimating) return; |
| 82 | + isAnimating = true; |
| 83 | + |
| 84 | + if (!isCached) { |
| 85 | + simulationLogEl.textContent = 'Primeira requisição: buscando no servidor...'; |
| 86 | + serverStatusEl.textContent = '(Processando...)'; |
| 87 | + serverEl.classList.add('processing'); |
| 88 | + |
| 89 | + await animatePacket(userEl, serverEl, 1.5); |
| 90 | + simulationLogEl.textContent = 'Servidor processando e salvando no cache...'; |
| 91 | + |
| 92 | + await new Promise(r => setTimeout(r, 1500)); |
| 93 | + |
| 94 | + isCached = true; |
| 95 | + cacheStatusEl.textContent = '(Preenchido)'; |
| 96 | + cacheStatusEl.style.color = '#10b981'; |
| 97 | + serverStatusEl.textContent = '(Ocioso)'; |
| 98 | + serverEl.classList.remove('processing'); |
| 99 | + |
| 100 | + await animatePacket(serverEl, userEl, 1.5); |
| 101 | + simulationLogEl.textContent = 'Dados entregues e cacheados.'; |
| 102 | + } else { |
| 103 | + simulationLogEl.textContent = 'Requisição subsequente: buscando no cache...'; |
| 104 | + cacheEl.classList.add('processing'); |
| 105 | + |
| 106 | + await animatePacket(userEl, cacheEl, 0.7); |
| 107 | + simulationLogEl.textContent = 'Cache encontrado! Entregando dados...'; |
| 108 | + |
| 109 | + await new Promise(r => setTimeout(r, 500)); |
| 110 | + cacheEl.classList.remove('processing'); |
| 111 | + |
| 112 | + await animatePacket(cacheEl, userEl, 0.7); |
| 113 | + simulationLogEl.textContent = 'Requisição com cache concluída. (Rápido!)'; |
| 114 | + } |
| 115 | + |
| 116 | + packetEl.style.opacity = '0'; |
| 117 | + isAnimating = false; |
| 118 | + }); |
| 119 | + |
| 120 | + resetBtn.addEventListener('click', resetState); |
| 121 | + |
| 122 | + const cacheTypeCards = document.querySelectorAll('.cache-type-card'); |
| 123 | + const typeDetailsEl = document.getElementById('type-details'); |
| 124 | + const typeDetailsContent = { |
| 125 | + browser: ` |
| 126 | + <h4 class="text-lg font-bold mb-2">Detalhes do Cache no Navegador</h4> |
| 127 | + <p>O navegador armazena arquivos estáticos como imagens, CSS e JavaScript no disco local do seu computador. Quando você visita o mesmo site novamente, o navegador carrega esses arquivos diretamente do seu disco em vez de baixá-los da internet, resultando em um carregamento de página quase instantâneo.</p> |
| 128 | + `, |
| 129 | + proxy: ` |
| 130 | + <h4 class="text-lg font-bold mb-2">Detalhes do Cache de Proxy / CDN</h4> |
| 131 | + <p>Uma Content Delivery Network (CDN) é uma rede de servidores proxy distribuídos globalmente. Eles armazenam cópias do conteúdo de um site. Quando um usuário faz uma solicitação, a CDN a entrega a partir do servidor mais próximo geograficamente, reduzindo a latência e a carga no servidor original.</p> |
| 132 | + `, |
| 133 | + server: ` |
| 134 | + <h4 class="text-lg font-bold mb-2">Detalhes do Cache no Servidor</h4> |
| 135 | + <p>Esta técnica envolve o armazenamento de dados frequentemente acessados na memória RAM do próprio servidor. Por exemplo, os resultados de uma consulta de banco de dados pesada podem ser cacheados. A próxima vez que esses dados forem necessários, eles são lidos da memória ultrarrápida em vez de executar a consulta novamente, acelerando a geração da página.</p> |
| 136 | + ` |
| 137 | + }; |
| 138 | + |
| 139 | + cacheTypeCards.forEach(card => { |
| 140 | + card.addEventListener('click', () => { |
| 141 | + const type = card.dataset.type; |
| 142 | + typeDetailsEl.innerHTML = typeDetailsContent[type]; |
| 143 | + |
| 144 | + cacheTypeCards.forEach(c => c.classList.remove('border-teal-500', 'bg-teal-50')); |
| 145 | + card.classList.add('border-teal-500', 'bg-teal-50'); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + const navLinks = document.querySelectorAll('.nav-link'); |
| 150 | + const sections = document.querySelectorAll('section'); |
| 151 | + |
| 152 | + window.addEventListener('scroll', () => { |
| 153 | + let current = ''; |
| 154 | + sections.forEach(section => { |
| 155 | + const sectionTop = section.offsetTop; |
| 156 | + if (pageYOffset >= sectionTop - 80) { |
| 157 | + current = section.getAttribute('id'); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + navLinks.forEach(link => { |
| 162 | + link.classList.remove('active-nav'); |
| 163 | + if (link.getAttribute('href') === `#${current}`) { |
| 164 | + link.classList.add('active-nav'); |
| 165 | + } |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments