Skip to main content
Deno 2 is finally here 🎉️
Learn more

cache-mapset

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

Maps and Sets with cache replacement policies, TC39 proposal-policy-map-set implementation.

This can be used as a cache for TC39 proposal-function-memo. See memo for its implementation.

Common

List items common to all implementations.

Interface

MapLike:

interface MapLike<K, V> {
  /** The number of entries. */
  size: number;

  /** Whether has an entry with the given {@link key}. */
  has: (key: K) => boolean;

  /** Returns the value of the entry with the given {@link key}, if any such entry exists; otherwise returns `undefined`. */
  get: (key: K) => V | undefined;

  /** Adds an entry with the given {@link key} mapped to the given {@link value}. */
  set: (key: K, value: V) => this;

  /** Deletes the entry with the given {@link key}. */
  delete: (key: K) => boolean;

  /** Removes all entries. */
  clear: () => void;
}

SetLike:

interface SetLike<T> {
  /** The number of values. */
  size: number;

  /** Whether has the given {@link value}. */
  has: (value: T) => boolean;

  /** Adds the given {@link value}. */
  add: (value: T) => this;

  /** Deletes the given {@link value}. */
  delete: (value: T) => boolean;

  /** Removes all values. */
  clear: () => void;
}

Throwing error

All constructors specify a capacity as their first argument.

If it is a negative number, an error is thrown.

import { FIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/fifo.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => new FIFOMap(-1));

FIFO

FIFO(First In, First Out) implementations.

FIFOMap

When the upper limit is reached, replaces the entry with FIFO algorithm.

import { FIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/fifo.ts";

declare const maxNumOfEntries: number;
const map = new FIFOMap(maxNumOfEntries);

FIFOSet

When the upper limit is reached, replaces the value with FIFO algorithm.

import { FIFOSet } from "https://deno.land/x/cache_mapset@$VERSION/fifo.ts";

declare const maxNumOfValues: number;
const set = new FIFOSet(maxNumOfValues);

LIFO

LIFO(Last In, First Out) implementations.

LIFOMap

When the upper limit is reached, replaces the entry with LIFO algorithm.

import { LIFOMap } from "https://deno.land/x/cache_mapset@$VERSION/lifo.ts";

declare const maxNumOfEntries: number;
const map = new LIFOMap(maxNumOfEntries);

LIFOSet

When the upper limit is reached, replaces the value with LIFO algorithm.

import { LIFOSet } from "https://deno.land/x/cache_mapset@$VERSION/lifo.ts";

declare const maxNumOfValues: number;
const set = new LIFOSet(maxNumOfValues);

LRU

LRU(Least Recently Used) implementations.

LRUMap

When the upper limit is reached, replaces the entry with LRU algorithm.

import { LRUMap } from "https://deno.land/x/cache_mapset@$VERSION/lru.ts";

declare const maxNumOfEntries: number;
const map = new LRUMap(maxNumOfEntries);

LRUSet

When the upper limit is reached, replaces the value with LRU algorithm.

import { LRUSet } from "https://deno.land/x/cache_mapset@$VERSION/lru.ts";

declare const maxNumOfValues: number;
const set = new LRUSet(maxNumOfValues);

LFU

LFU(Least Frequently Used) implementations.

LFUMap

When the upper limit is reached, replaces the entry with LFU algorithm.

import { LFUMap } from "https://deno.land/x/cache_mapset@$VERSION/lfu.ts";

declare const maxNumOfEntries: number;
const map = new LFUMap(maxNumOfEntries);

LFUSet

When the upper limit is reached, replaces the value with LFU algorithm.

import { LFUSet } from "https://deno.land/x/cache_mapset@$VERSION/lfu.ts";

declare const maxNumOfValues: number;
const set = new LFUSet(maxNumOfValues);

API

See deno doc for all APIs.

License

Copyright © 2023-present Tomoki Miyauchi.

Released under the MIT license