diff --git a/dist-back/cacheMemoria.js b/dist-back/cacheMemoria.js new file mode 100644 index 0000000..35fc326 --- /dev/null +++ b/dist-back/cacheMemoria.js @@ -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 +}); diff --git a/dist-back/index.js b/dist-back/index.js index 43e949e..f24d44b 100644 --- a/dist-back/index.js +++ b/dist-back/index.js @@ -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"), diff --git a/dist-back/teste.js b/dist-back/teste.js index f9ed5e5..c2d0e9c 100644 --- a/dist-back/teste.js +++ b/dist-back/teste.js @@ -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)); diff --git a/dist-front/cacheMemoria.js b/dist-front/cacheMemoria.js new file mode 100644 index 0000000..658175a --- /dev/null +++ b/dist-front/cacheMemoria.js @@ -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 +}; diff --git a/dist-front/index.js b/dist-front/index.js index eaca916..2e58fa8 100644 --- a/dist-front/index.js +++ b/dist-front/index.js @@ -1,4 +1,5 @@ export * from "./aleatorio"; +export * from "./cacheMemoria"; export * from "./constantes"; export * from "./consulta"; export * from "./ecosistema"; diff --git a/dist-front/teste.js b/dist-front/teste.js index 5242bfb..29422ff 100644 --- a/dist-front/teste.js +++ b/dist-front/teste.js @@ -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)); diff --git a/package.json b/package.json index eb567dd..dbeeb2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "p-comuns", - "version": "0.156.0", + "version": "0.158.0", "description": "", "main": "./src/index.ts", "exports": { diff --git a/src/cacheMemoria.ts b/src/cacheMemoria.ts new file mode 100644 index 0000000..5a17cc2 --- /dev/null +++ b/src/cacheMemoria.ts @@ -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 = ( + 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 diff --git a/src/index.ts b/src/index.ts index c61ba3a..b5c6f4d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from "./aleatorio" +export * from "./cacheMemoria" export * from "./constantes" export * from "./consulta" export * from "./ecosistema" diff --git a/src/teste.ts b/src/teste.ts index 93e1015..77f84c2 100644 --- a/src/teste.ts +++ b/src/teste.ts @@ -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))