UNPKG

@appigram/keyv-lru

Version:

An in-memory LRU back-end for Keyv.

41 lines (31 loc) 672 B
"use strict"; const lru = require('tiny-lru'); const EventEmitter = require('events'); /** * An adaptor from tiny-lru to a Map API. */ class KeyvLru extends EventEmitter { // @TODO: Type this in a less generic way. constructor(options = { max: 500 }) { super(); this.defaultTtl = options.ttl; this.cache = lru(options.max, this.defaultTtl); } clear() { this.cache.clear(); } delete(key) { const removed = this.cache.delete(key); return typeof removed !== 'undefined'; } get(key) { return this.cache.get(key); } set(key, value) { this.cache.set(key, value); return 1; } } module.exports = KeyvLru;