This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { memoize } from './memoize'; | |
// Função simulando cálculo pesado | |
function slowAdd(a: number, b: number): number { | |
console.log(`Calculando: ${a} + ${b}`); | |
return a + b; | |
} | |
// Criando função memoizada | |
const memoAdd = memoize(slowAdd, { maxSize: 2, ttl: 3000 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Memoiza o resultado de uma função com suporte a múltiplos parâmetros, | |
* controle de expiração por tempo (TTL) e limite de tamanho do cache. | |
* | |
* @param fn - A função original a ser memoizada. | |
* @param options - Configurações opcionais: maxSize (número máx. de itens no cache), ttl (tempo em ms). | |
* @returns Uma nova função que cacheia os resultados com base nos argumentos. | |
*/ | |
export function memoize<T extends (...args: any[]) => any>( | |
fn: T, |