@vanesyan/lru-cache

1.0.0 • Public • Published

LRU cache

Linux Build
LRU cache implementation on top of Map.

Installation

The easiest way to get the package is to install it through npm

$ npm install @vanesyan/lru-cache

Example

const cache = new LRUCache(100)
const obj   = {}

cache.set('foo', 1)
cache.set(obj, 2)

console.log(cache.get(obj)) // => 2

API

LRUCache(capacity[, age])

capacity - Cache capacity
age - Maximum age of cached entry in milliseconds

cache#capacity

Returns the cache capacity.

cache#size

Returns the cache size.

cache#set(key, value)

Sets the value for the key in the cache.

cache.set('foo', { bar: 'baz' })

cache#get(key)

Returns cached value associated to the key if exists otherwise returns undefined.

cache.get('foo')

cache#has(key)

Check whether an element with specified key exists or not. Returns true if exists, otherwise false.

cache.has('foo')

cache#clear()

Removes all cached key-value pairs.

cache.clear()

cache#delete(key)

Removes the specified element by key from the cache. Returns true if the key-value pair was successfully removed from cache, otherwise false.

cache.delete('foo')

Licence

Released under the MIT licence.

Readme

Keywords

Package Sidebar

Install

npm i @vanesyan/lru-cache

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • vanesyan