all files / Caches/ LRUCache.js

15.38% Statements 2/13
0% Branches 0/10
0% Functions 0/6
15.38% Lines 2/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                                                                  
import LRUCache from 'lru-cache'
 
class _LRUCache {
    constructor(opts) {
        const options = Object.assign(
            {
                max: 500,
                length: function(n, key) {
                    return 1
                },
                dispose: function(key, n) {},
            },
            opts || {},
        )
 
        if (options.maxAge) {
            console.error(
                "[kayn] Do not use options.maxAge. It will not do anything. Rely on ttl's instead.",
            )
        }
 
        this.cache = LRUCache(options)
    }
 
    get(args, cb) {
        const blob = this.cache.get(args.key)
        return blob ? cb(null, blob) : cb("cache key doesn't exist", null)
    }
 
    set(args, value) {
        this.cache.set(args.key, value, args.ttl ? args.ttl : null)
    }
 
    flushCache(cb) {
        this.cache.reset()
        if (typeof cb === 'function') {
            cb(null, 'OK')
        }
    }
}
 
export default _LRUCache