Git Product home page Git Product logo

cache's Introduction

@feathers-plus/cache

Dependency Status Download Status

LRU (least recently used) cache for @feathers-plus/batch-loader and @feathers-plus/feathers-hooks-common cache hook.

Installation

npm install @feathers-plus/cache --save

Documentation

@feathers-plus/batch-loader, by default, uses the standard Map which simply grows until the BatchLoader is released. The default is appropriate when requests to your application are short-lived.

Longer lived BatchLoaders, such as ones which persist between @feathers-plus/feathers-hooks-common fastJoin hook 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 BatchLoader-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 BatchLoader = require('@feathers-plus/batch-loader');
const feathersCache = require('@feathers-plus/cache');

const cacheMap = feathersCache({ max: 3 }); // see options in isaacs/node-lru-cache
const batchLoader = new BatchLoader(batchLoadFn, { cacheMap });

Promise.all(
  ['a', 'b', 'c', 'd', 'e'].map(key => batchLoader.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
batchLoader.clear('d');
console.log(cacheMap.keys().sort()); // ['c', 'e']

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

Note

lib/hooks contains hooks to clear the keys of mutated records.

License

Copyright (c) 2017

Licensed under the MIT license.

cache's People

Contributors

eddyystop avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cache's Issues

Cache should be busted based on events (and not just methods)

The cache is being busted when mutating methods are called, which is fine when you are executing the code in single thread.

What if, the mutations to the data are happening from another thread or another process (e.g. the same service running in another server, behind load-balancer)?

I believe, the cache should subscribe to the events and be busted based on 'mutation events' emitted from across all similar instances running on the same service.

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.