mistval / node-fetch-cache

Node-fetch with built-in response caching.
MIT License
55 stars 9 forks source link

memory cache - default shared memory cache vs custom memory cache #52

Closed campbellmc closed 5 months ago

campbellmc commented 5 months ago

Hi. Was messing around with some timing checks and see different results for subsequent fetches with default shared memory cache (caching is obvious) vs custom memory cache (doesn't appear to cache).

Am I missing something?

Here's the output of my messing around...

With default memory cache it says it's cached (returnedFromCache is true, fetch time is much shorter):

import fetch from "node-fetch-cache"; yields the following:

Cache in Memory, Default Cache
Status: 200 (OK)
isCacheMiss: false
returnedFromCache: false
Fetch Time: 84.95ms
Status: 200 (OK)
isCacheMiss: false
returnedFromCache: true
Fetch Time: 0.532ms

Cache in Memory, Default Cache, Cache Control
Status: 200 (OK)
isCacheMiss: false
returnedFromCache: true
Fetch Time: 0.393ms

A Few Moments Later
Status: 200 (OK)
isCacheMiss: false
returnedFromCache: true
Fetch Time: 0.636ms

However, with the Custom Memory Cache, it doesn't appear to actually cache (returnedFromCache is false, fetch time is roughly the same):

  const fetch = NodeFetchCache.create({
    cache: new MemoryCache({ ttl: 100 }),
  });

and we get this result:

Cache in Memory, Custom Cache with TTL
Fetch 1
Status: 200 (OK)
isCacheMiss: false
returnedFromCache: false
Fetch Time: 94.664ms
Fetch 2
Status: 200 (OK)
isCacheMiss: false
returnedFromCache: false
Fetch Time: 86.484ms

Cache in Memory, Custom Cache with TTL, Cache Control
Fetch 1
Status: 504 (Gateway Timeout)
isCacheMiss: true
returnedFromCache: false
Fetch Time: 0.318ms

A Few Moments Later
Fetch 2
Status: 504 (Gateway Timeout)
isCacheMiss: true
returnedFromCache: false
Fetch Time: 1.061ms
campbellmc commented 5 months ago

Here's my quick and dirty test script: https://github.com/dxclabs/node-fetch-cache-test/blob/4992d88ecb2a9a96f7c8c35cc43cee12b90f1fc6/src/cache_times.ts

mistval commented 5 months ago

Hi, it looks like you are constructing a separate MemoryCache for every request. To get the behavior it sounds like you want, you must use the same MemoryCache instance for all requests. The default cache is a globally shared instance of MemoryCache.

campbellmc commented 5 months ago

Duh. Thanks. Yes that changes everything. It wasn't happening for disk or redis because they are persistent and global stores. If I change the script to reuse the cached fetch instance, I see the right results.