Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

LRUCache

Default export, the thing you're using this module to get.

All properties from the options object (with the exception of {@link OptionsBase.max} and {@link OptionsBase.maxSize}) are added as normal public members. (max and maxBase are read-only getters.) Changing any of these will alter the defaults for subsequent method calls, but is otherwise safe.

class LRUCache<K extends {}, V extends {}, FC = unknown> {
constructor(options: LRUCache.Options<K, V, FC> | LRUCache<K, V, FC>);
allowStale: boolean;
allowStaleOnFetchAbort: boolean;
allowStaleOnFetchRejection: boolean;
ignoreFetchAbort: boolean;
get max(): LRUCache.Count;
get fetchMethod(): LRUCache.Fetcher<K, V, FC> | undefined;
maxEntrySize: LRUCache.Size;
get maxSize(): LRUCache.Count;
get calculatedSize(): LRUCache.Size;
get dispose(): LRUCache.Disposer<K, V> | undefined;
get disposeAfter(): LRUCache.Disposer<K, V> | undefined;
noDeleteOnFetchRejection: boolean;
noDeleteOnStaleGet: boolean;
noDisposeOnSet: boolean;
noUpdateTTL: boolean;
get size(): LRUCache.Count;
sizeCalculation?: LRUCache.SizeCalculator<K, V>;
ttlAutopurge: boolean;
ttlResolution: LRUCache.Milliseconds;
updateAgeOnGet: boolean;
updateAgeOnHas: boolean;
 
clear(): void;
delete(k: K): boolean;
dump(): [K, LRUCache.Entry<V>][];
entries(): Generator<(
| K
| V
| undefined
)
[]
, void, unknown>
;
fetch(k: K, fetchOptions: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : LRUCache.FetchOptionsWithContext<K, V, FC>): Promise<undefined | V>;
fetch(k: unknown extends FC ? K : FC extends undefined | void ? K : never, fetchOptions?: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : never): Promise<undefined | V>;
find(fn: (
v: V,
k: K,
self: LRUCache<K, V, FC>,
) => boolean
, getOptions?: LRUCache.GetOptions<K, V, FC>): V | undefined;
forEach(fn: (
v: V,
k: K,
self: LRUCache<K, V, FC>,
) => any
, thisp?: any): void;
get(k: K, getOptions?: LRUCache.GetOptions<K, V, FC>): V | undefined;
getRemainingTTL(key: K): number;
has(k: K, hasOptions?: LRUCache.HasOptions<K, V, FC>): boolean;
info(key: K): LRUCache.Entry<V> | undefined;
keys(): Generator<K, void, unknown>;
load(arr: [K, LRUCache.Entry<V>][]): void;
peek(k: K, peekOptions?: LRUCache.PeekOptions<K, V, FC>): V | undefined;
pop(): V | undefined;
purgeStale(): boolean;
rentries(): Generator<(
| K
| V
| undefined
)
[]
, void, unknown>
;
rforEach(fn: (
v: V,
k: K,
self: LRUCache<K, V, FC>,
) => any
, thisp?: any): void;
rkeys(): Generator<K, void, unknown>;
rvalues(): Generator<V | BackgroundFetch<V> | undefined, void, unknown>;
set(
k: K,
v: V | BackgroundFetch<V> | undefined,
setOptions?: LRUCache.SetOptions<K, V, FC>,
): this;
values(): Generator<V | BackgroundFetch<V> | undefined, void, unknown>;
[Symbol.iterator](): Generator<(
| K
| V
| undefined
)
[]
, void, unknown>
;
 
static unsafeExposeInternals<K extends {}, V extends {}, FC extends unknown = unknown>(c: LRUCache<K, V, FC>): {
starts: ZeroArray | undefined;
ttls: ZeroArray | undefined;
sizes: ZeroArray | undefined;
keyMap: Map<K, number>;
keyList: (K | undefined)[];
valList: (V | BackgroundFetch<V> | undefined)[];
next: NumberArray;
prev: NumberArray;
readonly head: Index;
readonly tail: Index;
free: StackLike;
isBackgroundFetch: (p: any) => boolean;
backgroundFetch: (
k: K,
index: number | undefined,
options: LRUCache.FetchOptions<K, V, FC>,
context: any,
) => BackgroundFetch<V>
;
moveToTail: (index: number) => void;
indexes: (options?: {
allowStale: boolean;
}
) => Generator<Index, void, unknown>
;
rindexes: (options?: {
allowStale: boolean;
}
) => Generator<Index, void, unknown>
;
isStale: (index: number | undefined) => boolean;
}
;
}

§Type Parameters

§
K extends {}
[src]
§
V extends {}
[src]
§
FC = unknown
[src]

§Constructors

§
new LRUCache(options: LRUCache.Options<K, V, FC> | LRUCache<K, V, FC>)
[src]

§Properties

§
allowStaleOnFetchRejection: boolean
[src]
§
fetchMethod: LRUCache.Fetcher<K, V, FC> | undefined readonly
[src]
§
calculatedSize: LRUCache.Size readonly
[src]

The total computed size of items in the cache (read-only)

§
dispose: LRUCache.Disposer<K, V> | undefined readonly
[src]
§
disposeAfter: LRUCache.Disposer<K, V> | undefined readonly
[src]
§
size: LRUCache.Count readonly
[src]

The number of items stored in the cache (read-only)

§Methods

§
clear(): void
[src]

Clear the cache entirely, throwing away all values.

§
delete(k: K): boolean
[src]

Deletes a key out of the cache. Returns true if the key was deleted, false otherwise.

§
dump(): [K, LRUCache.Entry<V>][]
[src]

Return an array of [key, LRUCache.Entry] tuples which can be passed to cache.load()

§
entries(): Generator<(
| K
| V
| undefined
)
[]
, void, unknown>
[src]

Return a generator yielding [key, value] pairs, in order from most recently used to least recently used.

§
fetch(k: K, fetchOptions: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : LRUCache.FetchOptionsWithContext<K, V, FC>): Promise<undefined | V>
[src]

Make an asynchronous cached fetch using the LRUCache.OptionsBase.fetchMethod function.

If multiple fetches for the same key are issued, then they will all be coalesced into a single call to fetchMethod.

Note that this means that handling options such as LRUCache.OptionsBase.allowStaleOnFetchAbort, LRUCache.FetchOptions.signal, and LRUCache.OptionsBase.allowStaleOnFetchRejection will be determined by the FIRST fetch() call for a given key.

This is a known (fixable) shortcoming which will be addresed on when someone complains about it, as the fix would involve added complexity and may not be worth the costs for this edge case.

fetch(k: unknown extends FC ? K : FC extends undefined | void ? K : never, fetchOptions?: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : never): Promise<undefined | V>
[src]
§
find(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => boolean, getOptions?: LRUCache.GetOptions<K, V, FC>): V | undefined
[src]

Find a value for which the supplied fn method returns a truthy value, similar to Array.find(). fn is called as fn(value, key, cache).

§
forEach(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => any, thisp?: any): void
[src]

Call the supplied function on each item in the cache, in order from most recently used to least recently used. fn is called as fn(value, key, cache). Does not update age or recenty of use. Does not iterate over stale values.

§
get(k: K, getOptions?: LRUCache.GetOptions<K, V, FC>): V | undefined
[src]

Return a value from the cache. Will update the recency of the cache entry found.

If the key is not found, get() will return undefined.

§
getRemainingTTL(key: K): number
[src]

Return the remaining TTL time for a given entry key

§
has(k: K, hasOptions?: LRUCache.HasOptions<K, V, FC>): boolean
[src]

Check if a key is in the cache, without updating the recency of use. Will return false if the item is stale, even though it is technically in the cache.

Will not update item age unless LRUCache.OptionsBase.updateAgeOnHas is set.

§
info(key: K): LRUCache.Entry<V> | undefined
[src]

Get the extended info about a given entry, to get its value, size, and TTL info simultaneously. Like {@link LRUCache#dump}, but just for a single key. Always returns stale values, if their info is found in the cache, so be sure to check for expired TTLs if relevant.

§
keys(): Generator<K, void, unknown>
[src]

Return a generator yielding the keys in the cache, in order from most recently used to least recently used.

§
load(arr: [K, LRUCache.Entry<V>][]): void
[src]

Reset the cache and load in the items in entries in the order listed. Note that the shape of the resulting cache may be different if the same options are not used in both caches.

§
peek(k: K, peekOptions?: LRUCache.PeekOptions<K, V, FC>): V | undefined
[src]

Like {@link LRUCache#get} but doesn't update recency or delete stale items.

Returns undefined if the item is stale, unless LRUCache.OptionsBase.allowStale is set.

§
pop(): V | undefined
[src]

Evict the least recently used item, returning its value or undefined if cache is empty.

§
purgeStale(): boolean
[src]

Delete any stale entries. Returns true if anything was removed, false otherwise.

§
rentries(): Generator<(
| K
| V
| undefined
)
[]
, void, unknown>
[src]

Inverse order version of LRUCache.entries

Return a generator yielding [key, value] pairs, in order from least recently used to most recently used.

§
rforEach(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => any, thisp?: any): void
[src]

The same as LRUCache.forEach but items are iterated over in reverse order. (ie, less recently used items are iterated over first.)

§
rkeys(): Generator<K, void, unknown>
[src]

Inverse order version of LRUCache.keys

Return a generator yielding the keys in the cache, in order from least recently used to most recently used.

§
rvalues(): Generator<V | BackgroundFetch<V> | undefined, void, unknown>
[src]

Inverse order version of LRUCache.values

Return a generator yielding the values in the cache, in order from least recently used to most recently used.

§
set(k: K, v: V | BackgroundFetch<V> | undefined, setOptions?: LRUCache.SetOptions<K, V, FC>): this
[src]

Add a value to the cache.

Note: if undefined is specified as a value, this is an alias for {@link LRUCache#delete}

§
values(): Generator<V | BackgroundFetch<V> | undefined, void, unknown>
[src]

Return a generator yielding the values in the cache, in order from most recently used to least recently used.

§
[Symbol.iterator](): Generator<(
| K
| V
| undefined
)
[]
, void, unknown>
[src]

Iterating over the cache itself yields the same results as LRUCache.entries

§Static Methods

§
unsafeExposeInternals<K extends {}, V extends {}, FC extends unknown = unknown>(c: LRUCache<K, V, FC>): {
starts: ZeroArray | undefined;
ttls: ZeroArray | undefined;
sizes: ZeroArray | undefined;
keyMap: Map<K, number>;
keyList: (K | undefined)[];
valList: (V | BackgroundFetch<V> | undefined)[];
next: NumberArray;
prev: NumberArray;
readonly head: Index;
readonly tail: Index;
free: StackLike;
isBackgroundFetch: (p: any) => boolean;
backgroundFetch: (
k: K,
index: number | undefined,
options: LRUCache.FetchOptions<K, V, FC>,
context: any,
) => BackgroundFetch<V>
;
moveToTail: (index: number) => void;
indexes: (options?: {
allowStale: boolean;
}
) => Generator<Index, void, unknown>
;
rindexes: (options?: {
allowStale: boolean;
}
) => Generator<Index, void, unknown>
;
isStale: (index: number | undefined) => boolean;
}
[src]

Do not call this method unless you need to inspect the inner workings of the cache. If anything returned by this object is modified in any way, strange breakage may occur.

These fields are private for a reason!