medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

can't figure out why cache is being hit #94

Closed SeeThruHead closed 6 years ago

SeeThruHead commented 6 years ago
const wrapApiCall = questionId => {
  console.log(`actual api call number = ${questionId}`);
  return get(`/questions/${questionId}`);
};

const questionById = memoize(R.pipeP(
  wrapApiCall,
  R.path(['data', 'data', 'attributes']),
  item => ({
    label: item.subject,
    value: `question::${item.id}`,
    id: item.id,
    category: 'Challenge Questions',
    type: 'question'
  })
));

const wrapped = input => {
  console.log(`called with ${input}`);
  return questionById(input);
};

in my local environment:

called with 71
actual api call number = 71
called with 74
actual api call number = 74
called with 92
actual api call number = 92

NODE_ENV=production (on qa server)

called with 30
actual api call number = 30
called with 2449
called with 34
called with 3986

I wouldn't ask this if i have a clue what was happening, hoping you might have an idea

SeeThruHead commented 6 years ago

is there some way to show what's in the cache on every execution? and what memoizee is using to index the cache? a debug mode perhaps?

medikoo commented 6 years ago

@SeeThruHead what you shown doesn't expose all the details. Some questions:

I'm gonna close this as it's not a bug report or feature request. Still feel free to continue discussion below

SeeThruHead commented 6 years ago

thank you for taking the time to help, the function length resulting is 1 and the argument for each call is typeof string

medikoo commented 6 years ago

In such case cache hit will only occur if function is called with string it was already called with. There's on other possibility.

If you feel it's otherwise, increase logging (ensure that argument is indeed string) and ensure that indeed function that's put to memoizee has length 1

SeeThruHead commented 6 years ago

thank you very much, this very incredibly helpful to track down the offending cause.