npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

disk-memoizer

v4.1.0

Published

Simple disk memoization and in memory LRU cache for high latency IO responses

Downloads

64

Readme

disk-memoizer

Simple disk memoization and in memory LRU cache for speeding up frequently accessed high latency IO resources.

Queues up concurrent requests for the same resource before it has been cached to avoid fetching it multiple times in parallel.

Build Status Dependency Status

Installation

As an npm module

$ npm install disk-memoizer

Usage

const diskMemoizer = require("disk-memoizer");

function fn(data, callback) {
  setTimeout(() => {
    callback(null, data);
  }, 2000);
}

const memoizedFn = diskMemoizer(fn, [options]);

console.time("first");
memoizedFn("foo", () => {
  console.timeEnd("first");

  console.time("second");
  memoizedFn("foo", () => {
    console.timeEnd("second");
  });
});

Options

None of the following options are required:

{
  // Number of milliseconds before considering the cache stale
  // By default the cache won't expire
  maxAge,

  // Content type, right now only "json" is supported, for other types
  // use a custom marshaller (see bellow)
  type,

  // Optional marshaller object with a marshall and an unmarshall
  // asynchronous function that can prepare data before saving it on the
  // disk and after reading it back.
  //
  // Unmarshalled response references will be kept in memory when
  // memoryCacheItems > 0.
  //
  // Example JSON marshaller (simplified without error handing):
  // {
  //    marshall: (data, callback) => callback(null, JSON.stringify(data)),
  //    unmarshall: (data, callback) => callback(null, JSON.parse(data))
  //  }
  //
  marshaller,

  // By default the first argument of the method to be memoized will be used
  // as the cache key, you can provide a custom synchronous function that
  // will receive the arguments of the original function and can return
  // a unique string as the identifier for the cache key.
  identity,

  // Where to store the cache? Defaults to the value set via the
  // environment variable DISK_MEMOIZER_CACHE_DIR
  cacheDir,

  // Number of elements to keep on the lru in memory cache. Keep in mind
  // that each worker on a cluster will keep it's own copy.
  // Defaults to 0 or the environment variable
  // DISK_MEMOIZER_MEMORY_CACHE_ITEMS
  memoryCacheItems,

  // lru-cache options
  lruCacheOptions = {
    max: memoryCacheItems,
    maxAge
  }
}

Environment variables

The disk-memoizer module will make use of the following defaults if set as environment variables.

| Environment Variable | Default value | Description | |---|---|---| | DISK_MEMOIZER_MEMORY_CACHE_ITEMS | 0 | How many items should be kept in memory. This uses the lru-cache module under the hood | | DISK_MEMOIZER_CACHE_DIR | $TMPDIR/disk-memoizer | Directory where the cache will be stored. | | DISK_MEMOIZER_FLUSH_CACHE | false | Forces re-caching items when set to true. | | DISK_MEMOIZER_GC | true | Disables memoization garbage collection when set to false. Garbage collection will not take place on cluster workers, so you'll have to require disk-memoizer on a master process. | | DISK_MEMOIZER_GC_INTERVAL | 300000 (5 minutes) | Seconds to wait between running the garbage collector. | | DISK_MEMOIZER_GC_LAST_ACCESS | 1h | When removing old files only those that have not been accessed for the specified time will be removed. | | DISK_MEMOIZER_LOCK_STALE_MS | 5000 | Milliseconds for the cache lock to be considerer stale. |

Garbage collection

Memoized function contain a .gc method that will trigger garbage collection for the selected cacheDir and maxAge.

WARNING: gc will only work on the master node of a cluster, not in it's workers. It's recommended that you run gc on a separated process.

const memoizedFn = diskMemoizer(fn, [options]);
const gcInterval = memoizedFn.gc({
  // Optional time in seconds between gc runs.
  // Default set via the environment variable DISK_MEMOIZER_GC_INTERVAL
  interval: 300000
});

// clear the gcInterval by calling clearInterval(gcInterval)

Running tests

npm run test

License

(The MIT License)

Copyright (c) 2019 Astound Commerce, Inc, Bermi Ferrer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.