Skip to content

Instantly share code, notes, and snippets.

@slavahatnuke
Created July 15, 2020 12:49
Show Gist options
  • Select an option

  • Save slavahatnuke/8bdfc38335c9ea6e55b5bb3ac1ffaa27 to your computer and use it in GitHub Desktop.

Select an option

Save slavahatnuke/8bdfc38335c9ea6e55b5bb3ac1ffaa27 to your computer and use it in GitHub Desktop.

Revisions

  1. slavahatnuke created this gist Jul 15, 2020.
    53 changes: 53 additions & 0 deletions lru-maxAge-close-connection.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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)