1.0.0 • Published 12 months ago

@neumatter/lru-cache v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
12 months ago

LRUCache

JavaScript Style Guide

A simple LRUCache with maxAge and allowStale options.

Table of Contents

Install

npm i @neumatter/lru-cache

Usage

LRUCache:

import LRUCache from '@neumatter/lru-cache'

type LRUCacheOptions = {
  maxSize?: number,
  maxAge?: number,
  allowStale?: boolean,
  sizeCalculation?: (value: any, key: any) => number,
  notFoundReturnValue?: any
}

// All Options set to default
const cache = new LRUCache({
  maxSize: 1e4,
  maxAge: Infinity,
  allowStale: false
  sizeCalculation: (value, key) => 1,
  notFoundReturnValue: undefined
})

LRUCache.get:

// All Options set to default
const value = cache.get('/key', { allowStale: false }) // returns value or cache.notFoundReturnValue

LRUCache.peek:

Same as LRUCache.get but it doesn't change the order of the entries.

// All Options set to default
const value = cache.peek('/key', { allowStale: false }) // returns value or cache.notFoundReturnValue

LRUCache.set:

cache.set('/key', 99)

LRUCache.clear:

cache.clear()

LRUCache.delete:

const isDeleted = cache.delete('/key') // returns boolean