@lnear/cache
@lnear/cache
is a TypeScript library that provides implementations for two types of caching mechanisms: Least Recently Used (LRU) and Least Frequently Used (LFU). Caching is crucial in applications to improve performance by storing frequently accessed data in memory.
This package offers flexibility through generic implementations, allowing developers to cache any type of data with customizable options for maximum size and expiration times.
Installation
Install the package via npm:
Usage
Importing
Import the cache classes into your TypeScript project:
Creating a Cache Instance
LRU Cache Example:
LFU Cache Example:
API
Common Methods for LRUCache and LFUCache
set(key: K, value: V, options?: { maxAge?: number }): this
: Adds or updates a key-value pair in the cache.get(key: K): Maybe<V>
: Retrieves the value associated with the key, if present.has(key: K): boolean
: Checks if the cache contains the specified key.delete(key: K): boolean
: Deletes a key-value pair from the cache.clear(): void
: Clears all entries from the cache.resize(newSize: number): void
: Resizes the cache to the new size, evicting entries if necessary.entries(): IterableIterator<[K, V]>
: Returns an iterator for all entries in the cache.forEach(callback: (value: V, key: K, map: Cache<K, V>) => void, thisArg?: any): void
: Executes a callback for each entry in the cache.peek(key: K): Maybe<V>
: Retrieves the value associated with the key without updating its frequency.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For questions or issues regarding the package, please open an issue on GitHub.