📊 Capítulo 15: Caching com Redis no Laravel
Especialização Backend com PHP & Laravel
🗺️ O Padrão Cache-Aside com Tags e Redis
flowchart TD
GET["Cache::tags(['produtos', 'eletronicos'])->remember('destaques', 3600, fn() => ...)"]
--> CHECK{"O Dado existe no Redis?"}
CHECK -- "SIM (Cache Hit)" --> RESP["Retorna Array/Object do Redis em < 2 ms ⚡"]
CHECK -- "NÃO (Cache Miss)" --> DB["Executa consulta SQL complexa no PostgreSQL/MySQL"]
DB --> POPULATE["Grava JSON no Redis com TTL de 3600s associado às tags"]
POPULATE --> RESP
FLUSH["Novo Produto Criado!"] --> CLEAR["Cache::tags(['produtos'])->flush() (Invalida todas as chaves da tag atomicamente!)"]
style GET fill:#ede7f6,stroke:#512da8,stroke-width:2px
style CHECK fill:#fff3e0,stroke:#ff9800,stroke-width:1.5px
style RESP fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px
style DB fill:#ffebee,stroke:#d32f2f,stroke-width:1.5px
style POPULATE fill:#e8f5e9,stroke:#4caf50,stroke-width:1.5px
style FLUSH fill:#ede7f6,stroke:#7e57c2,stroke-width:1.5px
style CLEAR fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px
🎯 Slide 1: A Elegância do Cache::remember
$categorias = Cache::remember('categorias_menu', now()->addHours(6), function () {
return Categoria::with('subcategorias')->get();
});
🎯 Slide 2: Cache Tags no Redis (Invalidação Inteligente)
- Permite agrupar centenas de chaves sob tags lógicas:
Cache::tags(['produtos'])->flush(); // Limpa todo o cache de produtos de uma só vez!
🎯 Slide 3: Locks Atômicos Distribuídos
Cache::lock('processar-faturamento-10', 10)->get(function () {
// Execução exclusiva protegida contra concorrência!
});
- Impede que duas requisições simultâneas cobrem o mesmo pedido ao mesmo tempo!
Slide 1 de 1
Atalhos: ← → Navegar • Espaço Avançar • F Tela Cheia • Home Início • End Fim