Code Monkey home page Code Monkey logo

modern-lru's Introduction

LRU cache for node.js

npm Travis Coverage

Simple LRU cache implementation on modern javascript based on native Map class.

Installation

npm i modern-lru --save

API

LRU cache class is based on native Map so API is similar.

Properties

.size

The number of keys currently in the cache.

.limit

Cache keys limit in current instance

Methods

.clear()

Removes all key/value pairs from the LRU object.

.delete(key)

Removes any value associated to the key and returns the value that .has(key) would have previously returned. .has(key) will return false afterwards.

.entries()

Returns a new LRUIterator object that contains an array of [key, value] for each element in the LRU object in last usage order.

.forEach(callbackFn[, thisArg])

Calls callbackFn once for each key-value pair present in the LRU object, in last usage order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.

.get(key)

Returns the value associated to the key, or undefined if there is none.

.has(key)

Returns a boolean asserting whether a value has been associated to the key in the LRU object or not.

.keys()

Returns a new LRUIterator object thet contains the keys for each element in the LRU object in last usage order.

.set(key, value)

Sets the value for the key in the LRU object. Returns the LRU object.

.values()

Returns a new LRUIterator object that contains the values for each element in the LRU object in last usage order.

[@@iterator]()

Returns a new LRUIterator object that contains an array of [key, value] for each element in the LRU object in last usage order.

Example

const LRU = require('modern-lru');

// lru cache with limit of 3 entries
const cache = new LRU(3);

console.log(cache.limit); // 3
console.log(cache.size); // 0 (elements count)

cache.set('first', 'first');
cache.set('second', 'second');
cache.set('third', 'third');

console.log(cache.get('second')); // second
console.log(cache); // LRU { 'second' => 'second', 'third' => 'third', 'first' => 'first' }

cache.set('fourth', 'fourth');
console.log(cache.has('first')); // false ("first" was evicted)
console.log(cache.get('fourth')); // fourth

// also it implements default Map
console.log(cache instanceof Map); // true

// so you can use `keys()` for example
console.log(Array.from(cache.keys()).join(', ')); // 'fourth, second, third'

// or use objects, as with Map
const myObject = { test: 5 };
cache.set(myObject, 'testme');
console.log(cache.has(myObject)); // true
console.log(cache.get(myObject)); // 'testme'
console.log(cache.get({ test: 5})); // undefined (different pointer)

cache.clear();

modern-lru's People

Contributors

silentroach avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.