Socket
Socket
Sign inDemoInstall

nanolru

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nanolru

A simple O(1) LRU cache


Version published
Maintainers
1
Install size
14.8 kB
Created

Readme

Source

nanolru stability

npm version build status downloads js-standard-style

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

Fork of lru to remove dependency on events.

Installation

$ npm install nanolru

Example

var LRU = require('nanolru');

var cache = new LRU(2)

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

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

cache.set('foo3', 'bar3'); // 'foo' evicted
cache.get('foo3');         // => 'bar3'
cache.remove('foo2')       // => 'bar2'
cache.remove('foo4')       // => undefined
cache.length               // => 1
cache.keys                 // => ['foo3']

cache.clear()
cache.length               // => 0
cache.keys                 // => []

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.

.clear()

Clear the cache.

Credits

Original library by Chris O'Hara. A big thanks to Dusty Leary who finished the library.

License

MIT

FAQs

Last updated on 16 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc