Skip to content

Instantly share code, notes, and snippets.

@slavahatnuke
Created July 15, 2020 12:49
Show Gist options
  • Save slavahatnuke/8bdfc38335c9ea6e55b5bb3ac1ffaa27 to your computer and use it in GitHub Desktop.
Save slavahatnuke/8bdfc38335c9ea6e55b5bb3ac1ffaa27 to your computer and use it in GitHub Desktop.
const LRU = require("lru-cache");
async function test1() {
console.log('TEST1')
const cache = new LRU({
max: 1,
dispose: function (key, n) {
console.log('close', key, n)
},
maxAge: 1e3
});
cache.set('connection-1', 'CONNECTION_VALUE_1')
cache.set('connection-1', 'CONNECTION_VALUE_2')
cache.set('connection-2', 'CONNECTION_VALUE_2')
}
async function test2() {
console.log('TEST2')
const maxAge = 1e3;
const cache = new LRU({
max: 100,
dispose: function (key, n) {
console.log('close', key, n)
},
maxAge: maxAge,
length: (n, key) => {
// console.log('LEN', n, key)
setTimeout(() => cache.get(key), maxAge + 1)
return 1;
}
});
cache.set('connection-1', 'CONNECTION_VALUE_1')
// console.log('GET-1', cache.get('connection-1'))
// await new Promise((resolve) => {
// setTimeout(resolve, 2e3)
// })
// console.log('GET-2', cache.get('connection-1'))
// cache.set('connection-1', 'CONNECTION_VALUE_2')
}
async function test() {
await test1()
await test2()
}
test().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment