├── .bumpedrc ├── .editorconfig ├── .gitattributes ├── .github └── dependabot.yml ├── .gitignore ├── .npmignore ├── .npmrc ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── compositor.json ├── index.js ├── package.json ├── src └── index.js └── test ├── index.js └── mocha.opts /.bumpedrc: -------------------------------------------------------------------------------- 1 | files: [ 2 | 'package.json' 3 | ] 4 | 5 | plugins: 6 | 7 | prerelease: 8 | 9 | 'Linting config files': 10 | plugin: 'bumped-finepack' 11 | 12 | postrelease: 13 | 14 | 'Generating CHANGELOG file': 15 | plugin: 'bumped-changelog' 16 | 17 | 'Commiting new version': 18 | plugin: 'bumped-terminal' 19 | command: 'git add CHANGELOG.md package.json && git commit -m "Release $newVersion"' 20 | 21 | 'Detecting problems before publish': 22 | plugin: 'bumped-terminal' 23 | command: 'git-dirty && npm test' 24 | 25 | 'Publishing tag at GitHub': 26 | plugin: 'bumped-terminal' 27 | command: 'git tag $newVersion && git push && git push --tags' 28 | 29 | 'Publishing at NPM': 30 | plugin: 'bumped-terminal' 31 | command: 'npm publish' 32 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 2 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | max_line_length = 100 13 | indent_brace_style = 1TBS 14 | spaces_around_operators = true 15 | quote_type = auto 16 | 17 | [package.json] 18 | indent_style = space 19 | indent_size = 2 20 | 21 | [*.md] 22 | trim_trailing_whitespace = false 23 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | # Check for updates to GitHub Actions every weekday 11 | interval: "daily" 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############################ 2 | # npm 3 | ############################ 4 | node_modules 5 | npm-debug.log 6 | 7 | ############################ 8 | # tmp, editor & OS files 9 | ############################ 10 | .tmp 11 | *.swo 12 | *.swp 13 | *.swn 14 | *.swm 15 | .DS_Store 16 | *# 17 | *~ 18 | .idea 19 | *sublime* 20 | nbproject 21 | 22 | ############################ 23 | # Tests 24 | ############################ 25 | testApp 26 | coverage 27 | .nyc_output 28 | 29 | ############################ 30 | # Other 31 | ############################ 32 | .node_history 33 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .project 3 | *.sublime-* 4 | .DS_Store 5 | *.seed 6 | *.log 7 | *.csv 8 | *.dat 9 | *.out 10 | *.pid 11 | *.swp 12 | *.swo 13 | node_modules 14 | coverage 15 | *.tgz 16 | *.xml 17 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | unsafe-perm=true 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ## 2.1.2 (2017-10-28) 3 | 4 | * Add missing dep ([270c33d](https://github.com/kikobeats/hyperlru/commit/270c33d)) 5 | 6 | 7 | 8 | 9 | ## 2.1.1 (2017-10-10) 10 | 11 | * Remove unnecessary deps ([8f1a85c](https://github.com/kikobeats/hyperlru/commit/8f1a85c)) 12 | 13 | 14 | 15 | 16 | # 2.1.0 (2017-10-03) 17 | 18 | * Add more tests ([6499306](https://github.com/kikobeats/hyperlru/commit/6499306)) 19 | * Avoid increment size under max ([22b02c0](https://github.com/kikobeats/hyperlru/commit/22b02c0)) 20 | * Avoid unnecessary method ([576199f](https://github.com/kikobeats/hyperlru/commit/576199f)) 21 | * Update ([a9490eb](https://github.com/kikobeats/hyperlru/commit/a9490eb)) 22 | 23 | 24 | 25 | 26 | ## 2.0.2 (2017-10-02) 27 | 28 | * Add missing file ([1b05191](https://github.com/kikobeats/hyperlru/commit/1b05191)) 29 | 30 | 31 | 32 | 33 | ## 2.0.1 (2017-10-02) 34 | 35 | * Add missing file ([193376f](https://github.com/kikobeats/hyperlru/commit/193376f)) 36 | * Update README.md ([754fb33](https://github.com/kikobeats/hyperlru/commit/754fb33)) 37 | 38 | 39 | 40 | 41 | # 2.0.0 (2017-10-02) 42 | 43 | * Move specific implementations in separate pkgs ([0656386](https://github.com/kikobeats/hyperlru/commit/0656386)) 44 | * Tweaks ([9b5e125](https://github.com/kikobeats/hyperlru/commit/9b5e125)) 45 | * WIP ([3ff855a](https://github.com/kikobeats/hyperlru/commit/3ff855a)) 46 | * WIP ([adb4be1](https://github.com/kikobeats/hyperlru/commit/adb4be1)) 47 | * WIP ([e81cd9d](https://github.com/kikobeats/hyperlru/commit/e81cd9d)) 48 | * WIP ([5fc3f74](https://github.com/kikobeats/hyperlru/commit/5fc3f74)) 49 | * WIP ([fd0456e](https://github.com/kikobeats/hyperlru/commit/fd0456e)) 50 | 51 | 52 | 53 | 54 | ## 1.0.2 (2017-10-01) 55 | 56 | * Update doc ([9134b9e](https://github.com/kikobeats/hyperlru/commit/9134b9e)) 57 | 58 | 59 | 60 | 61 | ## 1.0.1 (2017-10-01) 62 | 63 | * Avoid anonymous fn ([09fb9b9](https://github.com/kikobeats/hyperlru/commit/09fb9b9)) 64 | * Avoid unnecessary variables ([bcb03ef](https://github.com/kikobeats/hyperlru/commit/bcb03ef)) 65 | * Avoid use this ([4ba74fc](https://github.com/kikobeats/hyperlru/commit/4ba74fc)) 66 | * Extract hasKey method ([fba3c9a](https://github.com/kikobeats/hyperlru/commit/fba3c9a)) 67 | * Tweaks ([a7d4498](https://github.com/kikobeats/hyperlru/commit/a7d4498)) 68 | * Update README.md ([4339b3d](https://github.com/kikobeats/hyperlru/commit/4339b3d)) 69 | * docs(readme): add Greenkeeper badge ([4f9cd55](https://github.com/kikobeats/hyperlru/commit/4f9cd55)) 70 | 71 | 72 | 73 | 74 | # 1.0.0 (2016-12-25) 75 | 76 | * Add docs ([9f8974c](https://github.com/kikobeats/hyperlru/commit/9f8974c)) 77 | * First commit ([14a071b](https://github.com/kikobeats/hyperlru/commit/14a071b)) 78 | * Tweaks ([02a2286](https://github.com/kikobeats/hyperlru/commit/02a2286)) 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2016 Kiko Beats (https://github.com/Kikobeats) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hyperlru 2 | 3 | ![Last version](https://img.shields.io/github/tag/Kikobeats/hyperlru.svg?style=flat-square) 4 | [![Build Status](https://img.shields.io/travis/Kikobeats/hyperlru/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/hyperlru) 5 | [![Coverage Status](https://img.shields.io/coveralls/Kikobeats/hyperlru.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/hyperlru) 6 | [![Dependency status](https://img.shields.io/david/Kikobeats/hyperlru.svg?style=flat-square)](https://david-dm.org/Kikobeats/hyperlru) 7 | [![Dev Dependencies Status](https://img.shields.io/david/dev/Kikobeats/hyperlru.svg?style=flat-square)](https://david-dm.org/Kikobeats/hyperlru#info=devDependencies) 8 | [![NPM Status](https://img.shields.io/npm/dm/hyperlru.svg?style=flat-square)](https://www.npmjs.org/package/hyperlru) 9 | [![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/Kikobeats) 10 | 11 | > Tiny & Fast LRU Implementation as possible. 12 | 13 | ## Features 14 | 15 | * **Fast**: High performance (See [benchmark](#benchmark)). 16 | * **Simple**: The whole project is ~60 lines of code. 17 | * **Lightweight**: The package weighs less than a megabyte, with zero dependencies. 18 | 19 | There are a lot of LRU implementations, but most of them have a poor perfomance and they are hard to understand. 20 | 21 | **hyperlru** is an Abstract LRU implementation using traditional and efficienty data structures: 22 | 23 | - **Double Linked List**: It maintains the least recent list of items. 24 | - **Hash Table** It keeps the data for easily access to cache content. 25 | 26 | For use it, you need to provide one of the created providers: 27 | 28 | - [hyperlru-object](https://github.com/Kikobeats/hyperlru-object) 29 | - [hyperlru-map](https://github.com/Kikobeats/hyperlru-map) 30 | 31 | ## Install 32 | 33 | ```bash 34 | $ npm install hyperlru --save 35 | ``` 36 | 37 | ## Usage 38 | 39 | ```js 40 | const hyperlru = require('hyperlru') 41 | const myProvider = require('my-hyperlru-provider') 42 | 43 | const createLRU = hyperlru(myProvider) 44 | const cache = createLRU({ max: 1000 }) 45 | ``` 46 | 47 | ## Benchmark 48 | 49 | | name | size | gzip | set | get1 | update | get2 | evict | 50 | |-----------------------------------------------------|---------|---------|------|-------|--------|-------|-------| 51 | | [lru-fast](https://npm.im/lru-fast) | 2.34 kB | 793 B | 6855 | 27105 | 21550 | 25159 | 4003 | 52 | | [tiny-lru](https://npm.im/tiny-lru) | 4 kB | 1.64 kB | 4159 | 10746 | 18909 | 15925 | 4042 | 53 | | [lru_cache](https://npm.im/lru_cache) | 2.19 kB | 756 B | 5320 | 14489 | 10785 | 15963 | 4242 | 54 | | [simple-lru-cache](https://npm.im/simple-lru-cache) | 1.43 kB | 565 B | 3289 | 12134 | 8600 | 15266 | 3334 | 55 | | **[hyperlru-object](https://npm.im/hyperlru-object)** | 433 B | 265 B | 1152 | 8800 | 6205 | 8635 | 1039 | 56 | | [hashlru](https://npm.im/hashlru) | 628 B | 332 B | 4438 | 5834 | 4703 | 5960 | 3474 | 57 | | **[hyperlru-map](https://npm.im/hyperlru-map)** | 329 B | 232 B | 850 | 4555 | 4030 | 4397 | 690 | 58 | | [lru](https://npm.im/lru) | 6.07 kB | 1.86 kB | 2672 | 3302 | 3142 | 3898 | 1347 | 59 | | [lru-cache](https://npm.im/lru-cache) | 19.1 kB | 6.23 kB | 989 | 4702 | 3034 | 4536 | 773 | 60 | | [secondary-cache](https://npm.im/secondary-cache) | 22.6 kB | 6.54 kB | 1427 | 2292 | 2740 | 4579 | 1164 | 61 | | [quick-lru](https://npm.im/quick-lru) | 1.23 kB | 489 B | 2441 | 2075 | 2525 | 2119 | 2525 | 62 | | [modern-lru](https://npm.im/modern-lru) | 2.27 kB | 907 B | 1019 | 2531 | 2021 | 2456 | 731 | 63 | | [mkc](https://npm.im/mkc) | 10.5 kB | 3.61 kB | 729 | 1230 | 715 | 1129 | 575 | 64 | 65 | ## API 66 | 67 | ### hyperlru([options]) 68 | 69 | #### options 70 | 71 | ##### max 72 | 73 | Type: `number`
74 | Default: `1000` 75 | 76 | Max of element to keep into the cache. 77 | 78 | ### .set(key, value) 79 | 80 | Set the value of the key and mark the key as most recently used. 81 | 82 | It returns the `value`. 83 | 84 | ### .get(key) 85 | 86 | Query the value of the key and mark the key as most recently used. 87 | 88 | It returns the value of key if found; `undefined` otherwise. 89 | 90 | ### .peek(key) 91 | 92 | Query the value of the key without marking the key as most recently used. 93 | 94 | It returns the value of key if found; `undefined` otherwise. 95 | 96 | ### .keys() 97 | 98 | It retrieves all the keys currently in the cache. 99 | 100 | ### .values() 101 | 102 | It retrieves all the values currently in the cache. 103 | 104 | ### .clear() 105 | 106 | Clear all the elements in the cache. 107 | 108 | ### .remove(key) 109 | 110 | Remove the value from the cache. 111 | 112 | Returns: value of key if found; `undefined` otherwise. 113 | 114 | ## License 115 | 116 | MIT © [Kiko Beats](https://github.com/Kikobeats). 117 | -------------------------------------------------------------------------------- /compositor.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Kikobeats/hyperlru", 3 | "version": "0.1.4", 4 | "libraries": { 5 | "xv": "^1.1.25" 6 | }, 7 | "title": "", 8 | "branch": "", 9 | "style": { 10 | "name": "Material", 11 | "componentSet": { 12 | "nav": "nav/DarkAbsoluteNav", 13 | "header": "header/GradientHeader", 14 | "article": "article/BasicArticle", 15 | "footer": "footer/BasicFooter" 16 | }, 17 | "fontFamily": "Roboto, sans-serif", 18 | "heading": { 19 | "fontWeight": 500, 20 | "letterSpacing": "-0.01em" 21 | }, 22 | "colors": { 23 | "text": "#212121", 24 | "background": "#fff", 25 | "primary": "#2196f3", 26 | "secondary": "#1565c0", 27 | "highlight": "#ff4081", 28 | "border": "#e0e0e0", 29 | "muted": "#f5f5f5" 30 | }, 31 | "layout": { 32 | "centered": true, 33 | "bannerHeight": "80vh", 34 | "maxWidth": 896 35 | } 36 | }, 37 | "content": [ 38 | { 39 | "component": "nav", 40 | "links": [ 41 | { 42 | "href": "https://kikobeats.github.io/hyperlru/", 43 | "text": "Home" 44 | }, 45 | { 46 | "href": "https://github.com/Kikobeats/hyperlru", 47 | "text": "GitHub" 48 | }, 49 | { 50 | "href": "https://npmjs.com/package/hyperlru", 51 | "text": "npm" 52 | } 53 | ] 54 | }, 55 | { 56 | "component": "header", 57 | "heading": "hyperlru", 58 | "subhead": "Tiny & Fast LRU Implementation as possible.", 59 | "children": [ 60 | { 61 | "component": "ui/TweetButton", 62 | "text": "hyperlru: Tiny & Fast LRU Implementation as possible.", 63 | "url": "https://kikobeats.github.io/hyperlru/" 64 | }, 65 | { 66 | "component": "ui/GithubButton", 67 | "user": "Kikobeats", 68 | "repo": "hyperlru" 69 | } 70 | ], 71 | "text": "v2.1.2" 72 | }, 73 | { 74 | "component": "article", 75 | "metadata": { 76 | "source": "github.readme" 77 | }, 78 | "html": "\n

\n\n\n\n\n\n

\n
\n

Tiny & Fast LRU Implementation as possible.

\n
\n

Features

\n\n

There are a lot of LRU implementations, but most of them have a poor perfomance and they are hard to understand.

\n

hyperlru is an Abstract LRU implementation using traditional and efficienty data structures:

\n\n

For use it, you need to provide one of the created providers:

\n\n

Install

\n
$ npm install hyperlru --save

Usage

\n
const hyperlru = require('hyperlru')\nconst myProvider = require('my-hyperlru-provider')\n\nconst createLRU = hyperlru(myProvider)\nconst cache = createLRU({max: 1000})

Benchmark

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
namesizegzipsetget1updateget2evict
lru-fast2.34 kB793 B68552710521550251594003
tiny-lru4 kB1.64 kB41591074618909159254042
lru_cache2.19 kB756 B53201448910785159634242
simple-lru-cache1.43 kB565 B3289121348600152663334
hyperlru-object433 B265 B11528800620586351039
hashlru628 B332 B44385834470359603474
hyperlru-map329 B232 B850455540304397690
lru6.07 kB1.86 kB26723302314238981347
lru-cache19.1 kB6.23 kB989470230344536773
secondary-cache22.6 kB6.54 kB14272292274045791164
quick-lru1.23 kB489 B24412075252521192525
modern-lru2.27 kB907 B1019253120212456731
mkc10.5 kB3.61 kB72912307151129575
\n

API

\n

hyperlru([options])

\n

options

\n
max
\n

Type: number
\nDefault: 1000

\n

Max of element to keep into the cache.

\n

.set(key, value)

\n

Set the value of the key and mark the key as most recently used.

\n

It returns the value.

\n

.get(key)

\n

Query the value of the key and mark the key as most recently used.

\n

It returns the value of key if found; undefined otherwise.

\n

.peek(key)

\n

Query the value of the key without marking the key as most recently used.

\n

It returns the value of key if found; undefined otherwise.

\n

.keys()

\n

It retrieves all the keys currently in the cache.

\n

.values()

\n

It retrieves all the values currently in the cache.

\n

.clear()

\n

Clear all the elements in the cache.

\n

.remove(key)

\n

Remove the value from the cache.

\n

Returns: value of key if found; undefined otherwise.

\n

License

\n

MIT © Kiko Beats.

\n" 79 | }, 80 | { 81 | "component": "footer", 82 | "links": [ 83 | { 84 | "href": "https://github.com/Kikobeats/hyperlru", 85 | "text": "GitHub" 86 | }, 87 | { 88 | "href": "https://github.com/Kikobeats", 89 | "text": "Kikobeats" 90 | } 91 | ] 92 | } 93 | ] 94 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = require('./src') 4 | module.exports.test = require('./test') 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hyperlru", 3 | "description": "Tiny & Fast LRU Implementation as possible", 4 | "homepage": "https://github.com/Kikobeats/hyperlru", 5 | "version": "2.1.2", 6 | "main": "index.js", 7 | "author": { 8 | "email": "josefrancisco.verdu@gmail.com", 9 | "name": "Kiko Beats", 10 | "url": "https://github.com/Kikobeats" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/kikobeats/hyperlru.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/Kikobeats/hyperlru/issues" 18 | }, 19 | "keywords": [ 20 | "buffer", 21 | "cache", 22 | "caching", 23 | "fast", 24 | "hash", 25 | "least", 26 | "lru", 27 | "mru", 28 | "quick", 29 | "recently", 30 | "used" 31 | ], 32 | "dependencies": { 33 | "linked-list": "~2.1.0" 34 | }, 35 | "devDependencies": { 36 | "git-dirty": "latest", 37 | "should": "latest", 38 | "standard": "latest", 39 | "standard-markdown": "latest" 40 | }, 41 | "engines": { 42 | "node": ">= 6" 43 | }, 44 | "files": [ 45 | "index.js", 46 | "src/index.js", 47 | "test/index.js" 48 | ], 49 | "scripts": { 50 | "clean": "rm -rf node_modules", 51 | "lint": "standard-markdown && standard", 52 | "pretest": "npm run lint", 53 | "test": "echo 'test on connectors'" 54 | }, 55 | "license": "MIT", 56 | "standard": { 57 | "globals": [ 58 | "describe", 59 | "it" 60 | ] 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const LinkedList = require('linked-list') 4 | const exists = value => value !== undefined 5 | 6 | const hyperlru = createStore => { 7 | if (!createStore) { 8 | return new TypeError('You need to specify an store implementation') 9 | } 10 | 11 | return ({ max }) => { 12 | let dict = createStore() 13 | let list = new LinkedList() 14 | let size = max 15 | 16 | const _get = ({ isPeek }) => key => { 17 | const entry = dict.get(key) 18 | if (!exists(entry)) return 19 | if (isPeek) list.append(entry) 20 | return entry.value 21 | } 22 | 23 | function set (key, value) { 24 | let entry = dict.get(key) 25 | 26 | if (exists(entry)) { 27 | entry.value = value 28 | list.append(entry) 29 | } else { 30 | entry = Object.assign(new LinkedList.Item(), { key, value }) 31 | dict.set(key, entry) 32 | !size ? dict.delete(list.head.detach().key) : --size 33 | list.append(entry) 34 | } 35 | 36 | return value 37 | } 38 | 39 | function clear () { 40 | dict = createStore() 41 | list = new LinkedList() 42 | size = 0 43 | } 44 | 45 | function remove (key) { 46 | const entry = dict.get(key) 47 | if (!exists(entry)) return 48 | dict.delete(entry.detach().key) 49 | --size 50 | } 51 | 52 | const keys = () => dict.keys() 53 | const has = key => dict.has(key) 54 | const get = _get({ isPeek: true }) 55 | const peek = _get({ isPeek: false }) 56 | const values = () => dict.values() 57 | 58 | return { set, keys, get, clear, remove, has, peek, values } 59 | } 60 | } 61 | 62 | module.exports = hyperlru 63 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const should = require('should') 4 | const range = n => [...Array(n).keys()] 5 | const hyperlru = require('..') 6 | 7 | module.exports = createStore => { 8 | const createLRU = hyperlru(createStore) 9 | 10 | describe('constructor', function () { 11 | describe('options', function () { 12 | it('max', function () { 13 | const cache = createLRU({ max: 3 }) 14 | range(10).forEach((n) => cache.set(`foo${n}`, `bar${n}`)) 15 | should(cache.keys()).be.eql(['foo7', 'foo8', 'foo9']) 16 | }) 17 | }) 18 | }) 19 | 20 | describe('.get', function () { 21 | it('not previous set value is undefined', function () { 22 | const cache = createLRU({ max: 3 }) 23 | should(cache.get('foo')).be.undefined() 24 | }) 25 | it('return previous declared value', function () { 26 | const cache = createLRU({ max: 3 }) 27 | range(10).forEach((n) => cache.set(`foo${n}`, `bar${n}`)) 28 | should(cache.get('foo8')).be.equal('bar8') 29 | }) 30 | }) 31 | 32 | describe('.set', function () { 33 | it('set a value and retrieve it', function () { 34 | const cache = createLRU({ max: 3 }) 35 | should(cache.set('foo', 'bar')).be.equal('bar') 36 | }) 37 | }) 38 | 39 | describe('.keys', function () { 40 | it('retrieve all the keys', function () { 41 | const cache = createLRU({ max: 2 }) 42 | range(5).forEach((n) => cache.set(`foo${n}`, `bar${n}`)) 43 | should(cache.keys()).be.eql(['foo3', 'foo4']) 44 | }) 45 | }) 46 | 47 | describe('.clear', function () { 48 | it('remove all the elements', function () { 49 | const cache = createLRU({ max: 2 }) 50 | cache.set('foo', 'bar') 51 | cache.clear() 52 | should(cache.keys().length).be.equal(0) 53 | }) 54 | }) 55 | 56 | describe('.remove', function () { 57 | it('remove one time', function () { 58 | const cache = createLRU({ max: 2 }) 59 | range(5).forEach((n) => cache.set(`foo${n}`, `bar${n}`)) 60 | cache.remove('foo4') 61 | should(cache.keys()).be.eql(['foo3']) 62 | }) 63 | }) 64 | 65 | describe('.has', function () { 66 | it('a key that was added previously', function () { 67 | const cache = createLRU({ max: 2 }) 68 | cache.set('foo', 'bar') 69 | should(cache.has('foo')).be.true() 70 | }) 71 | }) 72 | 73 | describe('.values', function () { 74 | it('get all values present in the cache', function () { 75 | const cache = createLRU({ max: 2 }) 76 | cache.set('foo', 'bar') 77 | should(cache.values()).be.eql(['bar']) 78 | }) 79 | }) 80 | } 81 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require should 2 | --reporter spec 3 | --timeout 120000 4 | --slow 300 5 | --bail 6 | --recursive 7 | --------------------------------------------------------------------------------