# lru

> A simple O(1) LRU cache

Latest version **3.1.0** (published 2016-07-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install lru
pnpm add lru
yarn add lru
bun add lru
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2016-07-07 |
| First published | 2012-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.4.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 136 |
| Author | Chris O'Hara |
| Maintainers | cohara87, mafintosh |

## Links

- npm: https://www.npmjs.com/package/lru
- Repository: https://github.com/chriso/lru
- Homepage: http://github.com/chriso/lru
- Issues: http://github.com/chriso/lru/issues
- npm.io page: https://npm.io/package/lru

## Dependencies (1)

- [inherits](https://npm.io/package/inherits.md) ^2.0.1

## Recent versions

- 3.1.0 (latest) — 2016-07-07
- 3.0.0 — 2016-06-10
- 2.0.1 — 2016-03-27
- 2.0.0 — 2016-02-08
- 1.2.1 — 2016-02-08
- 1.2.0 — 2016-01-05
- 1.1.0 — 2016-01-03
- 1.0.1 — 2016-01-03
- 1.0.0 — 2015-08-03
- 0.2.2 — 2015-07-01
- 0.2.0 — 2015-07-01
- 0.1.5 — 2012-08-15
- 0.1.4 — 2012-04-18
- 0.1.3 — 2012-04-01
- 0.1.1 — 2012-03-08
- … 1 more at https://npm.io/package/lru/versions

## README

# lru

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

## Installation

```bash
$ npm install lru
```

### Example

```javascript
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
cache.keys                 // => ['foo3']

cache.clear()              // => it will NOT emit the 'evict' event
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:

```js
{
  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. This method does **NOT** emit the `evict` event.

##### `.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](https://github.com/dustyleary) who
finished the library.

### License

MIT

---
_Source: https://npm.io/package/lru · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
