A simple caching library for async functions
npm install @gorgonjs/gorgonA 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.
npm install @gorgonjs/gorgon
yarn add @gorgonjs/gorgon
pnpm add @gorgonjs/gorgon
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.
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.
Bypass the get function and store an object directly into the cache.
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*')_
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_
The simplest policy is to simply set a duration, pass in any integer and the object will be cached for that many miliseconds.
``javascript`
Gorgon.get('sample', resolver, 1000).then(res => { console.log(res); });
If you have a specific date and time you would like a cache item to expire, you can pass in a date object
`javascript`
var midnight = new Date();
midnight.setHours(24,0,0,0); // midnight
Gorgon.get('sample', resolver, midnight).then(res => { console.log(res); });
#### 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
`javascript`
Gorgon.get('sample', resolver, {
expiry: 1000,
provider: 'memory',
}).then(res => { console.log(res); });
`javascript
var Gorgon = require('@gorgonjs/gorgon');
var storageCache = require('@gorgonjs/gorgon/storageObjectProvider');
storageCache.setStorage(window.sessionStorage);
Gorgon.addProvider('session', storageCache);
``