Git Product home page Git Product logo

lru's Introduction

lru

A simple LRU cache supporting O(1) set, get and eviction of old keys

Installation

$ npm install lru

Example

var LRU = require('lru');

var cache = new LRU(2),
    evicted

cache.on('evict',function(data) { evicted = data });

cache.set('foo', 'bar');
cache.get('foo'); //=> bar

cache.set('foo2', 'bar2');
cache.get('foo2'); //=> bar2

cache.set('foo3', 'bar3'); // => evicted = { key: 'foo', value: 'bar' }
cache.get('foo3');         // => 'bar3'
cache.remove('foo2')       // => 'bar2'
cache.remove('foo4')       // => undefined
cache.length               // => 1

API

LRU( length )

Create a new LRU cache that stores length elements before evicting the least recently used. Optionally you can pass an options map with additional options:

{
  max: maxElementsToStore,
  maxAge: maxAgeInMilliseconds
}

If you pass maxAge items will be evicted if they are older than maxAge when you access them.

Returns: the newly created LRU cache

Properties

.length

The number of keys currently in the cache.

.keys

Array of all the keys currently in the cache.

Methods

.set( key, value )

Set the value of the key and mark the key as most recently used.

Returns: value

.get( key )

Query the value of the key and mark the key as most recently used.

Returns: value of key if found; undefined otherwise.

.peek( key )

Query the value of the key without marking the key as most recently used.

Returns: value of key if found; undefined otherwise.

.remove( key )

Remove the value from the cache.

Returns: value of key if found; undefined otherwise.

.on( event, callback )

Respond to events. Currently only the evict event is implemented. When a key is evicted, the callback is executed with an associative array containing the evicted key: {key: key, value: value}.

Credits

A big thanks to Dusty Leary who finished the library.

License

MIT

lru's People

Contributors

chriso avatar rreusser avatar mafintosh avatar diegorbaquero avatar dustyleary avatar bsonntag avatar fxsjy avatar

Watchers

James Cloos avatar  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.