lru-queue

2019-01-12 admin

lru-queue是什么

什么是lru-queue,LRU Queue

lru-queue使用教程帮助文档

Build status Windows status Transpilation status npm version

lru-queue

Size limited queue based on LRU algorithm

Originally derived from memoizee package.

It’s low-level utility meant to be used internally within cache algorithms. It backs up max functionality in memoizee 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, Webmake or Webpack

Usage

Create queue, and provide a limit

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).

queue.hit("raz"); // size: 1

If hit doesn’t remove any old item from list it returns undefined, otherwise removed id is returned.

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

queue.delete("raz"); // size: 2
queue.delete("cztery"); // size: 1

queue.clear();

Resets the queue

queue.clear(); // size: 0

Tests

$ npm test

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-50786.html

文章标题:lru-queue

回到顶部