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 GCconst minWritablePortion = 0.2; // What portion of the writable bin should be reserved for new datalet cache = new MemCacheLRUBinning(
maxBinSize, // defaults to 50MiB
minWritablePortion // defaults to 0.2
);
cache.set("key", Buffer.from("value"));
cache.get("key").toString(); //=> "value"
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
, andfrozenBin
. Each is aMegaHash
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:
frozenBin
is scheduled to be cleared in the nextTick (unless it is garbage collected by node before then)writableBin
becomes thefrozenBin
protectedBin
becomes thewritableBin
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"