jonathanong / lru-cache-decorator

lru-cache as a decorator function

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LRU Cache Decorator

NPM version Build status Test coverage Dependency Status License Greenkeeper badge Downloads

A decorator that caches functions.

  • Supports both sync and async functions.
  • Supports hashing the function arguments - only necessary if you have really long/large function arguments.

API

const decorate = require('lru-cache-decorator')

fn = decorate(options)(fn)

Options are passed to lru-cache.

Other options:

  • async = false - whether the function is async (i.e. uses promises)
    • Caches functions as sync by default, but async functions would still work
    • Sync functions do not cache errors while async functions do since it's stored as a rejected promise
  • hash = Algorithm<String>|<Function> - a function to hash the stringified function arguments. You can also supply a hashing function algorithm such as sha256
  • cacheErrors = true - when false and async=true, errors are not cached.

About

lru-cache as a decorator function

License:MIT License


Languages

Language:JavaScript 100.0%