Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dataloader-cache-lru

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dataloader-cache-lru

LRU (least recently used) cache for Facebook's DataLoader.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

dataloader-cache-lru

Dependency Status Download Status

LRU (least recently used) cache for Facebook's DataLoader.

Installation

npm install dataloader-cache-lru --save

Documentation

DataLoader, by default, uses the standard Map which simply grows until the DataLoader is released. The default is appropriate when requests to your application are short-lived.

Longer lived DataLoaders, such as ones which persist between GraphQL queries, will build up memory pressure unless the size of the cache is controlled. Furthermore, if records mutate, their cache entries must be removed.

This DataLoader-compatible cache implements a LRU (least recently used) cache, it deletes the least recently used items when the cache size is exceeded.

Complete Example

const DataLoader = require('dataloader');
const dataLoaderCacheLru = require('dataloader-cache-lru');

const cacheMap = dataLoaderCacheLru({ max: 3 }); // see options in isaacs/node-lru-cache
const dataloader = new DataLoader(batchLoadFn, { cacheMap });

Promise.all(
  ['a', 'b', 'c', 'd', 'e'].map(key => dataloader.load(key))
)
  .then(data => {
    console.log(data); // [{ key: 'a', data: 'a' }, ..., { key: 'e', data: 'e' }]
    console.log(cacheMap.keys().sort()); // ['c', 'd', 'e']
  });

// record with key 'd' has been mutated
dataloader.clear('d');
console.log(cacheMap.keys().sort()); // ['c', 'e']

function batchLoadFn (keys) {
  return Promise.all(
    keys.map(key => ({ key, data: key }))
  );
}

License

Copyright (c) 2017

Licensed under the MIT license.

Keywords

FAQs

Package last updated on 02 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc