├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Mathias Buus 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 | # refpool 2 | 3 | Pool of references that gc the least recently unref'ed ones when it reaches a max size 4 | 5 | ``` 6 | npm install refpool 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` js 12 | const Pool = require('refpool') 13 | 14 | const p = new Pool({ 15 | maxSize: 42, 16 | close (data) { 17 | console.log('should close', data) 18 | } 19 | }) 20 | 21 | const someResource = ... 22 | 23 | p.set(key, val) // add a key to the pool 24 | p.get(key) // get a val (bumps it) get(key, false) does not bump 25 | p.add(data) // sugar for p.set(data, data) 26 | const e = p.entry(key) // get the cache entry object out. 27 | // you can call e.increment, decrement and bump on this direcly 28 | 29 | p.increment(key) // add it and increment the reference count 30 | p.decrement(key) // decrement the ref count 31 | p.bump(key) // indicate that you used a thing in the pool 32 | ``` 33 | 34 | When more than `maxSize` items are inserted the least recently used 35 | resource with no references will be passed to close. 36 | 37 | ## License 38 | 39 | MIT 40 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const TOS = require('time-ordered-set') 2 | 3 | class Entry { 4 | constructor (pool, key, val) { 5 | this.pool = pool 6 | this.prev = null 7 | this.next = null 8 | this.key = key 9 | this.value = val 10 | this.refs = 0 11 | } 12 | 13 | bump () { 14 | if (this.refs > 0) return 15 | this.pool.gcable.add(this) 16 | this.pool._gcMaybe() 17 | } 18 | 19 | delete () { 20 | this.pool.gcable.remove(this) 21 | this.pool.entries.delete(this.key) 22 | if (this.pool.close) this.pool.close(this.value) 23 | } 24 | 25 | increment () { 26 | this.refs++ 27 | if (this.refs === 1) { 28 | this.pool.gcable.remove(this) 29 | } 30 | this.pool._gcMaybe() 31 | } 32 | 33 | decrement () { 34 | this.refs-- 35 | if (this.refs === 0) { 36 | this.pool.gcable.add(this) 37 | this.pool._gcMaybe() 38 | } 39 | this.pool._gcMaybe() 40 | } 41 | } 42 | 43 | module.exports = class Pool { 44 | constructor ({ maxSize = Infinity, close } = {}) { 45 | this.maxSize = maxSize 46 | this.close = close 47 | this.gcable = new TOS() 48 | this.entries = new Map() 49 | } 50 | 51 | get size () { 52 | return this.entries.size 53 | } 54 | 55 | isFull () { 56 | return this.entries.size < this.maxSize 57 | } 58 | 59 | entry (key) { 60 | return this.entries.get(key) 61 | } 62 | 63 | get (key, bump = true) { 64 | const entry = this.entry(key) 65 | if (!entry) return 66 | if (bump) entry.bump() 67 | return entry.value 68 | } 69 | 70 | delete (key) { 71 | const e = this.entry(key) 72 | if (e) e.delete() 73 | } 74 | 75 | add (val, forceGC = false) { 76 | return this.set(val, val, forceGC) 77 | } 78 | 79 | has (key) { 80 | return this.entries.has(key) 81 | } 82 | 83 | set (key, val, forceGC = false) { 84 | const existing = this.entries.get(key) 85 | if (existing) { 86 | existing.bump() 87 | existing.value = val 88 | return existing 89 | } 90 | const entry = new Entry(this, key, val) 91 | this.gcable.add(entry) 92 | this.entries.set(key, entry) 93 | // allow gc list to grow by one if otherwise we could destroy ourself unless forceGC is set 94 | this._gcMaybe(!forceGC) 95 | return entry 96 | } 97 | 98 | gc () { 99 | const oldest = this.gcable.oldest 100 | if (!oldest) return null 101 | oldest.delete() 102 | return oldest.value 103 | } 104 | 105 | _gcMaybe (allowOne = false) { 106 | if (this.gcable.length === 1 && allowOne) return 107 | if (this.entries.size <= this.maxSize && this.close) return 108 | this.gc() 109 | } 110 | 111 | increment (key) { 112 | const e = this.entry(key) 113 | if (e) e.increment() 114 | } 115 | 116 | decrement (key) { 117 | const e = this.entry(key) 118 | if (e) e.decrement() 119 | } 120 | 121 | bump (key) { 122 | const e = this.entry(key) 123 | if (e) e.bump() 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "refpool", 3 | "version": "1.2.2", 4 | "description": "Pool of references that gc the least recently unref'ed ones when it reaches a max size", 5 | "main": "index.js", 6 | "dependencies": { 7 | "time-ordered-set": "^1.0.2" 8 | }, 9 | "devDependencies": { 10 | "standard": "^14.3.4", 11 | "tape": "^5.0.1" 12 | }, 13 | "scripts": { 14 | "test": "standard && tape test.js" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/mafintosh/refpool.git" 19 | }, 20 | "author": "Mathias Buus (@mafintosh)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/mafintosh/refpool/issues" 24 | }, 25 | "homepage": "https://github.com/mafintosh/refpool" 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const tape = require('tape') 2 | const Pool = require('./') 3 | 4 | tape('basic', function (t) { 5 | const closes = [4, 2, 3, 5] 6 | const p = new Pool({ 7 | maxSize: 2, 8 | close (data) { 9 | t.same(data, closes.shift()) 10 | } 11 | }) 12 | 13 | p.add(1) 14 | p.increment(1) 15 | 16 | p.add(2) 17 | p.increment(2) 18 | 19 | p.add(3) 20 | p.increment(3) 21 | 22 | p.add(4) 23 | 24 | p.increment(2) 25 | p.decrement(2) 26 | p.decrement(2) 27 | 28 | p.decrement(3) 29 | p.add(5) 30 | p.bump(1) 31 | p.add(6) 32 | 33 | t.same(closes.length, 0) 34 | 35 | t.end() 36 | }) 37 | --------------------------------------------------------------------------------