graphql / dataloader

DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
MIT License
12.89k stars 515 forks source link

Redis async cacheMap #53

Closed rbpinheiro closed 7 years ago

rbpinheiro commented 8 years ago

I am trying to implement a cacheMap that is async. Meaning the the "get" function needs to inform asynchronously wether the specified key is cached or not. But that is not working.

My code is here: https://gist.github.com/rbpinheiro/942218abe33a6b533d37bbd9e70e129c

It seems that returning a promise on "get" is the same as stating a cache hit. If you uncomment line 9, that returns false, it will always assume a cache miss. How to implement the cache miss asynchronously?

I investigated dataloader's code and found this on line 87:

var cachedPromise = this._promiseCache.get(cacheKey);
if (cachedPromise) {
  return cachedPromise;
}

That "if" seems to be the only check for cache miss, which doesn't cover the asynchronous case.

cristianocd commented 8 years ago

24 https://github.com/facebook/dataloader/issues/24#issuecomment-205618642

leebyron commented 7 years ago

DataLoader is a local in-memory cache, sort of like your browser's cache. Redis is a backing store which should be loaded from, see the example in Readme https://github.com/facebook/dataloader#common-back-ends

B-3PO commented 7 years ago

Here is a implementation that adds a Redis cache layer in place of the in memory cache. https://www.npmjs.com/package/redis-cache-dataloader