Coder Social home page Coder Social logo

Custom cache examples about dataloader HOT 5 CLOSED

graphql avatar graphql commented on May 3, 2024
Custom cache examples

from dataloader.

Comments (5)

lukaswelte avatar lukaswelte commented on May 3, 2024 2

The tests offer an example usage:

  describe('Accepts custom cacheMap instance', () => {

    class SimpleMap {
      stash: Object;

      constructor() {
        this.stash = {};
      }
      get(key) {
        return this.stash[key];
      }
      set(key, value) {
        this.stash[key] = value;
      }
      delete(key) {
        delete this.stash[key];
      }
      clear() {
        this.stash = {};
      }
    }

    it('Accepts a custom cache map implementation', async () => {
      var aCustomMap = new SimpleMap();
      var identityLoadCalls = [];
      var identityLoader = new DataLoader(keys => {
        identityLoadCalls.push(keys);
        return Promise.resolve(keys);
      }, { cacheMap: aCustomMap });

      // Fetches as expected

      var [ valueA, valueB1 ] = await Promise.all([
        identityLoader.load('a'),
        identityLoader.load('b'),
      ]);

      expect(valueA).to.equal('a');
      expect(valueB1).to.equal('b');

      expect(identityLoadCalls).to.deep.equal([ [ 'a', 'b' ] ]);
      expect(Object.keys(aCustomMap.stash)).to.deep.equal([ 'a', 'b' ]);

      var [ valueC, valueB2 ] = await Promise.all([
        identityLoader.load('c'),
        identityLoader.load('b'),
      ]);

      expect(valueC).to.equal('c');
      expect(valueB2).to.equal('b');

      expect(identityLoadCalls).to.deep.equal([ [ 'a', 'b' ], [ 'c' ] ]);
      expect(Object.keys(aCustomMap.stash)).to.deep.equal([ 'a', 'b', 'c' ]);

      // Supports clear

      identityLoader.clear('b');
      var valueB3 = await identityLoader.load('b');

      expect(valueB3).to.equal('b');
      expect(identityLoadCalls).to.deep.equal(
        [ [ 'a', 'b' ], [ 'c' ], [ 'b' ] ]
      );
      expect(Object.keys(aCustomMap.stash)).to.deep.equal([ 'a', 'c', 'b' ]);

      // Supports clear all

      identityLoader.clearAll();

      expect(Object.keys(aCustomMap.stash)).to.deep.equal([]);
    });

  });

});

The example in it is mainly:

  describe('Accepts custom cacheMap instance', () => {

    class SimpleMap {
      stash: Object;

      constructor() {
        this.stash = {};
      }
      get(key) {
        return this.stash[key];
      }
      set(key, value) {
        this.stash[key] = value;
      }
      delete(key) {
        delete this.stash[key];
      }
      clear() {
        this.stash = {};
      }
    }

    it('Accepts a custom cache map implementation', async () => {
      var aCustomMap = new SimpleMap();
      var identityLoadCalls = [];
      var identityLoader = new DataLoader(keys => {
        identityLoadCalls.push(keys);
        return Promise.resolve(keys);
      }, { cacheMap: aCustomMap });

So creating a class that implements get set delete and clear is sufficient that you can pass it into the dataloader as cacheMap. So you can create e.g. a class that uses redis in these methods to store or retrieve the value and you are good to go.

const dataloader = new DataLoader(keys => {
 // the loader code
}, {cacheMap: myRedisObject});

from dataloader.

tonyxiao avatar tonyxiao commented on May 3, 2024

Got it, thanks for the reference. What about async cache sources, powered by something like Redis? Is that supported by data loader?

from dataloader.

lukaswelte avatar lukaswelte commented on May 3, 2024

Looking at the source code I'd say yes, but maybe just do a little test.

from dataloader.

tonyxiao avatar tonyxiao commented on May 3, 2024

Got it, thanks for the hint. Will close this for now then :)

from dataloader.

B-3PO avatar B-3PO commented on May 3, 2024

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

from dataloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.