1.0.0 • Published 6 years ago

tlru-cache v1.0.0

Weekly downloads
2
License
ISC
Repository
-
Last release
6 years ago

TLRU-Cache

Time aware least recently used cache store

Installation

npm i tlru-cache

Usage

import { TLRUCache } from 'tlru-cache';

const tlruCache =  new TLRUCache({
  // optional values
  maxStoreSize: 5,
  timeToUseMs: 60 * 1000,
});

const value = tlruCache.get('value', () => 42);
// won't calculate again
const sameValue = tlruCache.get('value', () => 42);

Documentation

Constructor object properties:

NameTypeDefault valueDescription
maxStoreSizenumber1000Store capacity
maxAgeMsnumber60 60 1000 (1 hour)Record time to use in milliseconds. Put <= 0 for non-expiring records.

Methods:

get<TKey, TValue>(key: TKey, calculate: (key: TKey) => TValue, maxAgeMs: number = null): TValue

Arguments:

  • key: record key

  • calculate: function to get record. receive key as argument

  • maxAgeMs: record time to use. if presented, override constructor option