Spaces:
Runtime error
Runtime error
File size: 1,752 Bytes
b5ea024 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# lru-queue
## Size limited queue based on [LRU](http://en.wikipedia.org/wiki/Least_Recently_Used#LRU) algorithm
_Originally derived from [memoizee](https://github.com/medikoo/memoize) package._
It's low-level utility meant to be used internally within cache algorithms. It backs up [`max`](https://github.com/medikoo/memoize#limiting-cache-size) functionality in [memoizee](https://github.com/medikoo/memoize) project.
### Installation
$ npm install lru-queue
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/)
### Usage
Create queue, and provide a limit
```javascript
var lruQueue = require('lru-queue');
var queue = lruQueue(3); // limit size to 3
```
Each queue exposes three methods:
#### queue.hit(id)
Registers hit for given _id_ (must be plain string).
```javascript
queue.hit('raz'); // size: 1
```
If hit doesn't remove any old item from list it returns `undefined`, otherwise it returns removed _id_.
```javascript
queue.hit('dwa'); // undefined, size: 2
queue.hit('trzy'); // undefined, size: 3 (at max)
queue.hit('raz'); // undefined, size: 3 (at max)
queue.hit('dwa'); // undefined, size: 3 (at max)
queue.hit('cztery'); // 'trzy', size: 3 (at max)
```
#### queue.delete(id);
_id's_ can be cleared from queue externally
```javascript
queue.delete('raz'); // size: 2
queue.delete('cztery'); // size: 1
```
#### queue.clear();
Resets queue
```javascript
queue.clear(); // size: 0
```
### Tests [](https://travis-ci.org/medikoo/lru-queue)
$ npm test
|