criado cache m
This commit is contained in:
parent
326112a16a
commit
3c7926df4d
7 changed files with 122 additions and 1 deletions
45
src/cacheMemoria.ts
Normal file
45
src/cacheMemoria.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/** gerar uma função de cache para uso em memoria */
|
||||
|
||||
const _cache: {
|
||||
[k: string]:
|
||||
| {
|
||||
/** new Date().getTime() */
|
||||
validade?: number | undefined
|
||||
valor: any
|
||||
}
|
||||
| undefined
|
||||
} = {}
|
||||
|
||||
export const cacheM = <T>(
|
||||
chave: any,
|
||||
valor?: T,
|
||||
validadeSeg?: number,
|
||||
): T | undefined => {
|
||||
// converte a chave e string
|
||||
const txChave: string =
|
||||
typeof chave == "string"
|
||||
? chave
|
||||
: typeof chave == "number"
|
||||
? String(chave)
|
||||
: encodeURIComponent(JSON.stringify(chave))
|
||||
|
||||
const validade = validadeSeg && new Date().getTime() + validadeSeg * 1000
|
||||
|
||||
if (valor !== undefined) {
|
||||
_cache[txChave] = {
|
||||
valor,
|
||||
validade,
|
||||
}
|
||||
}
|
||||
|
||||
const busca = _cache[txChave]
|
||||
|
||||
if (busca?.validade && busca.validade < new Date().getTime()) {
|
||||
return undefined
|
||||
}
|
||||
return busca?.valor
|
||||
}
|
||||
|
||||
export const verCacheM = () => _cache
|
||||
|
||||
export const cacheMemoria = cacheM
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
export * from "./aleatorio"
|
||||
export * from "./cacheMemoria"
|
||||
export * from "./constantes"
|
||||
export * from "./consulta"
|
||||
export * from "./ecosistema"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue