Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bimapcache

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bimapcache

Module for memoizing an individual function or a pair of inverse functions to persistent storage

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
increased by275%
Maintainers
1
Weekly downloads
 
Created
Source

BiMapCache

Circle CI npm version

This module is for memoizing to localStorage a single asynchronous function or a pair of inverse asynchronous functions.

API

This module exports the BiMapCache class, which must be instantiated with new and has several public methods.

new BiMapCache({key, getBfromA, getAfromB, storage, saveThrottle, maxLimit, maxAge})

When constructing a BiMapCache instance, you must pass an object with key, getAfromB, and getBfromA properties. The rest of the properties are optional.

key is the name of the property on localStorage (or the object passed as the storage parameter) to use to store the cached data in.

getAfromB and getBfromA are the functions you want to cache. These functions are expected to be inverses of each other, though if you only want to cache one function then you could supply it as getAfromB and supply an error-throwing function as getBfromA. Each function only gets called when the same-named method on the BiMapCache instance is called. Each function must return a Promise if it is to be used. The inputs and (resolved) outputs of these functions must work as the keys of Maps and be JSONifiable, so it's recommended that they be numbers or strings.

saveThrottle is an optional property specifying a number of milliseconds to throttle saves to the storage by. This defaults to 3000 (or 3 seconds).

maxLimit is an optional property specifying the maximum number of entries to keep in the persistent cache. Old entries will be removed first if this limit is exceeded. This defaults to 1000.

maxAge is an optional property specifying the maximum age in milliseconds for cache entries. Cache entries that haven't been set or retrieved within this time will be removed. This defaults to one year.

biMapCache.getAfromB(input): Promise, biMapCache.getBfromA(input): Promise

Calling either of these methods on the instance will cause the cache to be checked first. If no result is found in the cache, then the matching callback given in the constructor will be called. If the promise from the given callback resolves successfully, its result will be cached.

biMapCache.removeAfromCache(input), biMapCache.removeBfromCache(input)

These methods allow a value to be removed from the cache. If the corresponding getAfromB(input) / getBfromA(input) function was previously called and its returned Promise has not yet resolved, then its final result will not be cached.

Types

Both TypeScript and Flow type definitions for this module are included! The type definitions won't require any configuration to use.

Keywords

FAQs

Package last updated on 16 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc