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

cachify-wrapper

v6.0.18

Published

Wraps a function with a caching layer

Downloads

617

Readme

cachify-wrapper

Wraps a function with a caching layer

Usage

callback

Wraps a function with a caching layer

Parameters

  • fn function callback-last style function
  • storage Storage<K, RecordPacked<V>> cache storage (optional, default InMemoryStorageCb)
  • options Options (optional, default {})
  • hasher Function creates key for KV-storage from fn arguments (optional, default JSON.stringify)

Examples

const wrapper = require('cachify-wrapper').default;
class Storage {
	constructor() {
		this.data = new Map();
	}
	get = (key, cb) => cb(null, this.data.get(key))
	set = (key, value, ttl, cb) => {
		this.data.set(key, value);
		if (ttl > 0) setTimeout(() => this.data.delete(key), ttl);
		cb(null, true);
	}
	del = (key, cb) => cb(null, this.data.delete(key))
}
const storage = new Storage();
let count = 0;
const inc = (a, cb) => cb(null, count += a);
const cached = wrapper(inc, storage, {expire: 100});
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 0); // Invokes request
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 100); // Takes cached result
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 200); // Invokes second request

cached.set(2, 'manual value', 1000, () =>
	cached.get(2, (_, result) => {
		console.info(result);
		cached.del(2, () =>
			cached.get(2, (_, result) => console.info(result)))
	}));

Returns function

promise

Wraps a function with a caching layer

Parameters

Examples

const wrapperPromise = require('cachify-wrapper').promise;
let count = 0;
const inc = async(a) => count += a;
const cached = wrapperPromise(inc, storage, {expire: 1000});
const p1 = cached(1).then((payload) => console.info(payload)); // Invokes request
const p2 = p1.then(() => cached(1).then((payload) => console.info(payload))); // Takes cached result

Returns function (...any): Promise<any>

Options

Type: Object

Properties

  • storage Object?

    • storage.timeout number? max storage response time before considering it as failed, and invoking fn
  • source Object?

    • source.timeout number? max fn response time before considering it as failed
  • expire number? time to consider cached data expired [in milliseconds]

  • spread number? expire time spread (prevents simultaneous deletions saved items from storage)

  • lock number? lock timeout (prevents simultaneous concurrent invoke of fn at initial period)

  • stale number? additional ttl for stale data

  • ttl number? forced ttl (TimeToLive) for data (useful if storage is using from multiply services with different expire)

  • retries number? number of storage requests passes before fn call

  • error number? ttl for erroneous state cache (prevents frequent call of fn)

  • verbose number? verbosity flag

CacheAbsentError

Extends Error

no cache error

Functions

Storage

storage interface

get

Parameters
  • key K
  • cb CB<V>

set

Parameters

del

Parameters

RecordPacked

Type: Object

Properties

InMemoryStorageCb

get

Parameters
  • key K
  • cb CB<V>

set

Parameters

del

Parameters

InMemoryStorageSerializable

Extends InMemoryStorage

expire

Parameters

export

Returns Iterable<KRecordTuple<K, V>>

import

Parameters
  • dump Iterable<KRecordTuple<K, V>>

InMemoryStorageRecord

Type: Object

Properties

InMemoryStorage

Parameters

  • source Iterable<KRecordTuple<K, V>>?

get

Parameters
  • key K

set

Parameters
  • key K
  • value V

del

Parameters
  • key K

has

Parameters
  • key K

expire

Parameters