close
Skip to main content

@std/cache@0.2.3
Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
License
MIT
Downloads40/wk
Publisheda month ago (0.2.3)

UNSTABLE: Cache utilities

Classes

c
LruCache<K, V>(
maxSize: number,
options?: LruCacheOptions<K, V>
)

Least-recently-used cache.

  • clear(): void

    Clears the cache.

  • delete(key: K): boolean

    Deletes the value associated with the given key.

  • get(key: K): V | undefined

    Gets the element with the specified key.

  • has(key: K): boolean

    Checks whether an element with the specified key exists or not. Does not update the entry's position in the eviction order.

  • maxSize(): number

    The maximum number of entries to store in the cache.

  • peek(key: K): V | undefined

    Returns the value associated with the given key, or undefined if the key is not present, without updating its position in the eviction order.

  • set(
    key: K,
    value: V
    ): this

    Sets the specified key to the specified value.

c
TtlCache<K, V>(
defaultTtl: number,
options?: TtlCacheOptions<K, V>
)

Time-to-live cache.

  • clear(): void

    Clears the cache.

  • delete(key: K): boolean

    Deletes the value associated with the given key.

  • get(key: K): V | undefined

    Gets the value associated with the specified key.

  • peek(key: K): V | undefined

    Returns the value associated with the given key, or undefined if the key is not present, without resetting its TTL.

  • set(
    key: K,
    value: V,
    options?: TtlCacheSetOptions
    ): this

    Set a value in the cache.

Functions

f
memoize<
Fn extends (...args: never[]) => unknown,
Key = string,
Cache extends MemoizationCache<Key, MemoizationCacheResult<ReturnType<Fn>>> = Map<Key, MemoizationCacheResult<ReturnType<Fn>>>
>
(
fn: Fn,
options?: MemoizeOptions<Fn, Key, Cache>
): Fn & { cache: Cache; }

Cache the results of a function based on its arguments.

Interfaces

I

Options for the LruCache constructor.

  • onEject: (
    ejectedKey: K,
    ejectedValue: V,
    reason: LruCacheEjectionReason
    ) => void

    Callback invoked when an entry is removed, whether by eviction, manual deletion, or clearing the cache. The entry is already removed from the cache when this callback fires. Overwriting an existing key via set() does not trigger this callback. The cache is not re-entrant during this callback: calling set, delete, or clear will throw.

I

A cache suitable for use with memoize.

  • delete(key: K): unknown

    Removes the value associated with the given key from the cache.

  • get(key: K): V | undefined

    Returns the cached value associated with the given key, if present.

  • has(key: K): boolean

    Checks whether a value for the given key exists in the cache.

  • set(
    key: K,
    val: V
    ): unknown

    Stores a value in the cache under the given key.

I

Options for the TtlCache constructor.

  • onEject: (
    ejectedKey: K,
    ejectedValue: V
    ) => void

    Callback invoked when an entry is removed, whether by TTL expiry, manual deletion, or clearing the cache.

  • When true, each get() call resets the entry's TTL.

  • A maximum lifetime in milliseconds for this entry, measured from the time it is set. When slidingExpiration is enabled, the sliding window cannot extend past this duration. Throws if slidingExpiration is not enabled.

  • ttl: number

    A custom time-to-live in milliseconds for this entry. If supplied, overrides the cache's default TTL. Must be a finite, non-negative number.

Type Aliases

T
LruCacheEjectionReason = "evicted" | "deleted" | "cleared"

The reason an entry was removed from the cache.

T
MemoizationCacheResult<T> =
{ kind: "ok"; value: T; }
| { kind: "error"; error: unknown; }
| (T extends Promise<unknown> ? { kind: "promise"; value: T; } : never)

The result of a memoized function, as stored in its cache.

T
MemoizeOptions<
Fn extends (...args: never[]) => unknown,
Key,
Cache extends MemoizationCache<Key, MemoizationCacheResult<ReturnType<Fn>>>
>
= { cache?: Cache; getKey?: (
this: ThisParameterType<Fn>,
...args: Parameters<Fn>
) => Key
; errorIsCacheable?: (err: unknown) => boolean; }

Options for memoize.

  • cache: Cache

    Provide a custom cache for getting previous results. By default, a new Map object is instantiated upon memoization and used as a cache, with no limit on the number of results to be cached.

  • errorIsCacheable: (err: unknown) => boolean

    Callback to determine if an error or other thrown value is cacheable.

  • getKey: (
    this: ThisParameterType<Fn>,
    ...args: Parameters<Fn>
    ) => Key

    Function to get a unique cache key from the function's arguments. By default, a composite key is created from all the arguments plus the this value, using reference equality to check for equivalence.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/cache

Import symbol

import * as mod from "@std/cache";
or

Import directly with a jsr specifier

import * as mod from "jsr:@std/cache";