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

stale-lru-cache-cluster

v1.0.3

Published

A promise-based Stale LRU Cache with support for clusters.

Downloads

7

Readme

Stale LRU Cache

A promise-based Stale LRU Cache with support for cluster.

Features

  • A promise-based API.
  • Support for cluster.
  • Wrapper for stale-lru-cache module.

Installation

npm install --save stale-lru-cache-cluster

Usage

const LRUCache = require('stale-lru-cache-cluster');

const options = {
  maxSize: 100,
  maxAge: 600,
  staleWhileRevalidate: 86400,
  revalidate: function (key, callback) {
    fetchSomeAsyncData(callback);
  },
};

const cache = LRUCache(options);

cache.set('key', 'value');

cache.get('key').then((value) => {
  // 'value'
});

Usage with cluster

  1. Require stale-lru-cache-cluster in master process
require('stale-lru-cache-cluster');
  1. In worker processes, require stale-lru-cache-cluster and use as follows:
const LRUCache = require('stale-lru-cache-cluster');

const options = {
  maxSize: 100,
  maxAge: 600,
  staleWhileRevalidate: 86400,
  revalidate: function (key, callback) {
    fetchSomeAsyncData(callback);
  },
};

const cache = LRUCache(options);

cache.set('key', 'value');

cache.get('key').then((value) => {
  // 'value'
});

API

Cache(options)

Creates and returns a new Cache instance.

Parameters
  • options.maxAge - Time in seconds after which items will expire. (default: Infinity)
  • options.staleWhileRevalidate - Time in seconds, after maxAge has expired, when items are marked as stale but still usable. (default: 0)
  • options.revalidate(key, callback(error, value, [options])) - Function that refreshes items in the background after they become stale.
  • options.maxSize - Maximum cache size. (default: Infinity)
  • options.getSize(value, key) - Function used to calculate the length of each stored item. (default: 1)

delete(key)

Removes the specified item from the cache.

Returns true if an item existed and has been removed, or false if the item does not exist.

cache.delete('key').then((removed) => {
  // true or false
});

get(key)

Returns the value associated with the specified key, or undefined if the item does not exist.

cache.get('key').then((value) => {
  // 'value' or undefiend
});

has(key)

Returns true if an item with the specified key exists (may be fresh or stale), or false otherwise.

cache.has('key').then((exists) => {
  // true or false
});

isStale(key)

Returns true if an item with the specified key exists and is stale, or false otherwise.

cache.isStale('key').then((isStale) => {
  // true or false
});

keys()

Returns an array with all keys stored in the cache.

cache.keys().then((keys) => {
  // Array with all keys
});

set(key, value, [options])

Inserts a new item with the specified key and value.

Returns true if the item has been inserted, or false otherwise.

cache.set('key', 'value', options).then((inserted) => {
  // true or false
});
Parameters
  • key - Required. The key of the item to be inserted. (both objects and primitives may be used)
  • value - Required. The value of the item to be inserted. (both objects and primitives may be used)
  • options - Item specific Cache-Control string or options object.
  • options.maxAge - Time in seconds after which the item will expire.
  • options.staleWhileRevalidate - Time in seconds, after maxAge has expired, when the item is marked as stale but still usable.
  • options.revalidate(key, callback(error, value, [options])) - Function that refreshes the item in the background after it becomes stale.
Examples
cache.set('key', 'value'); // true

cache.set('key', 'value', { maxAge: 600, staleWhileRevalidate: 86400 }); // true
cache.set('key', 'value', { maxAge: 0 }); // false

cache.set('key', 'value', 'max-age=600, stale-while-revalidate=86400'); // true
cache.set('key', 'value', 'max-age=0'); // false
cache.set('key', 'value', 'no-cache, no-store, must-revalidate'); // false
Cache-Control Behaviour
  • max-age=600, must-revalidate - Will be cached for 10 minutes and removed afterwards
  • max-age=600, stale-while-revalidate=86400 - Will be cached for 10 minutes and then refreshed in the background if the item is accessed again within a time window of 1 day
  • max-age=0 - Will not be cached
  • no-cache, no-store, must-revalidate - Will not be cached
  • private - Will not be cached
  • public - Will be cached using default maxAge and staleWhileRevalidate options

size

Property indicating the size of all stored items in the cache. This is calculated using getSize options function.

cache.getSize().then((size) => {
  // size of all stored items in the cache
});

values()

Returns an array with all values stored in the cache.

cache.values().then((values) => {
  // values stored in the cache
});

With :heart: by

License

MIT license. Copyright © 2016.