Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
Lodash - Optional Memoize

Is it possible to avoid getting results from cache when calling a function wrapped in lodash _.memoize()?

For example

import { memoize } from "lodash";

export const getUserPost = memoize(
  async (userId, postId) => {
    const postRef = firestore
      .collection("posts")
      .doc(userId)
      .collection("userPosts")
      .doc(postId);

    const postDoc = await postRef.get();
   
    ...

    return parseUserPost(postDoc);
  },
  (userId, postId) => `[${userId},${postId}]`
);

If I call getUserPost("raul", "postId") I will always be getting cached result if they are in cache...

Is it possible to use the memoize method with some kind of argument to get from the server instead?

Something like

getUserPost("raul", "postId", { cached: false });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

By default _.memoize() uses the 1st parameter (userId in your case) as the key in in the memoization cache, or it uses a resolver, like the on you supplied:

(userId, postId) =>`[${userId},${postId}]`

You can delete that key from the cache, when you don't want to a specific value to return the same result:

Example:

const fn = _.memoize(x => x + Math.ceil(Math.random() * 1000))

console.log(fn(1))
console.log(fn(1))
fn.cache.delete(1) // remove a paremter from the cache
console.log(fn(1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda