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

map-cache-ttl

v0.1.1

Published

Map cache with expiration and es6 `Map`s

Downloads

363

Readme

map-cache-ttl

Map subclass for use as simple cache with expiration

Usage


const Cache = require('map-cache-ttl');

let cache = new Cache('5s', '1m');
// Will cache for 5seconds by default
// and trim() expired pairs every minute

cache.set('foo', {bar: 'baz'}, '30s');
// will cache in 'foo' key for 30 seconds
cache.has('foo'); // true
//... 30 seconds later
cache.has('foo'); // false


const getRawBody = require('raw-body');
let requests = 0;
const proxy = cache.proxy((url) => new Promise((resolve, reject) => {
	requests += 1;
	http
		.get(url, res => res.statusCode == 200 ?
			resolve(getRawBody(res)) :
			reject(new Error(`${res.statusCode} ${res.statusMessage}`))
		)
		.on('error', reject);
}), '30s');

// requests is 1
proxy('https://github.com')
	.then( github => console.log(github) ) // requests is 1
	.then( () => proxy('https://github.com') ) // requests is 1, cache hit
	.then( () => ... ) // 1 min later
	.then( () => proxy('https://github.com') )
	.then( github => console.log(github) ); // requests is 2, github page fresh

new Cache(max_age, interval)

max_age sets the default max age for keys. If not set or is less then 1ms it will default to Infinity and keys will never expire unless a per-key max_age is passed during cache.set(key, value, max_age) or cache.proxy(fn, max_age).

interval sets a setInterval for cache.trim() to automatically trim expired keys. Default is to set no interval and use cache.trim() manually to free up expired objects.

cache.get(key)

Will return the stored value for the specified key if the expiration time is in the future.

cache.has(key)

Will return true if a value is stored the specified key and it has not expired.

cache.set(key, value, max_age)

Cache a value into a specific key.

The key can be any type other then string as Cache extends Map objects. The value will be cached for max_age ms but it's contents are not protected from modifications in any way. It's the value's user's responsibility to ensure that the values will not be modified if this is desired.

If no max_age is provided or it is less then 1ms it will default to the cache's default to cache.max_age.

cache.proxy(fn, max_age)

Will return a wrapper function that will cache the results of the first function invocation for every set of arguments. This works by hashing arguments using object-hash. The proxy function always returns a Promise

cache.trim()

Clean up expired key/value pairs.

cache.clear()

Clear all key/value pairs.

cache.size

Will return the number of not expired items.

Caution This will also trigger a cache.trim() to calculate the correct size.