From 3c7926df4df21b7edca8b84312315fd94f0abcc8 Mon Sep 17 00:00:00 2001 From: Luiz Silva Date: Thu, 17 Jul 2025 10:21:43 -0300 Subject: [PATCH] criado cache m --- dist-back/cacheMemoria.js | 49 ++++++++++++++++++++++++++++++++++++++ dist-back/index.js | 2 ++ dist-front/cacheMemoria.js | 23 ++++++++++++++++++ dist-front/index.js | 1 + package.json | 2 +- src/cacheMemoria.ts | 45 ++++++++++++++++++++++++++++++++++ src/index.ts | 1 + 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 dist-back/cacheMemoria.js create mode 100644 dist-front/cacheMemoria.js create mode 100644 src/cacheMemoria.ts 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-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/package.json b/package.json index eb567dd..431fe58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "p-comuns", - "version": "0.156.0", + "version": "0.157.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"