npm.io
1.0.0 • Published 3 years ago

@neumatter/object-cache

Licence
Version
1.0.0
Deps
1
Size
5 kB
Vulns
0
Weekly
0

ObjectCache

plot plot JavaScript Style Guide

Module for creating ObjectCaches that can be garbage collected. The ObjectCache will delete the least-recently-used entry to make room for new entries.


Table of Contents


Install

npm i @neumatter/object-cache

Usage

import ObjectCache from '@neumatter/object-cache'

class CustomObject {
  constructor () {}

  get () {
    const objectCache = ObjectCache.from(this, { maxSize })
    let value = objectCache.get('key')

    if (value === undefined) {
      value = someExpensiveOp()
      objectCache.set('key', value)
    }

    return value
  }
}

Keywords