Sistema de Cache
Sistema de Cache
Section titled “Sistema de Cache”O HelperDB inclui um sistema de cache inteligente que melhora significativamente a performance das operações de leitura.
Configuração
Section titled “Configuração”const { HelperDB } = require('helper.db');
const db = new HelperDB({ driver: 'sqlite', filePath: './data.sqlite', cache: { enabled: true, maxSize: 1000, ttl: 300000, // 5 minutos strategy: 'lru' }});Opções de Cache
Section titled “Opções de Cache”enabled
Section titled “enabled”- Tipo:
boolean - Padrão:
true - Descrição: Ativa/desativa o sistema de cache
maxSize
Section titled “maxSize”- Tipo:
number - Padrão:
1000 - Descrição: Número máximo de entradas no cache
- Tipo:
number - Padrão:
300000(5 minutos) - Descrição: Time-to-live em milissegundos
strategy
Section titled “strategy”- Tipo:
'lru' | 'lfu' | 'fifo' - Padrão:
'lru' - Descrição: Estratégia de remoção de entradas
Estratégias de Cache
Section titled “Estratégias de Cache”LRU (Least Recently Used)
Section titled “LRU (Least Recently Used)”Remove as entradas usadas há mais tempo:
const db = new HelperDB({ cache: { strategy: 'lru', maxSize: 500 }});LFU (Least Frequently Used)
Section titled “LFU (Least Frequently Used)”Remove as entradas menos utilizadas:
const db = new HelperDB({ cache: { strategy: 'lfu', maxSize: 500 }});FIFO (First In, First Out)
Section titled “FIFO (First In, First Out)”Remove as entradas mais antigas:
const db = new HelperDB({ cache: { strategy: 'fifo', maxSize: 500 }});Métodos de Controle
Section titled “Métodos de Controle”Limpar Cache
Section titled “Limpar Cache”// Limpar todo o cacheawait db.cache.clear();
// Limpar entrada específicaawait db.cache.delete('chave');
// Verificar se existe no cacheconst exists = await db.cache.has('chave');Estatísticas do Cache
Section titled “Estatísticas do Cache”const stats = await db.cache.getStats();console.log(stats);// {// hits: 150,// misses: 50,// hitRate: 0.75,// size: 100,// maxSize: 1000// }Configurar TTL Específico
Section titled “Configurar TTL Específico”// TTL específico para uma entradaawait db.set('chave', 'valor', { cacheTtl: 600000 // 10 minutos});Exemplo Avançado
Section titled “Exemplo Avançado”const db = new HelperDB({ driver: 'mysql', cache: { enabled: true, maxSize: 2000, ttl: 600000, // 10 minutos strategy: 'lru',
// Configurações avançadas updateOnWrite: true, prefetch: ['user:*', 'config:*'], warmup: async (cache) => { // Pré-carregar dados importantes const configs = await db.search('config:'); for (const [key, value] of configs) { await cache.set(key, value); } } }});
await db.init();
// O cache será automaticamente usadoconst user = await db.get('user:1'); // Miss - vai ao bancoconst user2 = await db.get('user:1'); // Hit - vem do cache
// Monitorar performancesetInterval(async () => { const stats = await db.cache.getStats(); console.log(`Cache Hit Rate: ${(stats.hitRate * 100).toFixed(2)}%`);}, 30000);Performance
Section titled “Performance”- Melhoria típica: 5-50x mais rápido
- Uso de memória: ~1KB por entrada
- Overhead: < 1ms por operação