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.
npm install fast-lru-cache --save
LRUCache<K, T>
is a generic class that has two type parameters:
K
- is a type of cache keys. The acceptable values arestring
ornumber
;T
- is a type of cache entries. The acceptable value isany
of the types.
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 toInfinity
because, in that case, it will be aMap
object with redundant functionality.unusedTimeout
(optional) - sets the maximum time in milliseconds, after which cache entries rates as unused. It should be more than0
and not equal toInfinity
. 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 than0
and not equal toInfinity
. The default value is equal tounusedTimeout
.
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(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(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(): void;
Clears a Cache
object.