New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

litter-box

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

litter-box

A modified catbox

  • 2.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by66.67%
Maintainers
1
Weekly downloads
 
Created
Source

litter-box

A few function memoization helpers to work with catbox.

standard travis npm

Promise Memoization

litterBox.memoizeFnPromise(options)

Arguments
  • options: object. Required. An object with the following keys:
    • client: Catbox Client Instance. Required. A catbox client instance.
    • fn: Function. Required. A function which returns a Promise.
    • ttl: Integer. Required. The time-to-live for the cached fn result.
    • keyProvider: (fn-input) => {id, segment}. Required. A function which returns a cache-key for Catbox. This function is called with the same arguments as fn, allowing you to create a dynamic cache-key, for example:
  const exampleKeyProvider = (input) => ({ segment: 'test', id: `test-${input}` })
Promise Memoization Example:

This code is also available here.

const litterBox = require('litter-box')

const examplePromiseFunction = (input) => Promise.resolve(input)

const cachedPromiseFunction = litterBox.memoizeFnPromise({
  client: catboxClientInstance,
  fn: examplePromiseFunction,
  keyProvider: (input) => ({ segment: 'test', id: `test-${input}` }),
  ttl: 5 * 60 * 1000 // 5 minutes
})

cachedPromiseFunction(1234)
  .then(console.log) // prints 1234

// later on...
cachedPromiseFunction(1234) // function not executed, value is pulled from the cache
  .then((result) => { // returns 1234
    console.log(result)
    process.exit(0)
  })

Callback Memoization

litterBox.memoizeFnCallback(options)

Arguments
  • options: object. Required. An object with the following keys:
    • client: Catbox Client Instance. Required. A catbox client instance.
    • fn: Function. Required. A function which has a callback as it's final argument.
    • ttl: Integer. Required. The time-to-live for the cached fn result.
    • keyProvider: (fn-input) => {id, segment}. Required. A function which returns a cache-key for Catbox. This function is called with the same arguments as fn, allowing you to create a dynamic cache-key, for example:
  const exampleKeyProvider = (input) => ({ segment: 'test', id: `test-${input}` })
Callback Memoization Example:

This code is also available here.

const litterBox = require('litter-box')

const exampleCallbackFunction = (input, cb) => cb(null, input)

const cachedCallbackFunction = litterBox.memoizeFnPromise({
  client: catboxClientInstance,
  fn: exampleCallbackFunction,
  keyProvider: (input) => ({ segment: 'test', id: `test-${input}` }),
  ttl: 5 * 60 * 1000 // 5 minutes
})

cachedCallbackFunction(1234, (err, result) => console.log(result)) // prints 1234

// later on...

// function not executed, value is pulled from the cache
cachedCallbackFunction(1234, (err, result) => console.log(result)) // prints 1234

l

Keywords

FAQs

Package last updated on 08 Apr 2017

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