GithubHelp home page GithubHelp logo

digitalcredentials / lru-memoize Goto Github PK

View Code? Open in Web Editor NEW

This project forked from digitalbazaar/lru-memoize

0.0 2.0 0.0 80 KB

License: BSD 3-Clause "New" or "Revised" License

JavaScript 97.54% Shell 2.46%

lru-memoize's Introduction

LRU-Memoize (@digitalcredentials/lru-memoize)

Node.js CI NPM Version

A Memoized wrapper around the javascript lru-cache library.

Table of Contents

Background

(Forked from digitalbazaar/lru-memoize v2.0.0 to provide TypeScript and ReactNative compatibility.)

lru-memoize is used to to memoize promises (as opposed to just the results of the operations), which helps in high-concurrency use cases. (And in turn, it uses lru-cache under the hood.)

Install

To install locally (for development):

git clone https://github.com/digitalcredentials/lru-memoize.git
cd lru-memoize
npm install

Usage

To import:

import { LruCache } from '@digitalcredentials/lru-memoize'
// or
const { LruCache } = require('@digitalcredentials/lru-memoize')

The memoized LruCache constructor passes any options given to it through to the lru-cache constructor, so see that repo for the full list of cache management options. Commonly used ones include:

  • max (default: 100) - maximum size of the cache.
  • maxAge (default: 5 sec/5000 ms) - maximum age of an item in ms.
  • updateAgeOnGet (default: false) - When using time-expiring entries with maxAge, setting this to true will make each entry's effective time update to the current time whenever it is retrieved from cache, thereby extending the expiration date of the entry.

This library is useful for caching (in a deterministic memoized fashion) expensive or long-running functions, such as API requests, database lookups, and so on.

For example, say you have a function fetchStatus() that retrieves a result from a web API (here, simulated with a delay() wait). To cache the result of this function:

import { LruCache } from '@digitalcredentials/lru-memoize'

// Cache expiration/TTL: 5 seconds
const myCache = new LruCache({ maxAge: 5000 })

async function fetchStatus() {
  // simulate an async task
  await delay(100);
  executedTestFn = true;
  return {success: true, timestamp: Date.now()};
}

// Load the cached result if it's present, otherwise, perform the operation
const result = await myCache.memoize({
  key: 'myApiResults',
  fn: fetchStatus
});

// You can also memoize a particular call to a function, using anonymous arrow functions:
const url = 'https://api.example'
const result = await myCache.memoize({
  key: 'myResults',
  fn: async () => fetchMyResultsFromWeb({ url })
})

The key param is used to namespace the caches, in case the same LruCache instance is being used to cache different types of operations/functions.

Contribute

PRs accepted.

If editing the Readme, please conform to the standard-readme specification.

License

  • MIT License - DCC - TypeScript compatibility.
  • New BSD License (3-clause) © 2020-2021 Digital Bazaar - Initial implementation.

lru-memoize's People

Contributors

mattcollier avatar dmitrizagidulin avatar msporny avatar gannan08 avatar aljones15 avatar fields37 avatar kimdhamilton avatar

Watchers

James Cloos avatar  avatar

lru-memoize's Issues

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.