melhorias de caches
This commit is contained in:
parent
29ae7400cf
commit
00e9c4d698
18 changed files with 210 additions and 66 deletions
4
dist-import/lista-vinculos.d.ts
vendored
4
dist-import/lista-vinculos.d.ts
vendored
|
|
@ -10,7 +10,9 @@ export type tipo_retorno_vinculo_listas = {
|
|||
_inquilino_nome: string;
|
||||
_inquilino_base_url: string;
|
||||
};
|
||||
export declare const listarVinculos: ({ token, url_api_autenticacao, }: {
|
||||
export declare const listarVinculos: ({ token, url_api_autenticacao, desativarCache, }: {
|
||||
url_api_autenticacao: string;
|
||||
token: string;
|
||||
/** por padrão será 10 segundos */
|
||||
desativarCache?: boolean;
|
||||
}) => Promise<tipoResposta<tipo_retorno_vinculo_listas[]>>;
|
||||
|
|
|
|||
|
|
@ -1,25 +1,45 @@
|
|||
import cFetch from "cross-fetch";
|
||||
import { respostaComuns } from "p-respostas";
|
||||
import { cacheAuDrive } from "./plugins/node-cache";
|
||||
import { uuidV3 } from "./plugins/uuid";
|
||||
export const tx_vinculos__listar = "vinculos__listar";
|
||||
export const listarVinculos = async ({ token, url_api_autenticacao, }) => {
|
||||
const url = `${url_api_autenticacao}/api/${tx_vinculos__listar}`;
|
||||
return cFetch(url, {
|
||||
headers: { token, "Content-Type": "application/json" },
|
||||
body: "{}",
|
||||
method: "post",
|
||||
})
|
||||
.then(async (a) => {
|
||||
const texto = await a.text();
|
||||
export const listarVinculos = async ({ token, url_api_autenticacao, desativarCache, }) => {
|
||||
const chaveCache = uuidV3({ token, url_api_autenticacao });
|
||||
// Buscar promeiro no cache
|
||||
if (!desativarCache) {
|
||||
const valorCache = cacheAuDrive.get(chaveCache);
|
||||
if (valorCache)
|
||||
return valorCache;
|
||||
}
|
||||
const res = (async () => {
|
||||
const url = `${url_api_autenticacao}/api/${tx_vinculos__listar}`;
|
||||
return cFetch(url, {
|
||||
headers: { token, "Content-Type": "application/json" },
|
||||
body: "{}",
|
||||
method: "post",
|
||||
})
|
||||
.then(async (a) => {
|
||||
const texto = await a.text();
|
||||
try {
|
||||
const res = JSON.parse(texto);
|
||||
return res;
|
||||
}
|
||||
catch (error) {
|
||||
return respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [texto, error]);
|
||||
}
|
||||
})
|
||||
.catch((error) => respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [
|
||||
error,
|
||||
]));
|
||||
})();
|
||||
cacheAuDrive.set(chaveCache, res.then((a) => {
|
||||
try {
|
||||
const res = JSON.parse(texto);
|
||||
return res;
|
||||
if (a.eErro) {
|
||||
cacheAuDrive.del(chaveCache);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
return respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [
|
||||
texto,
|
||||
error,
|
||||
]);
|
||||
}
|
||||
})
|
||||
.catch((error) => respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [error]));
|
||||
catch { }
|
||||
return a;
|
||||
}), 10);
|
||||
return res;
|
||||
};
|
||||
|
|
|
|||
2
dist-import/plugins/node-cache.d.ts
vendored
Normal file
2
dist-import/plugins/node-cache.d.ts
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import NodeCache from "node-cache";
|
||||
export declare const cacheAuDrive: NodeCache;
|
||||
2
dist-import/plugins/node-cache.js
Normal file
2
dist-import/plugins/node-cache.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import NodeCache from "node-cache";
|
||||
export const cacheAuDrive = new NodeCache();
|
||||
3
dist-import/plugins/uuid.d.ts
vendored
Normal file
3
dist-import/plugins/uuid.d.ts
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { v4 } from "uuid";
|
||||
export declare const uuidV3: (qualquerCoisa: any) => string;
|
||||
export declare const uuidV4: typeof v4;
|
||||
7
dist-import/plugins/uuid.js
Normal file
7
dist-import/plugins/uuid.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { NIL, v3, v4 } from "uuid";
|
||||
export const uuidV3 = (qualquerCoisa) => v3(typeof qualquerCoisa == "string"
|
||||
? qualquerCoisa
|
||||
: typeof qualquerCoisa == "number"
|
||||
? String(qualquerCoisa)
|
||||
: JSON.stringify(qualquerCoisa), NIL);
|
||||
export const uuidV4 = v4;
|
||||
Loading…
Add table
Add a link
Reference in a new issue