Options
All
  • Public
  • Public/Protected
  • All
Menu

with-cache

with-cache is a simple library that allows you to create with basic composition a function which results will be memoized. You can use cache for any operation from more common like HTTP requests to less like operations that requires heavy computation.

Documentation

Instalation

npm install --save with-cache

Usage

Simplest case:

import { withCache } from 'with-cache';
function heavyOperation(arg1, arg2) {
    //...do something
}
const heavyOperatuionWithCaching = withCache(heavyOperation);

//this call will run heavyOperation function
const result1 = heavyOperatuionWithCaching("pass", "secret");

//this call return previously cached value 
const result2 = heavyOperatuionWithCaching("pass", "secret");

withCache can also accept options as a second argument. See docs

import { withCache } from 'with-cache';

function heavyOperation(arg1, arg2) {
    //...do something
}
const customKeymaker = (arg1, arg2) => arg1 + arg2;

class CustomCache extends Map {}

const heavyOperatuionWithCaching = withCache(heavyOperation, {
    keymaker: customKeymaker,
    ttl: 420,
    cache: new CustomCache()
});

Index

Interfaces

Type aliases

Functions

Type aliases

Cache

Cache<T>: Map<string | number, CacheItem<T>>

Type parameters

  • T

    Cached value type

Functions

withCache

  • withCache<Args, ResultType>(fn: (...args: Args) => ResultType, options?: CacheOptions<Args, ResultType>): CacheResolver<Args, ResultType>
  • Factory function creating cache resolver for memoized operations

    Type parameters

    • Args: unknown[]

      Arguments array infered from passed function to memoize

    • ResultType: unknown

      Value that is returned, infered from passed function

    Parameters

    • fn: (...args: Args) => ResultType
        • (...args: Args): ResultType
        • Parameters

          • Rest ...args: Args

          Returns ResultType

    • Default value options: CacheOptions<Args, ResultType> = {}

    Returns CacheResolver<Args, ResultType>