Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/velo/src/cache/cache.ts>Cache

A high-performance caching library for Deno. Supports LRU, LFU, ARC, and TinyLFU.
Latest
interface Cache
import { type Cache } from "https://deno.land/x/velo@1.0.0/src/cache/cache.ts";

Maps keys to values. Entries are manually added via set and either evicted by the caching policy or manually removed via remove.

Properties

readonly
capacity: number

Returns the static maximum capacity of the cache.

readonly
size: number

Returns the number of items currently in the cache.

readonly
keys: K[]

Returns all keys stored in the cache. Ordering depends on the policy that is used; do not expect keys to be ordered in any particular way.

readonly
values: V[]

Returns all values stored in the cache. Ordering depends on the policy that is used; do not expect keys to be ordered in any particular way.

readonly
stats: CacheStatistics

Returns current cache statistics.

readonly
events: VeloEventEmitter<K, V>

Returns a VeloEventEmitter to add listeners for cache events.

readonly
options: Options<K, V>

Returns an Options objet that contains the configuration that was used to create this cache. This enables to create cache clones with an identical configuration.

Methods

get(key: K): V | undefined

Returns the value of an entry with given key. If the key is not cached, undefined is returned.

set(key: K, value: V): void

Maps a key to a value. If the key already exists in the cache, the value will be replaced by the new value.

setWithExpire(
key: K,
value: V,
expire: number,
): void

Performs a set. Additionally a ttl can be specified to timeout this specific key.

peek(key: K): V | undefined

Returns the value of an entry with given key. If the key is not cached, undefined is returned. This operation does not trigger policy actions and will not affect cache statistics.

has(key: K): boolean

Returns true if the cache contains key, otherwise false.

take(key: K): V | undefined

Returns the value associated with a key and removes the key from the cache. Equivalent of calling:

 cache.get(key);
 cache.remove(key);
remove(key: K): void

Removes an entry with key from the cache.

reset(): void

Discards all entries in the cache and clears internal state. View this as a destructive operation. Depending on the policy, historic data is lost (e.g. LFU frequencies). Not equivalent to calling remove for all keys.

forEach(callback: (item: Entry<K, V>, index?: number) => void): void

Array-like forEach.