Compare commits

..

2 commits

Author SHA1 Message Date
17aee620b3 build 2025-07-17 10:25:01 -03:00
3c7926df4d criado cache m 2025-07-17 10:21:43 -03:00
10 changed files with 132 additions and 1 deletions

49
dist-back/cacheMemoria.js Normal file
View file

@ -0,0 +1,49 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cacheMemoria_exports = {};
__export(cacheMemoria_exports, {
cacheM: () => cacheM,
cacheMemoria: () => cacheMemoria,
verCacheM: () => verCacheM
});
module.exports = __toCommonJS(cacheMemoria_exports);
const _cache = {};
const cacheM = (chave, valor, validadeSeg) => {
const txChave = typeof chave == "string" ? chave : typeof chave == "number" ? String(chave) : encodeURIComponent(JSON.stringify(chave));
const validade = validadeSeg && (/* @__PURE__ */ new Date()).getTime() + validadeSeg * 1e3;
if (valor !== void 0) {
_cache[txChave] = {
valor,
validade
};
}
const busca = _cache[txChave];
if (busca?.validade && busca.validade < (/* @__PURE__ */ new Date()).getTime()) {
return void 0;
}
return busca?.valor;
};
const verCacheM = () => _cache;
const cacheMemoria = cacheM;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
cacheM,
cacheMemoria,
verCacheM
});

View file

@ -16,6 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
var index_exports = {};
module.exports = __toCommonJS(index_exports);
__reExport(index_exports, require("./aleatorio"), module.exports);
__reExport(index_exports, require("./cacheMemoria"), module.exports);
__reExport(index_exports, require("./constantes"), module.exports);
__reExport(index_exports, require("./consulta"), module.exports);
__reExport(index_exports, require("./ecosistema"), module.exports);
@ -29,6 +30,7 @@ __reExport(index_exports, require("./variaveisComuns"), module.exports);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
...require("./aleatorio"),
...require("./cacheMemoria"),
...require("./constantes"),
...require("./consulta"),
...require("./ecosistema"),

View file

@ -1,3 +1,6 @@
"use strict";
var import_cacheMemoria = require("./cacheMemoria");
var import_texto_busca = require("./texto_busca");
console.log("Vari\xE1veis funcionando", import_texto_busca.texto_busca);
(0, import_cacheMemoria.cacheM)(1, { Jaca: Promise.resolve() });
console.log("cache:", (0, import_cacheMemoria.cacheM)(1));

View file

@ -0,0 +1,23 @@
const _cache = {};
const cacheM = (chave, valor, validadeSeg) => {
const txChave = typeof chave == "string" ? chave : typeof chave == "number" ? String(chave) : encodeURIComponent(JSON.stringify(chave));
const validade = validadeSeg && (/* @__PURE__ */ new Date()).getTime() + validadeSeg * 1e3;
if (valor !== void 0) {
_cache[txChave] = {
valor,
validade
};
}
const busca = _cache[txChave];
if (busca?.validade && busca.validade < (/* @__PURE__ */ new Date()).getTime()) {
return void 0;
}
return busca?.valor;
};
const verCacheM = () => _cache;
const cacheMemoria = cacheM;
export {
cacheM,
cacheMemoria,
verCacheM
};

View file

@ -1,4 +1,5 @@
export * from "./aleatorio";
export * from "./cacheMemoria";
export * from "./constantes";
export * from "./consulta";
export * from "./ecosistema";

View file

@ -1,2 +1,5 @@
import { cacheM } from "./cacheMemoria";
import { texto_busca } from "./texto_busca";
console.log("Vari\xE1veis funcionando", texto_busca);
cacheM(1, { Jaca: Promise.resolve() });
console.log("cache:", cacheM(1));

View file

@ -1,6 +1,6 @@
{
"name": "p-comuns",
"version": "0.156.0",
"version": "0.158.0",
"description": "",
"main": "./src/index.ts",
"exports": {

45
src/cacheMemoria.ts Normal file
View 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

View file

@ -1,4 +1,5 @@
export * from "./aleatorio"
export * from "./cacheMemoria"
export * from "./constantes"
export * from "./consulta"
export * from "./ecosistema"

View file

@ -1,4 +1,8 @@
import { cacheM } from "./cacheMemoria"
import { texto_busca } from "./texto_busca"
// node dist-back/teste.mjs
console.log("Variáveis funcionando", texto_busca)
cacheM(1, { Jaca: Promise.resolve() })
console.log("cache:", cacheM(1))