Options
All
  • Public
  • Public/Protected
  • All
Menu

Class LRUBinningCache

An in-memory cache designed to mimick LRU caching without a doubly linked list. Uses bins to protect recent writes from deletion while clearing less frequently used keys in batches.

The first priority of this cache method is to ensure memory usage stays below a configurable maximum.

The cache contains three bins, a protectedBin, writableBin, and frozenBin. Each is a MegaHash which stores large blobs outside of V8's heap.

When writing to the cache, we first check to make sure there is room (maxBinSize). If there is not enough space, we rotate the bins before setting the value.

When reading the cache, we first look for the value in the protected bin. If the key is in the protected bin, return the value, otherwise check the writable bin. If the key is in the writable bin, return the value, otherwise check the frozen bin. If the key is in the frozen bin, move the value to the protected bin and return the value Otherwise, returns undefined.

Rotates are O(1), clearing is O(n) and scheduled for the nextTick after rotation. During a rotate:

  • the frozenBin is scheduled to be cleared in the nextTick (unless it is garbage collected by node before then)
  • the writableBin becomes the frozenBin
  • the protectedBin becomes the writableBin
example
import { MemCacheLRUBinning, toMebibytes } from "mega-cache";

const maxBinSize = toMebibytes(256); // The cache can store upto 2 times this amount, and potentially peak at 3 times prior to GC
const minWritablePortion = 0.2; // What portion of the writable bin should be reserved for new data

let cache = new MemCacheLRUBinning(
  maxBinSize, // defaults to 50MiB
  minWritablePortion // defaults to 0.2
);
cache.set("key", Buffer.from("value"));
cache.get("key").toString(); //=> "value"

Hierarchy

Implements

Index

Constructors

constructor

  • new LRUBinningCache(maxBinSize: number, maxBinEntries: number, minWritablePortion?: number, newCache: () => Cache): LRUBinningCache
  • Parameters

    • maxBinSize: number

      The maximum size, in bytes, for each bin. The cache will store up to thrice this amount of data

    • maxBinEntries: number
    • Default value minWritablePortion: number = 0.2
    • newCache: () => Cache

    Returns LRUBinningCache

Properties

cacheType

cacheType: "async" = "async"

Optional coldBin

coldBin: Cache

maxBinEntries

maxBinEntries: number

maxBinSize

maxBinSize: number

minWritablePortion

minWritablePortion: number

newCache

newCache: () => Cache

Type declaration

protectedBin

protectedBin: Cache

writableBin

writableBin: Cache

Accessors

count

  • get count(): number
  • Returns number

dataSize

  • get dataSize(): number
  • Returns number

maxProtectedBinSize

  • get maxProtectedBinSize(): number
  • Returns number

Methods

close

  • close(): void

delete

  • delete(key: string | Buffer): Promise<void>
  • Parameters

    • key: string | Buffer

    Returns Promise<void>

get

  • get(key: Buffer | string): Promise<Buffer | undefined>
  • Parameters

    • key: Buffer | string

    Returns Promise<Buffer | undefined>

rotate

  • rotate(): void
  • Returns void

set

  • set(key: string, value: Buffer): Promise<void>
  • Parameters

    • key: string
    • value: Buffer

    Returns Promise<void>

Generated using TypeDoc