pax_global_header00006660000000000000000000000064131363672000014513gustar00rootroot0000000000000052 comment=618c5cdcc591289e58fc02c93830b2dc6162cdfa quick-lru-1.1.0/000077500000000000000000000000001313636720000134265ustar00rootroot00000000000000quick-lru-1.1.0/.editorconfig000066400000000000000000000002571313636720000161070ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 quick-lru-1.1.0/.gitattributes000066400000000000000000000000351313636720000163170ustar00rootroot00000000000000* text=auto *.js text eol=lf quick-lru-1.1.0/.gitignore000066400000000000000000000000431313636720000154130ustar00rootroot00000000000000node_modules yarn.lock .nyc_output quick-lru-1.1.0/.npmrc000066400000000000000000000000231313636720000145410ustar00rootroot00000000000000package-lock=false quick-lru-1.1.0/.travis.yml000066400000000000000000000002341313636720000155360ustar00rootroot00000000000000language: node_js node_js: - '8' - '6' - '4' after_success: - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' quick-lru-1.1.0/index.js000066400000000000000000000033341313636720000150760ustar00rootroot00000000000000'use strict'; class QuickLRU { constructor(opts) { opts = Object.assign({}, opts); if (!(opts.maxSize && opts.maxSize > 0)) { throw new TypeError('`maxSize` must be a number greater than 0'); } this.maxSize = opts.maxSize; this.cache = new Map(); this.oldCache = new Map(); this._size = 0; } _set(key, value) { this.cache.set(key, value); this._size++; if (this._size >= this.maxSize) { this._size = 0; this.oldCache = this.cache; this.cache = new Map(); } } get(key) { if (this.cache.has(key)) { return this.cache.get(key); } if (this.oldCache.has(key)) { const value = this.oldCache.get(key); this._set(key, value); return value; } } set(key, value) { if (this.cache.has(key)) { this.cache.set(key, value); } else { this._set(key, value); } return this; } has(key) { return this.cache.has(key) || this.oldCache.has(key); } peek(key) { if (this.cache.has(key)) { return this.cache.get(key); } if (this.oldCache.has(key)) { return this.oldCache.get(key); } } delete(key) { if (this.cache.delete(key)) { this._size--; } this.oldCache.delete(key); } clear() { this.cache.clear(); this.oldCache.clear(); this._size = 0; } * keys() { for (const el of this) { yield el[0]; } } * values() { for (const el of this) { yield el[1]; } } * [Symbol.iterator]() { for (const el of this.cache) { yield el; } for (const el of this.oldCache) { if (!this.cache.has(el[0])) { yield el; } } } get size() { let oldCacheSize = 0; for (const el of this.oldCache) { if (!this.cache.has(el[0])) { oldCacheSize++; } } return this._size + oldCacheSize; } } module.exports = QuickLRU; quick-lru-1.1.0/license000066400000000000000000000021251313636720000147730ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. quick-lru-1.1.0/package.json000066400000000000000000000012471313636720000157200ustar00rootroot00000000000000{ "name": "quick-lru", "version": "1.1.0", "description": "Simple \"Least Recently Used\" (LRU) cache", "license": "MIT", "repository": "sindresorhus/quick-lru", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && nyc ava" }, "files": [ "index.js" ], "keywords": [ "lru", "quick", "cache", "caching", "least", "recently", "used", "fast", "map", "hash", "buffer" ], "devDependencies": { "ava": "*", "coveralls": "^2.12.0", "nyc": "^11.0.3", "xo": "*" } } quick-lru-1.1.0/readme.md000066400000000000000000000037041313636720000152110ustar00rootroot00000000000000# quick-lru [![Build Status](https://travis-ci.org/sindresorhus/quick-lru.svg?branch=master)](https://travis-ci.org/sindresorhus/quick-lru) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/quick-lru/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/quick-lru?branch=master) > Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29) Useful when you need to cache something and limit memory usage. Inspired by the [`haslru` algorithm](https://github.com/dominictarr/hashlru#algorithm), but instead uses [`Map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map) to support keys of any type, not just strings, and values can be `undefined`. ## Install ``` $ npm install quick-lru ``` ## Usage ```js const QuickLRU = require('quick-lru'); const lru = new QuickLRU({maxSize: 1000}); lru.set('πŸ¦„', '🌈'); lru.has('πŸ¦„'); //=> true lru.get('πŸ¦„'); //=> '🌈' ``` ## API ### new QuickLRU([options]) Returns a new instance. ### options Type: `Object` #### maxSize *Required*
Type: `Object` Maximum number of items before evicting the least recently used items. ### Instance The instance is [`iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) so you can use it directly in a [`for…of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop. Both `key` and `value` can be of any type. #### .set(key, value) Set an item. Returns the instance. #### .get(key) Get an item. #### .has(key) Check if an item exists. #### .peek(key) Get an item without marking it as recently used. #### .delete(key) Delete an item. #### .clear() Delete all items. #### .keys() Iterable for all the keys. #### .values() Iterable for all the values. #### .size Get the item count. ## License MIT Β© [Sindre Sorhus](https://sindresorhus.com) quick-lru-1.1.0/test.js000066400000000000000000000065501313636720000147510ustar00rootroot00000000000000import test from 'ava'; import M from '.'; const lruWithDupes = () => { const lru = new M({maxSize: 2}); lru.set('key', 'value'); lru.set('keyDupe', 1); lru.set('keyDupe', 2); return lru; }; test('main', t => { t.throws(() => { new M(); // eslint-disable-line no-new }, /maxSize/); }); test('.get() / .set()', t => { const lru = new M({maxSize: 100}); lru.set('foo', 1); lru.set('bar', 2); t.is(lru.get('foo'), 1); t.is(lru.size, 2); }); test('.get() - limit', t => { const lru = new M({maxSize: 2}); lru.set('1', 1); lru.set('2', 2); t.is(lru.get('1'), 1); t.is(lru.get('3'), undefined); lru.set('3', 3); lru.get('1'); lru.set('4', 4); lru.get('1'); lru.set('5', 5); t.true(lru.has('1')); }); test('.set() - limit', t => { const lru = new M({maxSize: 2}); lru.set('foo', 1); lru.set('bar', 2); t.is(lru.get('foo'), 1); t.is(lru.get('bar'), 2); lru.set('baz', 3); lru.set('faz', 4); t.false(lru.has('foo')); t.false(lru.has('bar')); t.true(lru.has('baz')); t.true(lru.has('faz')); t.is(lru.size, 2); }); test('.set() - update item', t => { const lru = new M({maxSize: 100}); lru.set('foo', 1); t.is(lru.get('foo'), 1); lru.set('foo', 2); t.is(lru.get('foo'), 2); t.is(lru.size, 1); }); test('.has()', t => { const lru = new M({maxSize: 100}); lru.set('foo', 1); t.true(lru.has('foo')); }); test('.peek()', t => { const lru = new M({maxSize: 2}); lru.set('1', 1); t.is(lru.peek('1'), 1); lru.set('2', 2); t.is(lru.peek('1'), 1); t.is(lru.peek('3'), undefined); lru.set('3', 3); lru.set('4', 4); t.false(lru.has('1')); }); test('.delete()', t => { const lru = new M({maxSize: 100}); lru.set('foo', 1); lru.set('bar', 2); lru.delete('foo'); t.false(lru.has('foo')); t.true(lru.has('bar')); t.is(lru.size, 1); }); test('.delete() - limit', t => { const lru = new M({maxSize: 2}); lru.set('foo', 1); lru.set('bar', 2); t.is(lru.size, 2); lru.delete('foo'); t.false(lru.has('foo')); t.true(lru.has('bar')); lru.delete('bar'); t.is(lru.size, 0); }); test('.clear()', t => { const lru = new M({maxSize: 2}); lru.set('foo', 1); lru.set('bar', 2); lru.set('baz', 3); lru.clear(); t.is(lru.size, 0); }); test('.keys()', t => { const lru = new M({maxSize: 2}); lru.set('1', 1); lru.set('2', 2); lru.set('3', 3); t.deepEqual([...lru.keys()].sort(), ['1', '2', '3']); }); test('.keys() - accounts for dupes', t => { const lru = lruWithDupes(); t.deepEqual([...lru.keys()].sort(), ['key', 'keyDupe']); }); test('.values()', t => { const lru = new M({maxSize: 2}); lru.set('1', 1); lru.set('2', 2); lru.set('3', 3); t.deepEqual([...lru.values()].sort(), [1, 2, 3]); }); test('.values() - accounts for dupes', t => { const lru = lruWithDupes(); t.deepEqual([...lru.values()].sort(), [2, 'value']); }); test('.[Symbol.iterator]()', t => { const lru = new M({maxSize: 2}); lru.set('1', 1); lru.set('2', 2); lru.set('3', 3); t.deepEqual([...lru].sort(), [['1', 1], ['2', 2], ['3', 3]]); }); test('.[Symbol.iterator]() - accounts for dupes', t => { const lru = lruWithDupes(); t.deepEqual([...lru].sort(), [['key', 'value'], ['keyDupe', 2]]); }); test('.size', t => { const lru = new M({maxSize: 100}); lru.set('1', 1); lru.set('2', 2); t.is(lru.size, 2); lru.delete('1'); t.is(lru.size, 1); lru.set('3', 3); t.is(lru.size, 2); }); test('.size - accounts for dupes', t => { const lru = lruWithDupes(); t.is(lru.size, 2); });