fast-lru-cache
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Least recently used cache

The fast and simple LRU cache realization.

  • No other dependencies;
  • Flyweight (~4.4KB or ~2.4KB if you minify it);
  • TypeScript typings;
  • Short and simple API.

Installation:

npm install fast-lru-cache --save

API

LRUCache

LRUCache<K, T> is a generic class that has two type parameters:

  • K - is a type of cache keys. The acceptable values are string or number;
  • T - is a type of cache entries. The acceptable value is any of the types.

constructor

constructor(max: number, unusedTimeout?: number, checkoutInterval?: number);
  • max - sets the maximum count of the cache entries. It is an integer and should not be less than 2. Also, it should not be equal to Infinity because, in that case, it will be a Map object with redundant functionality.
  • unusedTimeout(optional) - sets the maximum time in milliseconds, after which cache entries rates as unused. It should be more than 0 and not equal to Infinity. If this argument passed, the cache validity method will be called with the defined interval and cleanse unused cache entries.
  • checkoutInterval(optional) - sets the checkout interval in milliseconds of the validity of cache entries. It should be more than 0 and not equal to Infinity. The default value is equal to unusedTimeout.

get

get(key: K): T | null;

Gets a cache entry by the specified key. The method returns the null if the key is absent in a Cache object.

set

set(key: K, entry: T): void;

Adds a cache entry with a specified key. The method throws the error if a specified key already exists in a Cache object.

delete

delete(key: K): boolean;

Deletes a cache entry by the specified key and returns true if the specified key exists. Or false if the entry does not exist.

clear

clear(): void;

Clears a Cache object.

Readme

Keywords

Package Sidebar

Install

npm i fast-lru-cache

Weekly Downloads

3

Version

1.0.0

License

ISC

Unpacked Size

8.02 kB

Total Files

4

Last publish

Collaborators

  • lavrovartem