fix(prefeitura): garantir que o cache do Redis seja utilizado corretamente e evitar falhas
All checks were successful
Dev Build & Deploy Portal / build-deploy (push) Successful in 2m32s

This commit is contained in:
Gabriel Bezerra 2026-05-19 21:10:29 -03:00
parent 8b5c37abe9
commit b638af1a39

View File

@ -17,13 +17,18 @@ export async function fetchPrefeituraInfo(dominio: string): Promise<PrefeituraIn
if (!dominio) return null
const cacheKey = `prefeitura:${dominio}`
const cached = await useRedis().get(cacheKey)
if (cached) {
try {
return JSON.parse(cached) as PrefeituraInfo
} catch {
// cache corrompido — segue para refetch
let redisAvailable = true
try {
const cached = await useRedis().get(cacheKey)
if (cached) {
try {
return JSON.parse(cached) as PrefeituraInfo
} catch {
// cache corrompido — segue para refetch
}
}
} catch {
redisAvailable = false
}
const cfg = useRuntimeConfig()
@ -35,7 +40,9 @@ export async function fetchPrefeituraInfo(dominio: string): Promise<PrefeituraIn
const info = res?.data
if (!info?.codigoMunicipio) return null
await useRedis().set(cacheKey, JSON.stringify(info), 'EX', CACHE_TTL_SECONDS)
if (redisAvailable) {
await useRedis().set(cacheKey, JSON.stringify(info), 'EX', CACHE_TTL_SECONDS).catch(() => {})
}
return info
} catch (err) {
console.error(`[prefeitura] lookup falhou para '${dominio}':`, (err as Error).message)