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

@gorgonjs/gorgon

v1.5.1

Published

A simple caching library for async functions

Downloads

1,164

Readme

Gorgon

coverage size version license

A typescript async based caching library for node or the browser.

Pass in a cache key, an async function and optionally a cache length and get back a cached object.

Installation

npm install @gorgonjs/gorgon

yarn add @gorgonjs/gorgon

pnpm add @gorgonjs/gorgon

Usage

See http://gorgonjs.dev for full documentation

API

get = function(key, asyncFunction, policy)

The function will lookup and resolve the value for the previously resolved promise, if no entry is in the cache the function will resolve the promise and resolve that.

Functions that throw errors are not cached. or if they return undefined.

The policy value will set the duration of the cache, if no policy is set the object will be cache until cleared manually.

overwrite = function(key, asyncFunction, policy)

The function will resolve the value for the asnc function and store that result in place of the current object. The original object will be available while the function resolves.

Functions that throw errors are not cached. or if they return undefined.

The policy value will set the duration of the cache, if no policy is set the object will be cache until cleared manually.

put = function(key, value, policy)

Bypass the get function and store an object directly into the cache.

clear = function(key, provider)

Clear a cached item, if no key is set all items will be cleared. Returns a promise that will resolve to true if successful, or an array of booleans for each key; if provider is not specified it will clear the default provider only.

You may also clear cache items using a wildcard characters e.g. Gorgon.clear('sample*')

settings = function(newSettings)

Send in an updated settings object:

  • debug: will output logging
  • retry: will allow for the concurrency queue to be bypassed after this interval, default: 5000

Policies

Simple Policy

The simplest policy is to simply set a duration, pass in any integer and the object will be cached for that many miliseconds.

Gorgon.get('sample', resolver, 1000).then(res => { console.log(res); });

Date Policy

If you have a specific date and time you would like a cache item to expire, you can pass in a date object

var midnight = new Date();
midnight.setHours(24,0,0,0); // midnight
Gorgon.get('sample', resolver, midnight).then(res => { console.log(res); });

Complex Policy

If you have something more complex you would like to do with the policy, you can pass in an object with your specifications.

Properties

  • expiry: Date or amount of milliseconds you would like the cache to expire (required but may be set to false)
  • provider: Specify the provider to use (default: 'memory')

Example

Gorgon.get('sample', resolver, {
  expiry: 1000,
  provider: 'memory',
}).then(res => { console.log(res); });

Alternate Storage Engines

var Gorgon = require('@gorgonjs/gorgon');
var storageCache = require('@gorgonjs/gorgon/storageObjectProvider');
storageCache.setStorage(window.sessionStorage);

Gorgon.addProvider('session', storageCache);

More

Concurrency

If you request 2 calls at the same time with the same key, the resolver will only resolve once no matter how long the resolver takes. Making a slow API call will only call the API once even if you request the information more then once in a short period. This can be used to help reduce trips to external systems.