Compare commits
2 commits
326112a16a
...
17aee620b3
| Author | SHA1 | Date | |
|---|---|---|---|
| 17aee620b3 | |||
| 3c7926df4d |
10 changed files with 132 additions and 1 deletions
49
dist-back/cacheMemoria.js
Normal file
49
dist-back/cacheMemoria.js
Normal 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
|
||||||
|
});
|
||||||
|
|
@ -16,6 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
||||||
var index_exports = {};
|
var index_exports = {};
|
||||||
module.exports = __toCommonJS(index_exports);
|
module.exports = __toCommonJS(index_exports);
|
||||||
__reExport(index_exports, require("./aleatorio"), module.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("./constantes"), module.exports);
|
||||||
__reExport(index_exports, require("./consulta"), module.exports);
|
__reExport(index_exports, require("./consulta"), module.exports);
|
||||||
__reExport(index_exports, require("./ecosistema"), 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:
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
0 && (module.exports = {
|
0 && (module.exports = {
|
||||||
...require("./aleatorio"),
|
...require("./aleatorio"),
|
||||||
|
...require("./cacheMemoria"),
|
||||||
...require("./constantes"),
|
...require("./constantes"),
|
||||||
...require("./consulta"),
|
...require("./consulta"),
|
||||||
...require("./ecosistema"),
|
...require("./ecosistema"),
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
var import_cacheMemoria = require("./cacheMemoria");
|
||||||
var import_texto_busca = require("./texto_busca");
|
var import_texto_busca = require("./texto_busca");
|
||||||
console.log("Vari\xE1veis funcionando", import_texto_busca.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));
|
||||||
|
|
|
||||||
23
dist-front/cacheMemoria.js
Normal file
23
dist-front/cacheMemoria.js
Normal 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
|
||||||
|
};
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
export * from "./aleatorio";
|
export * from "./aleatorio";
|
||||||
|
export * from "./cacheMemoria";
|
||||||
export * from "./constantes";
|
export * from "./constantes";
|
||||||
export * from "./consulta";
|
export * from "./consulta";
|
||||||
export * from "./ecosistema";
|
export * from "./ecosistema";
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,5 @@
|
||||||
|
import { cacheM } from "./cacheMemoria";
|
||||||
import { texto_busca } from "./texto_busca";
|
import { texto_busca } from "./texto_busca";
|
||||||
console.log("Vari\xE1veis funcionando", texto_busca);
|
console.log("Vari\xE1veis funcionando", texto_busca);
|
||||||
|
cacheM(1, { Jaca: Promise.resolve() });
|
||||||
|
console.log("cache:", cacheM(1));
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "p-comuns",
|
"name": "p-comuns",
|
||||||
"version": "0.156.0",
|
"version": "0.158.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "./src/index.ts",
|
"main": "./src/index.ts",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|
|
||||||
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 "./aleatorio"
|
||||||
|
export * from "./cacheMemoria"
|
||||||
export * from "./constantes"
|
export * from "./constantes"
|
||||||
export * from "./consulta"
|
export * from "./consulta"
|
||||||
export * from "./ecosistema"
|
export * from "./ecosistema"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
|
import { cacheM } from "./cacheMemoria"
|
||||||
import { texto_busca } from "./texto_busca"
|
import { texto_busca } from "./texto_busca"
|
||||||
|
|
||||||
// node dist-back/teste.mjs
|
// node dist-back/teste.mjs
|
||||||
console.log("Variáveis funcionando", texto_busca)
|
console.log("Variáveis funcionando", texto_busca)
|
||||||
|
|
||||||
|
cacheM(1, { Jaca: Promise.resolve() })
|
||||||
|
console.log("cache:", cacheM(1))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue